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