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