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