Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | #include "ethernet_interface.hpp" |
| 4 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 5 | #include "config_parser.hpp" |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 6 | #include "network_manager.hpp" |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 7 | #include "system_queries.hpp" |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 8 | #include "util.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 9 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 10 | #include <fmt/compile.h> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 11 | #include <fmt/format.h> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 12 | #include <linux/if_addr.h> |
| 13 | #include <linux/neighbour.h> |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 14 | #include <linux/rtnetlink.h> |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 15 | #include <net/if.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 16 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 17 | #include <algorithm> |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 18 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 19 | #include <phosphor-logging/elog-errors.hpp> |
| 20 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 21 | #include <sdbusplus/bus/match.hpp> |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 22 | #include <stdplus/raw.hpp> |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 23 | #include <stdplus/zstring.hpp> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 24 | #include <string> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 25 | #include <unordered_map> |
| 26 | #include <variant> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 27 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 28 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 29 | namespace phosphor |
| 30 | { |
| 31 | namespace network |
| 32 | { |
| 33 | |
| 34 | using namespace phosphor::logging; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 35 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 36 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 37 | using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 38 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 39 | using std::literals::string_view_literals::operator""sv; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 40 | constexpr auto RESOLVED_SERVICE = "org.freedesktop.resolve1"; |
| 41 | constexpr auto RESOLVED_INTERFACE = "org.freedesktop.resolve1.Link"; |
| 42 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 43 | constexpr auto RESOLVED_SERVICE_PATH = "/org/freedesktop/resolve1/link/"; |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 44 | |
| 45 | constexpr auto TIMESYNCD_SERVICE = "org.freedesktop.timesync1"; |
| 46 | constexpr auto TIMESYNCD_INTERFACE = "org.freedesktop.timesync1.Manager"; |
| 47 | constexpr auto TIMESYNCD_SERVICE_PATH = "/org/freedesktop/timesync1"; |
| 48 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 49 | constexpr auto METHOD_GET = "Get"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 50 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 51 | template <typename Func> |
| 52 | inline decltype(std::declval<Func>()()) |
| 53 | ignoreError(std::string_view msg, stdplus::zstring_view intf, |
| 54 | decltype(std::declval<Func>()()) fallback, Func&& func) noexcept |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 55 | { |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 56 | try |
| 57 | { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 58 | return func(); |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 59 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 60 | catch (const std::exception& e) |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 61 | { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 62 | auto err = fmt::format("{} failed on {}: {}", msg, intf, e.what()); |
| 63 | log<level::ERR>(err.c_str(), entry("INTERFACE=%s", intf.c_str())); |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 64 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 65 | return fallback; |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 66 | } |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 67 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 68 | static std::string makeObjPath(std::string_view root, std::string_view intf) |
| 69 | { |
| 70 | auto ret = fmt::format(FMT_COMPILE("{}/{}"), root, intf); |
| 71 | std::replace(ret.begin() + ret.size() - intf.size(), ret.end(), '.', '_'); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | EthernetInterface::EthernetInterface(sdbusplus::bus_t& bus, Manager& manager, |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 76 | const system::InterfaceInfo& info, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 77 | std::string_view objRoot, |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 78 | const config::Parser& config, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 79 | bool emitSignal, |
| 80 | std::optional<bool> enabled) : |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 81 | EthernetInterface(bus, manager, info, makeObjPath(objRoot, *info.name), |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 82 | config, emitSignal, enabled) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | EthernetInterface::EthernetInterface(sdbusplus::bus_t& bus, Manager& manager, |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 87 | const system::InterfaceInfo& info, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 88 | std::string&& objPath, |
| 89 | const config::Parser& config, |
| 90 | bool emitSignal, |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 91 | std::optional<bool> enabled) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 92 | Ifaces(bus, objPath.c_str(), |
| 93 | emitSignal ? Ifaces::action::defer_emit |
| 94 | : Ifaces::action::emit_no_signals), |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 95 | bus(bus), manager(manager), objPath(std::move(objPath)), ifIdx(info.idx) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 96 | { |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 97 | interfaceName(*info.name); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 98 | auto dhcpVal = getDHCPValue(config); |
| 99 | EthernetInterfaceIntf::dhcp4(dhcpVal.v4); |
| 100 | EthernetInterfaceIntf::dhcp6(dhcpVal.v6); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 101 | EthernetInterfaceIntf::ipv6AcceptRA(getIPv6AcceptRA(config)); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 102 | EthernetInterfaceIntf::nicEnabled(enabled ? *enabled : queryNicEnabled()); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 103 | { |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 104 | const auto& gws = manager.getRouteTable().getDefaultGateway(); |
| 105 | auto it = gws.find(ifIdx); |
| 106 | if (it != gws.end()) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 107 | { |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 108 | EthernetInterfaceIntf::defaultGateway(std::to_string(it->second)); |
| 109 | } |
| 110 | } |
| 111 | { |
| 112 | const auto& gws = manager.getRouteTable().getDefaultGateway6(); |
| 113 | auto it = gws.find(ifIdx); |
| 114 | if (it != gws.end()) |
| 115 | { |
| 116 | EthernetInterfaceIntf::defaultGateway6(std::to_string(it->second)); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 120 | EthernetInterfaceIntf::ntpServers( |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 121 | config.map.getValueStrings("Network", "NTP")); |
Ratan Gupta | 613a012 | 2020-04-24 15:18:53 +0530 | [diff] [blame] | 122 | |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 123 | if (ifIdx > 0) |
| 124 | { |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 125 | auto ethInfo = ignoreError("GetEthInfo", *info.name, {}, [&] { |
| 126 | return system::getEthInfo(*info.name); |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 127 | }); |
| 128 | EthernetInterfaceIntf::autoNeg(ethInfo.autoneg); |
| 129 | EthernetInterfaceIntf::speed(ethInfo.speed); |
| 130 | } |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 131 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 132 | updateInfo(info); |
| 133 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 134 | if (info.vlan_id) |
| 135 | { |
| 136 | if (!info.parent_idx) |
| 137 | { |
| 138 | std::runtime_error("Missing parent link"); |
| 139 | } |
| 140 | vlan.emplace(bus, this->objPath.c_str(), info, *this, emitSignal); |
| 141 | } |
| 142 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 143 | // Emit deferred signal. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 144 | if (emitSignal) |
| 145 | { |
| 146 | this->emit_object_added(); |
| 147 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 148 | } |
| 149 | |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 150 | void EthernetInterface::updateInfo(const system::InterfaceInfo& info) |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 151 | { |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 152 | EthernetInterfaceIntf::linkUp(info.flags & IFF_RUNNING); |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 153 | if (info.mac) |
| 154 | { |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 155 | MacAddressIntf::macAddress(std::to_string(*info.mac)); |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 156 | } |
| 157 | if (info.mtu) |
| 158 | { |
| 159 | EthernetInterfaceIntf::mtu(*info.mtu); |
| 160 | } |
| 161 | } |
| 162 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 163 | static IP::Protocol getProtocol(const InAddrAny& addr) |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 164 | { |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 165 | if (std::holds_alternative<in_addr>(addr)) |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 166 | { |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 167 | return IP::Protocol::IPv4; |
| 168 | } |
| 169 | else if (std::holds_alternative<in6_addr>(addr)) |
| 170 | { |
| 171 | return IP::Protocol::IPv6; |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 172 | } |
| 173 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 174 | throw std::runtime_error("Invalid addr type"); |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 175 | } |
| 176 | |
William A. Kennington III | 24957b9 | 2021-12-03 13:59:19 -0800 | [diff] [blame] | 177 | bool EthernetInterface::dhcpIsEnabled(IP::Protocol family) |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 178 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 179 | switch (family) |
| 180 | { |
| 181 | case IP::Protocol::IPv6: |
| 182 | return dhcp6(); |
| 183 | case IP::Protocol::IPv4: |
| 184 | return dhcp4(); |
| 185 | } |
| 186 | throw std::logic_error("Unreachable"); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 189 | bool EthernetInterface::originIsManuallyAssigned(IP::AddressOrigin origin) |
| 190 | { |
| 191 | return ( |
| 192 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
| 193 | (origin == IP::AddressOrigin::Static) |
| 194 | #else |
| 195 | (origin == IP::AddressOrigin::Static || |
| 196 | origin == IP::AddressOrigin::LinkLocal) |
| 197 | #endif |
| 198 | |
| 199 | ); |
| 200 | } |
| 201 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 202 | void EthernetInterface::createIPAddressObjects() |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 203 | { |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 204 | addrs.clear(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 205 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 206 | AddressFilter filter; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 207 | filter.interface = ifIdx; |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 208 | auto currentAddrs = getCurrentAddresses(filter); |
| 209 | for (const auto& addr : currentAddrs) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 210 | { |
William A. Kennington III | 13d1708 | 2021-11-04 21:36:54 -0700 | [diff] [blame] | 211 | if (addr.flags & IFA_F_DEPRECATED) |
| 212 | { |
| 213 | continue; |
| 214 | } |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 215 | auto address = std::to_string(addr.address); |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 216 | IP::Protocol addressType = getProtocol(addr.address); |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 217 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 218 | if (dhcpIsEnabled(addressType)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 219 | { |
| 220 | origin = IP::AddressOrigin::DHCP; |
| 221 | } |
Ali El-Haj-Mahmoud | 300ed5c | 2022-10-26 09:58:04 -0400 | [diff] [blame] | 222 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 223 | if (addr.scope == RT_SCOPE_LINK) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 224 | { |
| 225 | origin = IP::AddressOrigin::LinkLocal; |
| 226 | } |
Ali El-Haj-Mahmoud | 300ed5c | 2022-10-26 09:58:04 -0400 | [diff] [blame] | 227 | #endif |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 228 | |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 229 | auto ipAddressObjectPath = |
| 230 | generateObjectPath(addressType, address, addr.prefix, origin); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 231 | |
Lei YU | 7233c58 | 2021-04-08 14:39:43 +0800 | [diff] [blame] | 232 | this->addrs.insert_or_assign( |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 233 | address, std::make_unique<IPAddress>(bus, ipAddressObjectPath, |
| 234 | *this, addressType, address, |
| 235 | origin, addr.prefix)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 236 | } |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 237 | } |
| 238 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 239 | void EthernetInterface::createStaticNeighborObjects() |
| 240 | { |
| 241 | staticNeighbors.clear(); |
| 242 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 243 | NeighborFilter filter; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 244 | filter.interface = ifIdx; |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 245 | filter.state = NUD_PERMANENT; |
| 246 | auto neighbors = getCurrentNeighbors(filter); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 247 | for (const auto& neighbor : neighbors) |
| 248 | { |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 249 | if (!neighbor.mac) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 250 | { |
| 251 | continue; |
| 252 | } |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 253 | auto ip = std::to_string(neighbor.address); |
| 254 | auto mac = std::to_string(*neighbor.mac); |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 255 | auto objectPath = generateStaticNeighborObjectPath(ip, mac); |
William A. Kennington III | 9a20a6e | 2022-10-05 13:41:12 -0700 | [diff] [blame] | 256 | staticNeighbors.emplace( |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 257 | ip, std::make_unique<Neighbor>(bus, objectPath, *this, ip, mac, |
| 258 | Neighbor::State::Permanent)); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 262 | ObjectPath EthernetInterface::ip(IP::Protocol protType, std::string ipaddress, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 263 | uint8_t prefixLength, std::string) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 264 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 265 | if (dhcpIsEnabled(protType)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 266 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 267 | log<level::INFO>("DHCP enabled on the interface, disabling"), |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 268 | entry("INTERFACE=%s", interfaceName().c_str()); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 269 | switch (protType) |
| 270 | { |
| 271 | case IP::Protocol::IPv4: |
| 272 | dhcp4(false); |
| 273 | break; |
| 274 | case IP::Protocol::IPv6: |
| 275 | dhcp6(false); |
| 276 | break; |
| 277 | } |
Ravi Teja | 0745044 | 2022-07-07 04:30:57 -0500 | [diff] [blame] | 278 | // Delete the IP address object and that reloads the networkd |
| 279 | // to allow the same IP address to be set as Static IP |
| 280 | deleteObject(ipaddress); |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 281 | } |
| 282 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 283 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
| 284 | |
| 285 | int addressFamily = (protType == IP::Protocol::IPv4) ? AF_INET : AF_INET6; |
| 286 | |
| 287 | if (!isValidIP(addressFamily, ipaddress)) |
| 288 | { |
| 289 | log<level::ERR>("Not a valid IP address"), |
| 290 | entry("ADDRESS=%s", ipaddress.c_str()); |
| 291 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"), |
| 292 | Argument::ARGUMENT_VALUE(ipaddress.c_str())); |
| 293 | } |
| 294 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 295 | if (!isValidPrefix(addressFamily, prefixLength)) |
| 296 | { |
| 297 | log<level::ERR>("PrefixLength is not correct "), |
William A. Kennington III | f8c78f2 | 2019-04-20 20:32:59 -0700 | [diff] [blame] | 298 | entry("PREFIXLENGTH=%" PRIu8, prefixLength); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 299 | elog<InvalidArgument>( |
| 300 | Argument::ARGUMENT_NAME("prefixLength"), |
| 301 | Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str())); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 302 | } |
| 303 | |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 304 | auto objectPath = |
| 305 | generateObjectPath(protType, ipaddress, prefixLength, origin); |
William A. Kennington III | 9a20a6e | 2022-10-05 13:41:12 -0700 | [diff] [blame] | 306 | this->addrs.insert_or_assign( |
| 307 | ipaddress, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 308 | std::make_unique<IPAddress>(bus, objectPath, *this, protType, ipaddress, |
| 309 | origin, prefixLength)); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 310 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 311 | writeConfigurationFile(); |
| 312 | manager.reloadConfigs(); |
| 313 | |
raviteja-b | ce37956 | 2019-03-28 05:59:36 -0500 | [diff] [blame] | 314 | return objectPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 315 | } |
| 316 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 317 | ObjectPath EthernetInterface::neighbor(std::string ipAddress, |
| 318 | std::string macAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 319 | { |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 320 | if (!isValidIP(ipAddress)) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 321 | { |
| 322 | log<level::ERR>("Not a valid IP address", |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 323 | entry("ADDRESS=%s", ipAddress.c_str())); |
| 324 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipAddress"), |
| 325 | Argument::ARGUMENT_VALUE(ipAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 326 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 327 | if (!mac_address::isUnicast(mac_address::fromString(macAddress))) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 328 | { |
| 329 | log<level::ERR>("Not a valid MAC address", |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 330 | entry("MACADDRESS=%s", ipAddress.c_str())); |
| 331 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("macAddress"), |
| 332 | Argument::ARGUMENT_VALUE(macAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 333 | } |
| 334 | |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 335 | auto objectPath = generateStaticNeighborObjectPath(ipAddress, macAddress); |
William A. Kennington III | 9a20a6e | 2022-10-05 13:41:12 -0700 | [diff] [blame] | 336 | staticNeighbors.emplace( |
| 337 | ipAddress, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 338 | std::make_unique<Neighbor>(bus, objectPath, *this, ipAddress, |
William A. Kennington III | 9a20a6e | 2022-10-05 13:41:12 -0700 | [diff] [blame] | 339 | macAddress, Neighbor::State::Permanent)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 340 | |
| 341 | writeConfigurationFile(); |
| 342 | manager.reloadConfigs(); |
| 343 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 344 | return objectPath; |
| 345 | } |
| 346 | |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 347 | void EthernetInterface::deleteObject(std::string_view ipaddress) |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 348 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 349 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 350 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 351 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 352 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 353 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 354 | } |
| 355 | this->addrs.erase(it); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 356 | |
| 357 | writeConfigurationFile(); |
| 358 | manager.reloadConfigs(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 359 | } |
| 360 | |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 361 | void EthernetInterface::deleteStaticNeighborObject(std::string_view ipAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 362 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 363 | auto it = staticNeighbors.find(ipAddress); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 364 | if (it == staticNeighbors.end()) |
| 365 | { |
| 366 | log<level::ERR>( |
| 367 | "DeleteStaticNeighborObject:Unable to find the object."); |
| 368 | return; |
| 369 | } |
| 370 | staticNeighbors.erase(it); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 371 | |
| 372 | writeConfigurationFile(); |
| 373 | manager.reloadConfigs(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 376 | std::string EthernetInterface::generateObjectPath( |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 377 | IP::Protocol addressType, std::string_view ipAddress, uint8_t prefixLength, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 378 | IP::AddressOrigin origin) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 379 | { |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 380 | std::string_view type; |
| 381 | switch (addressType) |
| 382 | { |
| 383 | case IP::Protocol::IPv4: |
| 384 | type = "ipv4"sv; |
| 385 | break; |
| 386 | case IP::Protocol::IPv6: |
| 387 | type = "ipv6"sv; |
| 388 | break; |
| 389 | } |
| 390 | return fmt::format( |
| 391 | FMT_COMPILE("{}/{}/{:08x}"), objPath, type, |
| 392 | static_cast<uint32_t>(hash_multi( |
| 393 | ipAddress, prefixLength, |
| 394 | static_cast<std::underlying_type_t<IP::AddressOrigin>>(origin)))); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 395 | } |
| 396 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 397 | std::string EthernetInterface::generateStaticNeighborObjectPath( |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 398 | std::string_view ipAddress, std::string_view macAddress) const |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 399 | { |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 400 | return fmt::format( |
| 401 | FMT_COMPILE("{}/static_neighbor/{:08x}"), objPath, |
| 402 | static_cast<uint32_t>(hash_multi(ipAddress, macAddress))); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 405 | bool EthernetInterface::ipv6AcceptRA(bool value) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 406 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 407 | if (ipv6AcceptRA() != EthernetInterfaceIntf::ipv6AcceptRA(value)) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 408 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 409 | writeConfigurationFile(); |
| 410 | manager.reloadConfigs(); |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 411 | } |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 412 | return value; |
| 413 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 414 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 415 | bool EthernetInterface::dhcp4(bool value) |
| 416 | { |
| 417 | if (dhcp4() != EthernetInterfaceIntf::dhcp4(value)) |
| 418 | { |
| 419 | writeConfigurationFile(); |
| 420 | manager.reloadConfigs(); |
| 421 | } |
| 422 | return value; |
| 423 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 424 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 425 | bool EthernetInterface::dhcp6(bool value) |
| 426 | { |
| 427 | if (dhcp6() != EthernetInterfaceIntf::dhcp6(value)) |
| 428 | { |
| 429 | writeConfigurationFile(); |
| 430 | manager.reloadConfigs(); |
| 431 | } |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 432 | return value; |
| 433 | } |
| 434 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 435 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled(DHCPConf value) |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 436 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 437 | auto old4 = EthernetInterfaceIntf::dhcp4(); |
| 438 | auto new4 = EthernetInterfaceIntf::dhcp4(value == DHCPConf::v4 || |
| 439 | value == DHCPConf::v4v6stateless || |
| 440 | value == DHCPConf::both); |
| 441 | auto old6 = EthernetInterfaceIntf::dhcp6(); |
| 442 | auto new6 = EthernetInterfaceIntf::dhcp6(value == DHCPConf::v6 || |
| 443 | value == DHCPConf::both); |
| 444 | auto oldra = EthernetInterfaceIntf::ipv6AcceptRA(); |
| 445 | auto newra = EthernetInterfaceIntf::ipv6AcceptRA( |
| 446 | value == DHCPConf::v6stateless || value == DHCPConf::v4v6stateless || |
| 447 | value == DHCPConf::v6 || value == DHCPConf::both); |
| 448 | |
| 449 | if (old4 != new4 || old6 != new6 || oldra != newra) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 450 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 451 | writeConfigurationFile(); |
| 452 | manager.reloadConfigs(); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 453 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 454 | return value; |
| 455 | } |
| 456 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 457 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled() const |
| 458 | { |
| 459 | if (dhcp6()) |
| 460 | { |
| 461 | return dhcp4() ? DHCPConf::both : DHCPConf::v6; |
| 462 | } |
| 463 | else if (dhcp4()) |
| 464 | { |
| 465 | return ipv6AcceptRA() ? DHCPConf::v4v6stateless : DHCPConf::v4; |
| 466 | } |
| 467 | return ipv6AcceptRA() ? DHCPConf::v6stateless : DHCPConf::none; |
| 468 | } |
| 469 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 470 | bool EthernetInterface::linkUp() const |
| 471 | { |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 472 | if (ifIdx == 0) |
| 473 | { |
| 474 | return EthernetInterfaceIntf::linkUp(); |
| 475 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 476 | return system::intfIsRunning(interfaceName()); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 479 | size_t EthernetInterface::mtu() const |
| 480 | { |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 481 | if (ifIdx == 0) |
| 482 | { |
| 483 | return EthernetInterfaceIntf::mtu(); |
| 484 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 485 | const auto ifname = interfaceName(); |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 486 | return ignoreError("GetMTU", ifname, std::nullopt, |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 487 | [&] { return system::getMTU(ifname); }) |
| 488 | .value_or(EthernetInterfaceIntf::mtu()); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | size_t EthernetInterface::mtu(size_t value) |
| 492 | { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 493 | const size_t old = EthernetInterfaceIntf::mtu(); |
| 494 | if (value == old) |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 495 | { |
| 496 | return value; |
| 497 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 498 | const auto ifname = interfaceName(); |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 499 | return EthernetInterfaceIntf::mtu(ignoreError("SetMTU", ifname, old, [&] { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 500 | system::setMTU(ifname, value); |
| 501 | return value; |
| 502 | })); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 503 | } |
| 504 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 505 | bool EthernetInterface::queryNicEnabled() const |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 506 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 507 | constexpr auto svc = "org.freedesktop.network1"; |
| 508 | constexpr auto intf = "org.freedesktop.network1.Link"; |
| 509 | constexpr auto prop = "AdministrativeState"; |
| 510 | char* rpath; |
| 511 | sd_bus_path_encode("/org/freedesktop/network1/link", |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 512 | std::to_string(ifIdx).c_str(), &rpath); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 513 | std::string path(rpath); |
| 514 | free(rpath); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 515 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 516 | // Store / Parser for the AdministrativeState return value |
| 517 | std::optional<bool> ret; |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 518 | auto cb = [&](std::string_view state) { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 519 | if (state != "initialized") |
| 520 | { |
| 521 | ret = state != "unmanaged"; |
| 522 | } |
| 523 | }; |
| 524 | |
| 525 | // Build a matcher before making the property call to ensure we |
| 526 | // can eventually get the value. |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 527 | sdbusplus::bus::match_t match( |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 528 | bus, |
| 529 | fmt::format("type='signal',sender='{}',path='{}',interface='{}',member=" |
| 530 | "'PropertiesChanged',arg0='{}',", |
| 531 | svc, path, PROPERTY_INTERFACE, intf) |
| 532 | .c_str(), |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 533 | [&](sdbusplus::message_t& m) { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 534 | std::string intf; |
| 535 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 536 | try |
| 537 | { |
| 538 | m.read(intf, values); |
| 539 | auto it = values.find(prop); |
| 540 | // Ignore properties that aren't AdministrativeState |
| 541 | if (it != values.end()) |
| 542 | { |
| 543 | cb(std::get<std::string>(it->second)); |
| 544 | } |
| 545 | } |
| 546 | catch (const std::exception& e) |
| 547 | { |
| 548 | log<level::ERR>( |
| 549 | fmt::format( |
| 550 | "AdministrativeState match parsing failed on {}: {}", |
| 551 | interfaceName(), e.what()) |
| 552 | .c_str(), |
| 553 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 554 | entry("ERROR=%s", e.what())); |
| 555 | } |
| 556 | }); |
| 557 | |
| 558 | // Actively call for the value in case the interface is already configured |
| 559 | auto method = |
| 560 | bus.new_method_call(svc, path.c_str(), PROPERTY_INTERFACE, METHOD_GET); |
| 561 | method.append(intf, prop); |
| 562 | try |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 563 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 564 | auto reply = bus.call(method); |
| 565 | std::variant<std::string> state; |
| 566 | reply.read(state); |
| 567 | cb(std::get<std::string>(state)); |
| 568 | } |
| 569 | catch (const std::exception& e) |
| 570 | { |
| 571 | log<level::ERR>( |
| 572 | fmt::format("Failed to get AdministrativeState on {}: {}", |
| 573 | interfaceName(), e.what()) |
| 574 | .c_str(), |
| 575 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 576 | entry("ERROR=%s", e.what())); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 577 | } |
| 578 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 579 | // The interface is not yet configured by systemd-networkd, wait until it |
| 580 | // signals us a valid state. |
| 581 | while (!ret) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 582 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 583 | bus.wait(); |
| 584 | bus.process_discard(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 585 | } |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 586 | |
| 587 | return *ret; |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 590 | bool EthernetInterface::nicEnabled(bool value) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 591 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 592 | if (value == EthernetInterfaceIntf::nicEnabled()) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 593 | { |
| 594 | return value; |
| 595 | } |
| 596 | |
William A. Kennington III | c922d5e | 2022-01-21 01:08:31 -0800 | [diff] [blame] | 597 | EthernetInterfaceIntf::nicEnabled(value); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 598 | writeConfigurationFile(); |
William A. Kennington III | 329b5fb | 2021-11-09 17:19:30 -0800 | [diff] [blame] | 599 | if (!value) |
| 600 | { |
| 601 | // We only need to bring down the interface, networkd will always bring |
| 602 | // up managed interfaces |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 603 | manager.addReloadPreHook( |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 604 | [ifname = interfaceName()]() { system::setNICUp(ifname, false); }); |
William A. Kennington III | 329b5fb | 2021-11-09 17:19:30 -0800 | [diff] [blame] | 605 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 606 | manager.reloadConfigs(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 607 | |
| 608 | return value; |
| 609 | } |
| 610 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 611 | ServerList EthernetInterface::staticNameServers(ServerList value) |
| 612 | { |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 613 | for (const auto& nameserverip : value) |
| 614 | { |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 615 | if (!isValidIP(nameserverip)) |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 616 | { |
| 617 | log<level::ERR>("Not a valid IP address"), |
| 618 | entry("ADDRESS=%s", nameserverip.c_str()); |
| 619 | elog<InvalidArgument>( |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 620 | Argument::ARGUMENT_NAME("StaticNameserver"), |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 621 | Argument::ARGUMENT_VALUE(nameserverip.c_str())); |
| 622 | } |
| 623 | } |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 624 | try |
| 625 | { |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 626 | EthernetInterfaceIntf::staticNameServers(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 627 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 628 | writeConfigurationFile(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 629 | manager.reloadConfigs(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 630 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 631 | catch (const InternalFailure& e) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 632 | { |
| 633 | log<level::ERR>("Exception processing DNS entries"); |
| 634 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 635 | return EthernetInterfaceIntf::staticNameServers(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 636 | } |
| 637 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 638 | void EthernetInterface::loadNTPServers(const config::Parser& config) |
| 639 | { |
| 640 | EthernetInterfaceIntf::ntpServers(getNTPServerFromTimeSyncd()); |
| 641 | EthernetInterfaceIntf::staticNTPServers( |
| 642 | config.map.getValueStrings("Network", "NTP")); |
| 643 | } |
| 644 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 645 | void EthernetInterface::loadNameServers(const config::Parser& config) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 646 | { |
| 647 | EthernetInterfaceIntf::nameservers(getNameServerFromResolvd()); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 648 | EthernetInterfaceIntf::staticNameServers( |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 649 | config.map.getValueStrings("Network", "DNS")); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 650 | } |
| 651 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 652 | ServerList EthernetInterface::getNTPServerFromTimeSyncd() |
| 653 | { |
| 654 | ServerList servers; // Variable to capture the NTP Server IPs |
| 655 | auto method = bus.new_method_call(TIMESYNCD_SERVICE, TIMESYNCD_SERVICE_PATH, |
| 656 | PROPERTY_INTERFACE, METHOD_GET); |
| 657 | |
| 658 | method.append(TIMESYNCD_INTERFACE, "LinkNTPServers"); |
| 659 | |
| 660 | try |
| 661 | { |
| 662 | auto reply = bus.call(method); |
| 663 | std::variant<ServerList> response; |
| 664 | reply.read(response); |
| 665 | servers = std::get<ServerList>(response); |
| 666 | } |
| 667 | catch (const sdbusplus::exception::SdBusError& e) |
| 668 | { |
| 669 | log<level::ERR>( |
| 670 | "Failed to get NTP server information from Systemd-Timesyncd"); |
| 671 | } |
| 672 | |
| 673 | return servers; |
| 674 | } |
| 675 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 676 | ServerList EthernetInterface::getNameServerFromResolvd() |
| 677 | { |
| 678 | ServerList servers; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 679 | auto OBJ_PATH = fmt::format("{}{}", RESOLVED_SERVICE_PATH, ifIdx); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 680 | |
| 681 | /* |
| 682 | The DNS property under org.freedesktop.resolve1.Link interface contains |
| 683 | an array containing all DNS servers currently used by resolved. It |
| 684 | contains similar information as the DNS server data written to |
| 685 | /run/systemd/resolve/resolv.conf. |
| 686 | |
| 687 | Each structure in the array consists of a numeric network interface index, |
| 688 | an address family, and a byte array containing the DNS server address |
| 689 | (either 4 bytes in length for IPv4 or 16 bytes in lengths for IPv6). |
| 690 | The array contains DNS servers configured system-wide, including those |
| 691 | possibly read from a foreign /etc/resolv.conf or the DNS= setting in |
| 692 | /etc/systemd/resolved.conf, as well as per-interface DNS server |
| 693 | information either retrieved from systemd-networkd or configured by |
| 694 | external software via SetLinkDNS(). |
| 695 | */ |
| 696 | |
| 697 | using type = std::vector<std::tuple<int32_t, std::vector<uint8_t>>>; |
| 698 | std::variant<type> name; // Variable to capture the DNS property |
| 699 | auto method = bus.new_method_call(RESOLVED_SERVICE, OBJ_PATH.c_str(), |
| 700 | PROPERTY_INTERFACE, METHOD_GET); |
| 701 | |
| 702 | method.append(RESOLVED_INTERFACE, "DNS"); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 703 | |
| 704 | try |
| 705 | { |
Asmitha Karunanithi | 97ddb8d | 2022-05-05 01:00:18 -0500 | [diff] [blame] | 706 | auto reply = bus.call(method); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 707 | reply.read(name); |
| 708 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 709 | catch (const sdbusplus::exception_t& e) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 710 | { |
| 711 | log<level::ERR>("Failed to get DNS information from Systemd-Resolved"); |
| 712 | } |
| 713 | auto tupleVector = std::get_if<type>(&name); |
| 714 | for (auto i = tupleVector->begin(); i != tupleVector->end(); ++i) |
| 715 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 716 | int addressFamily = std::get<0>(*i); |
| 717 | std::vector<uint8_t>& ipaddress = std::get<1>(*i); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 718 | servers.push_back(std::to_string( |
| 719 | addrFromBuf(addressFamily, stdplus::raw::asView<char>(ipaddress)))); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 720 | } |
| 721 | return servers; |
| 722 | } |
| 723 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 724 | ObjectPath EthernetInterface::createVLAN(uint16_t id) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 725 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 726 | auto intfName = fmt::format(FMT_COMPILE("{}.{}"), interfaceName(), id); |
| 727 | auto idStr = std::to_string(id); |
| 728 | if (manager.interfaces.find(intfName) != manager.interfaces.end()) |
Jiaqing Zhao | 33b4eaa | 2022-04-12 23:11:40 +0800 | [diff] [blame] | 729 | { |
| 730 | log<level::ERR>("VLAN already exists", entry("VLANID=%u", id)); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 731 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("VLANId"), |
| 732 | Argument::ARGUMENT_VALUE(idStr.c_str())); |
Jiaqing Zhao | 33b4eaa | 2022-04-12 23:11:40 +0800 | [diff] [blame] | 733 | } |
| 734 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 735 | auto objRoot = std::string_view(objPath).substr(0, objPath.rfind('/')); |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 736 | auto macStr = MacAddressIntf::macAddress(); |
| 737 | std::optional<ether_addr> mac; |
| 738 | if (!macStr.empty()) |
| 739 | { |
| 740 | mac.emplace(mac_address::fromString(macStr)); |
| 741 | } |
| 742 | auto info = system::InterfaceInfo{ |
| 743 | .idx = 0, // TODO: Query the correct value after creation |
| 744 | .flags = 0, |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 745 | .name = intfName, |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 746 | .mac = std::move(mac), |
| 747 | .mtu = mtu(), |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 748 | .parent_idx = ifIdx, |
| 749 | .vlan_id = id, |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 750 | }; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 751 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 752 | // Pass the parents nicEnabled property, so that the child |
Manojkiran Eda | ca8b91b | 2020-05-28 09:28:42 +0530 | [diff] [blame] | 753 | // VLAN interface can inherit. |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 754 | auto vlanIntf = std::make_unique<EthernetInterface>( |
| 755 | bus, manager, info, objRoot, config::Parser(), /*emit=*/true, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 756 | nicEnabled()); |
| 757 | ObjectPath ret = vlanIntf->objPath; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 758 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 759 | manager.interfaces.emplace(intfName, std::move(vlanIntf)); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 760 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 761 | // write the device file for the vlan interface. |
| 762 | config::Parser config; |
| 763 | auto& netdev = config.map["NetDev"].emplace_back(); |
| 764 | netdev["Name"].emplace_back(intfName); |
| 765 | netdev["Kind"].emplace_back("vlan"); |
| 766 | config.map["VLAN"].emplace_back()["Id"].emplace_back(std::move(idStr)); |
| 767 | config.writeFile(config::pathForIntfDev(manager.getConfDir(), intfName)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 768 | |
| 769 | writeConfigurationFile(); |
| 770 | manager.reloadConfigs(); |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 771 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 772 | return objPath; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 773 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 774 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 775 | ServerList EthernetInterface::staticNTPServers(ServerList value) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 776 | { |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 777 | try |
| 778 | { |
| 779 | EthernetInterfaceIntf::staticNTPServers(value); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 780 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 781 | writeConfigurationFile(); |
| 782 | manager.reloadConfigs(); |
| 783 | } |
| 784 | catch (InternalFailure& e) |
| 785 | { |
| 786 | log<level::ERR>("Exception processing NTP entries"); |
| 787 | } |
| 788 | return EthernetInterfaceIntf::staticNTPServers(); |
| 789 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 790 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 791 | ServerList EthernetInterface::ntpServers(ServerList /*servers*/) |
| 792 | { |
| 793 | elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property")); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 794 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 795 | // Need to merge the below function with the code which writes the |
| 796 | // config file during factory reset. |
| 797 | // TODO openbmc/openbmc#1751 |
| 798 | |
| 799 | void EthernetInterface::writeConfigurationFile() |
| 800 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 801 | config::Parser config; |
| 802 | config.map["Match"].emplace_back()["Name"].emplace_back(interfaceName()); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 803 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 804 | auto& link = config.map["Link"].emplace_back(); |
Johnathan Mantey | 609c12d | 2022-02-03 09:23:09 -0800 | [diff] [blame] | 805 | #ifdef PERSIST_MAC |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 806 | auto mac = MacAddressIntf::macAddress(); |
| 807 | if (!mac.empty()) |
| 808 | { |
| 809 | link["MACAddress"].emplace_back(mac); |
| 810 | } |
Johnathan Mantey | 609c12d | 2022-02-03 09:23:09 -0800 | [diff] [blame] | 811 | #endif |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 812 | if (!EthernetInterfaceIntf::nicEnabled()) |
| 813 | { |
| 814 | link["Unmanaged"].emplace_back("yes"); |
| 815 | } |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 816 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 817 | { |
| 818 | auto& network = config.map["Network"].emplace_back(); |
| 819 | auto& lla = network["LinkLocalAddressing"]; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 820 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 821 | lla.emplace_back("yes"); |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 822 | #else |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 823 | lla.emplace_back("no"); |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 824 | #endif |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 825 | network["IPv6AcceptRA"].emplace_back(ipv6AcceptRA() ? "true" : "false"); |
| 826 | network["DHCP"].emplace_back(dhcp4() ? (dhcp6() ? "true" : "ipv4") |
| 827 | : (dhcp6() ? "ipv6" : "false")); |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 828 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 829 | auto& vlans = network["VLAN"]; |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 830 | for (const auto& [_, intf] : manager.interfaces) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 831 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 832 | if (intf->vlan && intf->vlan->parentIdx == ifIdx) |
| 833 | { |
| 834 | vlans.emplace_back(intf->interfaceName()); |
| 835 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | { |
| 839 | auto& ntps = network["NTP"]; |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 840 | for (const auto& ntp : EthernetInterfaceIntf::staticNTPServers()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 841 | { |
| 842 | ntps.emplace_back(ntp); |
| 843 | } |
| 844 | } |
| 845 | { |
| 846 | auto& dnss = network["DNS"]; |
| 847 | for (const auto& dns : EthernetInterfaceIntf::staticNameServers()) |
| 848 | { |
| 849 | dnss.emplace_back(dns); |
| 850 | } |
| 851 | } |
| 852 | { |
| 853 | auto& address = network["Address"]; |
| 854 | for (const auto& addr : getAddresses()) |
| 855 | { |
| 856 | if (originIsManuallyAssigned(addr.second->origin()) && |
| 857 | !dhcpIsEnabled(addr.second->type())) |
| 858 | { |
| 859 | address.emplace_back( |
| 860 | fmt::format("{}/{}", addr.second->address(), |
| 861 | addr.second->prefixLength())); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | { |
| 866 | auto& gateways = network["Gateway"]; |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 867 | if (!dhcp4()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 868 | { |
| 869 | auto gateway = EthernetInterfaceIntf::defaultGateway(); |
| 870 | if (!gateway.empty()) |
| 871 | { |
| 872 | gateways.emplace_back(gateway); |
| 873 | } |
| 874 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 875 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 876 | if (!dhcp6()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 877 | { |
| 878 | auto gateway6 = EthernetInterfaceIntf::defaultGateway6(); |
| 879 | if (!gateway6.empty()) |
| 880 | { |
| 881 | gateways.emplace_back(gateway6); |
| 882 | } |
| 883 | } |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 884 | } |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 885 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 886 | config.map["IPv6AcceptRA"].emplace_back()["DHCPv6Client"].emplace_back( |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 887 | dhcp6() ? "true" : "false"); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 888 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 889 | auto& neighbors = config.map["Neighbor"]; |
| 890 | for (const auto& sneighbor : staticNeighbors) |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 891 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 892 | auto& neighbor = neighbors.emplace_back(); |
| 893 | neighbor["Address"].emplace_back(sneighbor.second->ipAddress()); |
| 894 | neighbor["MACAddress"].emplace_back(sneighbor.second->macAddress()); |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 895 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 896 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 897 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 898 | auto& dhcp = config.map["DHCP"].emplace_back(); |
| 899 | dhcp["ClientIdentifier"].emplace_back("mac"); |
| 900 | if (manager.getDHCPConf()) |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 901 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 902 | const auto& conf = *manager.getDHCPConf(); |
| 903 | auto dns_enabled = conf.dnsEnabled() ? "true" : "false"; |
| 904 | dhcp["UseDNS"].emplace_back(dns_enabled); |
| 905 | dhcp["UseDomains"].emplace_back(dns_enabled); |
| 906 | dhcp["UseNTP"].emplace_back(conf.ntpEnabled() ? "true" : "false"); |
| 907 | dhcp["UseHostname"].emplace_back(conf.hostNameEnabled() ? "true" |
| 908 | : "false"); |
| 909 | dhcp["SendHostname"].emplace_back( |
| 910 | conf.sendHostNameEnabled() ? "true" : "false"); |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 911 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 912 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 913 | auto path = config::pathForIntfConf(manager.getConfDir(), interfaceName()); |
| 914 | config.writeFile(path); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 915 | auto msg = fmt::format("Wrote networkd file: {}", path.native()); |
| 916 | log<level::INFO>(msg.c_str(), entry("FILE=%s", path.c_str())); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 917 | } |
| 918 | |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 919 | std::string EthernetInterface::macAddress([[maybe_unused]] std::string value) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 920 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 921 | if (vlan) |
| 922 | { |
| 923 | log<level::ERR>("Tried to set MAC address on VLAN"); |
| 924 | elog<InternalFailure>(); |
| 925 | } |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 926 | #ifdef PERSIST_MAC |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 927 | ether_addr newMAC; |
| 928 | try |
| 929 | { |
| 930 | newMAC = mac_address::fromString(value); |
| 931 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 932 | catch (const std::invalid_argument&) |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 933 | { |
| 934 | log<level::ERR>("MACAddress is not valid.", |
| 935 | entry("MAC=%s", value.c_str())); |
| 936 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 937 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 938 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 939 | if (!mac_address::isUnicast(newMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 940 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 941 | log<level::ERR>("MACAddress is not valid.", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 942 | entry("MAC=%s", value.c_str())); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 943 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 944 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 945 | } |
| 946 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 947 | auto interface = interfaceName(); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 948 | auto validMAC = std::to_string(newMAC); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 949 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 950 | // We don't need to update the system if the address is unchanged |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 951 | ether_addr oldMAC = mac_address::fromString(MacAddressIntf::macAddress()); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 952 | if (newMAC != oldMAC) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 953 | { |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 954 | // Update everything that depends on the MAC value |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 955 | for (const auto& [_, intf] : manager.interfaces) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 956 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 957 | if (intf->vlan && intf->vlan->parentIdx == ifIdx) |
| 958 | { |
| 959 | intf->MacAddressIntf::macAddress(validMAC); |
| 960 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 961 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 962 | MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 963 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 964 | writeConfigurationFile(); |
William A. Kennington III | 6bfdf3e | 2021-11-09 17:15:12 -0800 | [diff] [blame] | 965 | manager.addReloadPreHook([interface]() { |
| 966 | // The MAC and LLADDRs will only update if the NIC is already down |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 967 | system::setNICUp(interface, false); |
William A. Kennington III | 6bfdf3e | 2021-11-09 17:15:12 -0800 | [diff] [blame] | 968 | }); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 969 | manager.reloadConfigs(); |
Ratan Gupta | 677ae12 | 2017-09-18 16:28:50 +0530 | [diff] [blame] | 970 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 971 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 972 | #ifdef HAVE_UBOOT_ENV |
| 973 | // Ensure that the valid address is stored in the u-boot-env |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 974 | auto envVar = interfaceToUbootEthAddr(interface); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 975 | if (envVar) |
| 976 | { |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 977 | // Trimming MAC addresses that are out of range. eg: AA:FF:FF:FF:FF:100; |
| 978 | // and those having more than 6 bytes. eg: AA:AA:AA:AA:AA:AA:BB |
| 979 | execute("/sbin/fw_setenv", "fw_setenv", envVar->c_str(), |
| 980 | validMAC.c_str()); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 981 | } |
| 982 | #endif // HAVE_UBOOT_ENV |
| 983 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 984 | return value; |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 985 | #else |
| 986 | elog<NotAllowed>( |
| 987 | NotAllowedArgument::REASON("Writing MAC address is not allowed")); |
| 988 | #endif // PERSIST_MAC |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 989 | } |
| 990 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 991 | void EthernetInterface::deleteAll() |
| 992 | { |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 993 | // clear all the ip on the interface |
| 994 | addrs.clear(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 995 | |
| 996 | writeConfigurationFile(); |
| 997 | manager.reloadConfigs(); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 998 | } |
| 999 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1000 | std::string EthernetInterface::defaultGateway(std::string gateway) |
| 1001 | { |
| 1002 | auto gw = EthernetInterfaceIntf::defaultGateway(); |
| 1003 | if (gw == gateway) |
| 1004 | { |
| 1005 | return gw; |
| 1006 | } |
| 1007 | |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 1008 | if (!isValidIP(AF_INET, gateway) && !gateway.empty()) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1009 | { |
| 1010 | log<level::ERR>("Not a valid v4 Gateway", |
| 1011 | entry("GATEWAY=%s", gateway.c_str())); |
| 1012 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1013 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1014 | } |
| 1015 | gw = EthernetInterfaceIntf::defaultGateway(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1016 | |
| 1017 | writeConfigurationFile(); |
| 1018 | manager.reloadConfigs(); |
| 1019 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1020 | return gw; |
| 1021 | } |
| 1022 | |
| 1023 | std::string EthernetInterface::defaultGateway6(std::string gateway) |
| 1024 | { |
| 1025 | auto gw = EthernetInterfaceIntf::defaultGateway6(); |
| 1026 | if (gw == gateway) |
| 1027 | { |
| 1028 | return gw; |
| 1029 | } |
| 1030 | |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 1031 | if (!isValidIP(AF_INET6, gateway) && !gateway.empty()) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1032 | { |
| 1033 | log<level::ERR>("Not a valid v6 Gateway", |
| 1034 | entry("GATEWAY=%s", gateway.c_str())); |
| 1035 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1036 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1037 | } |
| 1038 | gw = EthernetInterfaceIntf::defaultGateway6(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1039 | |
| 1040 | writeConfigurationFile(); |
| 1041 | manager.reloadConfigs(); |
| 1042 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1043 | return gw; |
| 1044 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 1045 | |
| 1046 | EthernetInterface::VlanProperties::VlanProperties( |
| 1047 | sdbusplus::bus_t& bus, stdplus::const_zstring objPath, |
| 1048 | const system::InterfaceInfo& info, EthernetInterface& eth, |
| 1049 | bool emitSignal) : |
| 1050 | VlanIfaces(bus, objPath.c_str(), |
| 1051 | emitSignal ? VlanIfaces::action::defer_emit |
| 1052 | : VlanIfaces::action::emit_no_signals), |
| 1053 | parentIdx(*info.parent_idx), eth(eth) |
| 1054 | { |
| 1055 | VlanIntf::id(*info.vlan_id); |
| 1056 | if (emitSignal) |
| 1057 | { |
| 1058 | this->emit_object_added(); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | void EthernetInterface::VlanProperties::delete_() |
| 1063 | { |
| 1064 | auto intf = eth.interfaceName(); |
| 1065 | |
| 1066 | // Remove all configs for the current interface |
| 1067 | const auto& confDir = eth.manager.getConfDir(); |
| 1068 | std::error_code ec; |
| 1069 | std::filesystem::remove(config::pathForIntfConf(confDir, intf), ec); |
| 1070 | std::filesystem::remove(config::pathForIntfDev(confDir, intf), ec); |
| 1071 | |
| 1072 | // Write an updated parent interface since it has a VLAN entry |
| 1073 | for (const auto& [_, intf] : eth.manager.interfaces) |
| 1074 | { |
| 1075 | if (intf->ifIdx == parentIdx) |
| 1076 | { |
| 1077 | intf->writeConfigurationFile(); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | // We need to forcibly delete the interface as systemd does not |
| 1082 | deleteInterface(intf); |
| 1083 | |
William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 1084 | if (eth.ifIdx > 0) |
| 1085 | { |
| 1086 | eth.manager.interfacesByIdx.erase(eth.ifIdx); |
| 1087 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 1088 | eth.manager.interfaces.erase(intf); |
| 1089 | } |
| 1090 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1091 | } // namespace network |
| 1092 | } // namespace phosphor |