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