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