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