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