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" |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 7 | #include "network_manager.hpp" |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [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> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 13 | #include <linux/sockios.h> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 14 | #include <net/if.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 15 | #include <netinet/in.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <unistd.h> |
| 19 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <experimental/filesystem> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 22 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 23 | #include <phosphor-logging/elog-errors.hpp> |
| 24 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 25 | #include <sstream> |
| 26 | #include <string> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 27 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 28 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 29 | namespace phosphor |
| 30 | { |
| 31 | namespace network |
| 32 | { |
| 33 | |
| 34 | using namespace phosphor::logging; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 35 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 36 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 37 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 38 | EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus, |
| 39 | const std::string& objPath, |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 40 | bool dhcpEnabled, Manager& parent, |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 41 | bool emitSignal) : |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 42 | Ifaces(bus, objPath.c_str(), true), |
| 43 | bus(bus), manager(parent), objPath(objPath) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 44 | { |
| 45 | auto intfName = objPath.substr(objPath.rfind("/") + 1); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 46 | std::replace(intfName.begin(), intfName.end(), '_', '.'); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 47 | interfaceName(intfName); |
Ratan Gupta | c35481d | 2017-08-18 06:12:26 +0530 | [diff] [blame] | 48 | EthernetInterfaceIntf::dHCPEnabled(dhcpEnabled); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 49 | MacAddressIntf::mACAddress(getMACAddress(intfName)); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 50 | EthernetInterfaceIntf::nTPServers(getNTPServersFromConf()); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 51 | EthernetInterfaceIntf::nameservers(getNameServerFromConf()); |
| 52 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 53 | // Emit deferred signal. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 54 | if (emitSignal) |
| 55 | { |
| 56 | this->emit_object_added(); |
| 57 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 58 | } |
| 59 | |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 60 | static IP::Protocol convertFamily(int family) |
| 61 | { |
| 62 | switch (family) |
| 63 | { |
| 64 | case AF_INET: |
| 65 | return IP::Protocol::IPv4; |
| 66 | case AF_INET6: |
| 67 | return IP::Protocol::IPv6; |
| 68 | } |
| 69 | |
| 70 | throw std::invalid_argument("Bad address family"); |
| 71 | } |
| 72 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 73 | void EthernetInterface::createIPAddressObjects() |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 74 | { |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 75 | addrs.clear(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 76 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 77 | auto addrs = getInterfaceAddrs()[interfaceName()]; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 78 | |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 79 | route::Table routingTable; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 80 | |
Ratan Gupta | 6a387c1 | 2017-08-03 13:26:19 +0530 | [diff] [blame] | 81 | for (auto& addr : addrs) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 82 | { |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 83 | IP::Protocol addressType = convertFamily(addr.addrType); |
| 84 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 85 | if (dHCPEnabled()) |
| 86 | { |
| 87 | origin = IP::AddressOrigin::DHCP; |
| 88 | } |
William A. Kennington III | 1689380 | 2019-01-30 16:01:01 -0800 | [diff] [blame] | 89 | if (isLinkLocalIP(addr.ipaddress)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 90 | { |
| 91 | origin = IP::AddressOrigin::LinkLocal; |
| 92 | } |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 93 | std::string gateway = |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 94 | routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 95 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 96 | std::string ipAddressObjectPath = generateObjectPath( |
| 97 | addressType, addr.ipaddress, addr.prefix, gateway); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 98 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 99 | this->addrs.emplace(addr.ipaddress, |
| 100 | std::make_shared<phosphor::network::IPAddress>( |
| 101 | bus, ipAddressObjectPath.c_str(), *this, |
| 102 | addressType, addr.ipaddress, origin, |
| 103 | addr.prefix, gateway)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 104 | } |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 105 | } |
| 106 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 107 | void EthernetInterface::iP(IP::Protocol protType, std::string ipaddress, |
| 108 | uint8_t prefixLength, std::string gateway) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 109 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 110 | |
| 111 | if (dHCPEnabled()) |
| 112 | { |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 113 | log<level::INFO>("DHCP enabled on the interface"), |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 114 | entry("INTERFACE=%s", interfaceName().c_str()); |
| 115 | dHCPEnabled(false); |
| 116 | } |
| 117 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 118 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
| 119 | |
| 120 | int addressFamily = (protType == IP::Protocol::IPv4) ? AF_INET : AF_INET6; |
| 121 | |
| 122 | if (!isValidIP(addressFamily, ipaddress)) |
| 123 | { |
| 124 | log<level::ERR>("Not a valid IP address"), |
| 125 | entry("ADDRESS=%s", ipaddress.c_str()); |
| 126 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"), |
| 127 | Argument::ARGUMENT_VALUE(ipaddress.c_str())); |
| 128 | } |
| 129 | |
| 130 | if (!gateway.empty() && (!isValidIP(addressFamily, gateway))) |
| 131 | { |
| 132 | log<level::ERR>("Not a valid Gateway"), |
| 133 | entry("GATEWAY=%s", gateway.c_str()); |
| 134 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("gateway"), |
| 135 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 136 | } |
| 137 | |
| 138 | if (!isValidPrefix(addressFamily, prefixLength)) |
| 139 | { |
| 140 | log<level::ERR>("PrefixLength is not correct "), |
| 141 | entry("PREFIXLENGTH=%d", gateway.c_str()); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 142 | elog<InvalidArgument>( |
| 143 | Argument::ARGUMENT_NAME("prefixLength"), |
| 144 | Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str())); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 145 | } |
| 146 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 147 | std::string objectPath = |
| 148 | generateObjectPath(protType, ipaddress, prefixLength, gateway); |
| 149 | this->addrs.emplace(ipaddress, |
| 150 | std::make_shared<phosphor::network::IPAddress>( |
| 151 | bus, objectPath.c_str(), *this, protType, ipaddress, |
| 152 | origin, prefixLength, gateway)); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 153 | |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 154 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 157 | /* |
| 158 | Note: We don't have support for ethtool now |
| 159 | will enable this code once we bring the ethtool |
| 160 | in the image. |
| 161 | TODO: https://github.com/openbmc/openbmc/issues/1484 |
| 162 | */ |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 163 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 164 | InterfaceInfo EthernetInterface::getInterfaceInfo() const |
| 165 | { |
| 166 | int sock{-1}; |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 167 | ifreq ifr{0}; |
| 168 | ethtool_cmd edata{0}; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 169 | LinkSpeed speed{0}; |
| 170 | Autoneg autoneg{0}; |
| 171 | DuplexMode duplex{0}; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 172 | do |
| 173 | { |
| 174 | sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 175 | if (sock < 0) |
| 176 | { |
| 177 | log<level::ERR>("socket creation failed:", |
| 178 | entry("ERROR=%s", strerror(errno))); |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | strncpy(ifr.ifr_name, interfaceName().c_str(), sizeof(ifr.ifr_name)); |
| 183 | ifr.ifr_data = reinterpret_cast<char*>(&edata); |
| 184 | |
| 185 | edata.cmd = ETHTOOL_GSET; |
| 186 | |
| 187 | if (ioctl(sock, SIOCETHTOOL, &ifr) < 0) |
| 188 | { |
| 189 | log<level::ERR>("ioctl failed for SIOCETHTOOL:", |
| 190 | entry("ERROR=%s", strerror(errno))); |
| 191 | break; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 192 | } |
| 193 | speed = edata.speed; |
| 194 | duplex = edata.duplex; |
| 195 | autoneg = edata.autoneg; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 196 | } while (0); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 197 | |
| 198 | if (sock) |
| 199 | { |
| 200 | close(sock); |
| 201 | } |
| 202 | return std::make_tuple(speed, duplex, autoneg); |
| 203 | } |
| 204 | |
| 205 | /** @brief get the mac address of the interface. |
| 206 | * @return macaddress on success |
| 207 | */ |
| 208 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 209 | std::string |
| 210 | EthernetInterface::getMACAddress(const std::string& interfaceName) const |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 211 | { |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 212 | ifreq ifr{}; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 213 | char macAddress[mac_address::size]{}; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 214 | |
| 215 | int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 216 | if (sock < 0) |
| 217 | { |
| 218 | log<level::ERR>("socket creation failed:", |
| 219 | entry("ERROR=%s", strerror(errno))); |
| 220 | return macAddress; |
| 221 | } |
| 222 | |
Patrick Venture | 836c91d | 2018-09-11 17:36:03 -0700 | [diff] [blame] | 223 | std::strcpy(ifr.ifr_name, interfaceName.c_str()); |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 224 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 225 | { |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 226 | log<level::ERR>("ioctl failed for SIOCGIFHWADDR:", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 227 | entry("ERROR=%s", strerror(errno))); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 228 | return macAddress; |
| 229 | } |
| 230 | |
Patrick Venture | 836c91d | 2018-09-11 17:36:03 -0700 | [diff] [blame] | 231 | std::snprintf(macAddress, mac_address::size, mac_address::format, |
| 232 | ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], |
| 233 | ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3], |
| 234 | ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 235 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 236 | return macAddress; |
| 237 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 238 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 239 | std::string EthernetInterface::generateId(const std::string& ipaddress, |
| 240 | uint8_t prefixLength, |
| 241 | const std::string& gateway) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 242 | { |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 243 | std::stringstream hexId; |
| 244 | std::string hashString = ipaddress; |
| 245 | hashString += std::to_string(prefixLength); |
| 246 | hashString += gateway; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 247 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 248 | // Only want 8 hex digits. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 249 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 250 | return hexId.str(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 251 | } |
| 252 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 253 | void EthernetInterface::deleteObject(const std::string& ipaddress) |
| 254 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 255 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 256 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 257 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 258 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 259 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 260 | } |
| 261 | this->addrs.erase(it); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 262 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 263 | } |
| 264 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 265 | void EthernetInterface::deleteVLANFromSystem(const std::string& interface) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 266 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 267 | auto confDir = manager.getConfDir(); |
| 268 | fs::path networkFile = confDir; |
| 269 | networkFile /= systemd::config::networkFilePrefix + interface + |
| 270 | systemd::config::networkFileSuffix; |
| 271 | |
| 272 | fs::path deviceFile = confDir; |
| 273 | deviceFile /= interface + systemd::config::deviceFileSuffix; |
| 274 | |
| 275 | // delete the vlan network file |
| 276 | if (fs::is_regular_file(networkFile)) |
| 277 | { |
| 278 | fs::remove(networkFile); |
| 279 | } |
| 280 | |
| 281 | // delete the vlan device file |
| 282 | if (fs::is_regular_file(deviceFile)) |
| 283 | { |
| 284 | fs::remove(deviceFile); |
| 285 | } |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 286 | |
| 287 | // TODO systemd doesn't delete the virtual network interface |
| 288 | // even after deleting all the related configuartion. |
| 289 | // https://github.com/systemd/systemd/issues/6600 |
| 290 | try |
| 291 | { |
| 292 | deleteInterface(interface); |
| 293 | } |
| 294 | catch (InternalFailure& e) |
| 295 | { |
| 296 | commit<InternalFailure>(); |
| 297 | } |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void EthernetInterface::deleteVLANObject(const std::string& interface) |
| 301 | { |
| 302 | auto it = vlanInterfaces.find(interface); |
| 303 | if (it == vlanInterfaces.end()) |
| 304 | { |
| 305 | log<level::ERR>("DeleteVLANObject:Unable to find the object", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 306 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 307 | return; |
| 308 | } |
| 309 | |
| 310 | deleteVLANFromSystem(interface); |
| 311 | // delete the interface |
| 312 | vlanInterfaces.erase(it); |
| 313 | |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 314 | manager.writeToConfigurationFile(); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 315 | } |
| 316 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 317 | std::string EthernetInterface::generateObjectPath( |
| 318 | IP::Protocol addressType, const std::string& ipaddress, |
| 319 | uint8_t prefixLength, const std::string& gateway) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 320 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 321 | std::string type = convertForMessage(addressType); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 322 | type = type.substr(type.rfind('.') + 1); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 323 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); |
| 324 | |
| 325 | std::experimental::filesystem::path objectPath; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 326 | objectPath /= objPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 327 | objectPath /= type; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 328 | objectPath /= generateId(ipaddress, prefixLength, gateway); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 329 | return objectPath.string(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 330 | } |
| 331 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 332 | bool EthernetInterface::dHCPEnabled(bool value) |
| 333 | { |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 334 | if (value == EthernetInterfaceIntf::dHCPEnabled()) |
| 335 | { |
| 336 | return value; |
| 337 | } |
| 338 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 339 | EthernetInterfaceIntf::dHCPEnabled(value); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 340 | manager.writeToConfigurationFile(); |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 341 | return value; |
| 342 | } |
| 343 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 344 | ServerList EthernetInterface::nameservers(ServerList value) |
| 345 | { |
| 346 | try |
| 347 | { |
| 348 | EthernetInterfaceIntf::nameservers(value); |
| 349 | |
| 350 | writeConfigurationFile(); |
| 351 | |
| 352 | // Currently we don't have systemd-resolved enabled |
| 353 | // in the openbmc. Once we update the network conf file, |
| 354 | // it should be read by systemd-resolved.service. |
| 355 | |
| 356 | // The other reason to write the resolv conf is, |
| 357 | // we don't want to restart the networkd for nameserver change. |
| 358 | // as restarting of systemd-networkd takes more then 2 secs |
| 359 | writeDNSEntries(value, resolvConfFile); |
| 360 | } |
| 361 | catch (InternalFailure& e) |
| 362 | { |
| 363 | log<level::ERR>("Exception processing DNS entries"); |
| 364 | } |
| 365 | return EthernetInterfaceIntf::nameservers(); |
| 366 | } |
| 367 | |
| 368 | ServerList EthernetInterface::getNameServerFromConf() |
| 369 | { |
| 370 | fs::path confPath = manager.getConfDir(); |
| 371 | |
| 372 | std::string fileName = systemd::config::networkFilePrefix + |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 373 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 374 | confPath /= fileName; |
| 375 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 376 | config::Parser parser(confPath.string()); |
| 377 | auto rc = config::ReturnCode::SUCCESS; |
| 378 | |
| 379 | std::tie(rc, servers) = parser.getValues("Network", "DNS"); |
| 380 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 381 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 382 | log<level::DEBUG>("Unable to get the value for network[DNS]", |
| 383 | entry("RC=%d", rc)); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 384 | } |
| 385 | return servers; |
| 386 | } |
| 387 | |
| 388 | void EthernetInterface::writeDNSEntries(const ServerList& dnsList, |
| 389 | const std::string& file) |
| 390 | { |
| 391 | std::fstream outStream(file, std::fstream::out); |
| 392 | if (!outStream.is_open()) |
| 393 | { |
| 394 | log<level::ERR>("Unable to open the file", |
| 395 | entry("FILE=%s", file.c_str())); |
| 396 | elog<InternalFailure>(); |
| 397 | } |
| 398 | |
Ratan Gupta | fe11691 | 2017-11-10 16:00:59 +0530 | [diff] [blame] | 399 | outStream << "### Generated manually via dbus settings ###\n"; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 400 | for (const auto& server : dnsList) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 401 | { |
| 402 | outStream << "nameserver " << server << "\n"; |
| 403 | } |
| 404 | } |
| 405 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 406 | void EthernetInterface::loadVLAN(VlanId id) |
| 407 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 408 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 409 | std::string path = objPath; |
| 410 | path += "_" + std::to_string(id); |
| 411 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 412 | auto dhcpEnabled = |
| 413 | getDHCPValue(manager.getConfDir().string(), vlanInterfaceName); |
Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 414 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 415 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 416 | bus, path.c_str(), dhcpEnabled, id, *this, manager); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 417 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 418 | // Fetch the ip address from the system |
| 419 | // and create the dbus object. |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 420 | vlanIntf->createIPAddressObjects(); |
| 421 | |
| 422 | this->vlanInterfaces.emplace(std::move(vlanInterfaceName), |
| 423 | std::move(vlanIntf)); |
| 424 | } |
| 425 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 426 | void EthernetInterface::createVLAN(VlanId id) |
| 427 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 428 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 429 | std::string path = objPath; |
| 430 | path += "_" + std::to_string(id); |
| 431 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 432 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 433 | bus, path.c_str(), false, id, *this, manager); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 434 | |
| 435 | // write the device file for the vlan interface. |
| 436 | vlanIntf->writeDeviceFile(); |
| 437 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 438 | this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf)); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 439 | // write the new vlan device entry to the configuration(network) file. |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 440 | manager.writeToConfigurationFile(); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 441 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 442 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 443 | ServerList EthernetInterface::getNTPServersFromConf() |
| 444 | { |
| 445 | fs::path confPath = manager.getConfDir(); |
| 446 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 447 | std::string fileName = systemd::config::networkFilePrefix + |
| 448 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 449 | confPath /= fileName; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 450 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 451 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 452 | config::Parser parser(confPath.string()); |
| 453 | auto rc = config::ReturnCode::SUCCESS; |
| 454 | |
| 455 | std::tie(rc, servers) = parser.getValues("Network", "NTP"); |
| 456 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 457 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 458 | log<level::DEBUG>("Unable to get the value for Network[NTP]", |
| 459 | entry("rc=%d", rc)); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 460 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 461 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 462 | return servers; |
| 463 | } |
| 464 | |
| 465 | ServerList EthernetInterface::nTPServers(ServerList servers) |
| 466 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 467 | auto ntpServers = EthernetInterfaceIntf::nTPServers(servers); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 468 | |
| 469 | writeConfigurationFile(); |
| 470 | // timesynchd reads the NTP server configuration from the |
| 471 | // network file. |
Ratan Gupta | 895f9e5 | 2018-11-26 20:57:34 +0530 | [diff] [blame] | 472 | manager.restartSystemdUnit(networkdService); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 473 | return ntpServers; |
| 474 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 475 | // Need to merge the below function with the code which writes the |
| 476 | // config file during factory reset. |
| 477 | // TODO openbmc/openbmc#1751 |
| 478 | |
| 479 | void EthernetInterface::writeConfigurationFile() |
| 480 | { |
| 481 | // write all the static ip address in the systemd-network conf file |
| 482 | |
| 483 | using namespace std::string_literals; |
| 484 | using AddressOrigin = |
| 485 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; |
| 486 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 487 | |
| 488 | // if there is vlan interafce then write the configuration file |
| 489 | // for vlan also. |
| 490 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 491 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 492 | { |
| 493 | intf.second->writeConfigurationFile(); |
| 494 | } |
| 495 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 496 | fs::path confPath = manager.getConfDir(); |
| 497 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 498 | std::string fileName = systemd::config::networkFilePrefix + |
| 499 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 500 | confPath /= fileName; |
| 501 | std::fstream stream; |
| 502 | |
| 503 | stream.open(confPath.c_str(), std::fstream::out); |
| 504 | if (!stream.is_open()) |
| 505 | { |
| 506 | log<level::ERR>("Unable to open the file", |
| 507 | entry("FILE=%s", confPath.c_str())); |
| 508 | elog<InternalFailure>(); |
| 509 | } |
| 510 | |
| 511 | // Write the device |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 512 | stream << "[Match]\n"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 513 | stream << "Name=" << interfaceName() << "\n"; |
| 514 | |
| 515 | auto addrs = getAddresses(); |
| 516 | |
| 517 | // write the network section |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 518 | stream << "[Network]\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 519 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
Nagaraju Goruganti | 24afe36 | 2017-09-21 07:40:26 -0500 | [diff] [blame] | 520 | stream << "LinkLocalAddressing=yes\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 521 | #else |
| 522 | stream << "LinkLocalAddressing=no\n"; |
| 523 | #endif |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 524 | stream << "IPv6AcceptRA=false\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 525 | |
| 526 | // Add the VLAN entry |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 527 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 528 | { |
| 529 | stream << "VLAN=" << intf.second->EthernetInterface::interfaceName() |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 530 | << "\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 531 | } |
Nagaraju Goruganti | e8b83ec | 2018-03-26 05:21:45 -0500 | [diff] [blame] | 532 | // Add the DHCP entry |
| 533 | auto value = dHCPEnabled() ? "true"s : "false"s; |
| 534 | stream << "DHCP="s + value + "\n"; |
| 535 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 536 | // When the interface configured as dhcp, we don't need below given entries |
| 537 | // in config file. |
| 538 | if (dHCPEnabled() == false) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 539 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 540 | // Add the NTP server |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 541 | for (const auto& ntp : EthernetInterfaceIntf::nTPServers()) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 542 | { |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 543 | stream << "NTP=" << ntp << "\n"; |
| 544 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 545 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 546 | // Add the DNS entry |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 547 | for (const auto& dns : EthernetInterfaceIntf::nameservers()) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 548 | { |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 549 | stream << "DNS=" << dns << "\n"; |
| 550 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 551 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 552 | // Static |
| 553 | for (const auto& addr : addrs) |
| 554 | { |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 555 | if (addr.second->origin() == AddressOrigin::Static |
| 556 | #ifndef LINK_LOCAL_AUTOCONFIGURATION |
| 557 | || addr.second->origin() == AddressOrigin::LinkLocal |
| 558 | #endif |
| 559 | ) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 560 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 561 | std::string address = |
| 562 | addr.second->address() + "/" + |
| 563 | std::to_string(addr.second->prefixLength()); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 564 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 565 | stream << "Address=" << address << "\n"; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (manager.getSystemConf()) |
| 570 | { |
William A. Kennington III | 781f335 | 2019-02-01 21:07:10 -0800 | [diff] [blame] | 571 | const auto& gateway = manager.getSystemConf()->defaultGateway(); |
| 572 | if (!gateway.empty()) |
| 573 | { |
| 574 | stream << "Gateway=" << gateway << "\n"; |
| 575 | } |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 576 | const auto& gateway6 = manager.getSystemConf()->defaultGateway6(); |
| 577 | if (!gateway6.empty()) |
| 578 | { |
| 579 | stream << "Gateway=" << gateway6 << "\n"; |
| 580 | } |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // write the route section |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 584 | for (const auto& addr : addrs) |
| 585 | { |
| 586 | if (addr.second->origin() == AddressOrigin::Static) |
| 587 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 588 | int addressFamily = addr.second->type() == IP::Protocol::IPv4 |
| 589 | ? AF_INET |
| 590 | : AF_INET6; |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 591 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 592 | std::string destination = |
| 593 | getNetworkID(addressFamily, addr.second->address(), |
| 594 | addr.second->prefixLength()); |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 595 | |
| 596 | if (addr.second->gateway() != "0.0.0.0" && |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 597 | addr.second->gateway() != "" && destination != "0.0.0.0" && |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 598 | destination != "") |
| 599 | { |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 600 | stream << "[Route]\n"; |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 601 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 602 | stream << "Destination=" << destination << "\n"; |
| 603 | } |
| 604 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 608 | // Write the dhcp section irrespective of whether DHCP is enabled or not |
| 609 | writeDHCPSection(stream); |
| 610 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 611 | stream.close(); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void EthernetInterface::writeDHCPSection(std::fstream& stream) |
| 615 | { |
| 616 | using namespace std::string_literals; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 617 | // write the dhcp section |
| 618 | stream << "[DHCP]\n"; |
| 619 | |
| 620 | // Hardcoding the client identifier to mac, to address below issue |
| 621 | // https://github.com/openbmc/openbmc/issues/1280 |
| 622 | stream << "ClientIdentifier=mac\n"; |
| 623 | if (manager.getDHCPConf()) |
| 624 | { |
| 625 | auto value = manager.getDHCPConf()->dNSEnabled() ? "true"s : "false"s; |
| 626 | stream << "UseDNS="s + value + "\n"; |
| 627 | |
| 628 | value = manager.getDHCPConf()->nTPEnabled() ? "true"s : "false"s; |
| 629 | stream << "UseNTP="s + value + "\n"; |
| 630 | |
| 631 | value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s; |
| 632 | stream << "UseHostname="s + value + "\n"; |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 633 | |
| 634 | value = |
| 635 | manager.getDHCPConf()->sendHostNameEnabled() ? "true"s : "false"s; |
| 636 | stream << "SendHostname="s + value + "\n"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 640 | std::string EthernetInterface::mACAddress(std::string value) |
| 641 | { |
| 642 | if (!mac_address::validate(value)) |
| 643 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 644 | log<level::ERR>("MACAddress is not valid.", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 645 | entry("MAC=%s", value.c_str())); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 646 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 647 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // check whether MAC is broadcast mac. |
| 651 | auto intMac = mac_address::internal::convertToInt(value); |
| 652 | |
| 653 | if (!(intMac ^ mac_address::broadcastMac)) |
| 654 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 655 | log<level::ERR>("MACAddress is a broadcast mac.", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 656 | entry("MAC=%s", value.c_str())); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 657 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 658 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 659 | } |
| 660 | |
Patrick Venture | d475cd6 | 2018-02-26 17:07:41 -0800 | [diff] [blame] | 661 | // Check if the MAC changed. |
| 662 | auto pmac = MacAddressIntf::mACAddress(); |
| 663 | if (strcasecmp(pmac.c_str(), value.c_str()) == 0) |
| 664 | { |
| 665 | return MacAddressIntf::mACAddress(); |
| 666 | } |
| 667 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 668 | // Allow the mac to be set if one of the condition is true. |
| 669 | // 1) Incoming Mac is of local admin type. |
| 670 | // or |
| 671 | // 2) Incoming mac is same as eeprom Mac. |
| 672 | |
| 673 | if (!(intMac & mac_address::localAdminMask)) |
| 674 | { |
| 675 | try |
| 676 | { |
| 677 | auto inventoryMac = mac_address::getfromInventory(bus); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 678 | auto intInventoryMac = |
| 679 | mac_address::internal::convertToInt(inventoryMac); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 680 | |
| 681 | if (intInventoryMac != intMac) |
| 682 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 683 | log<level::ERR>("Given MAC address is neither a local Admin " |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 684 | "type nor is same as in inventory"); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 685 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 686 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 687 | } |
| 688 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 689 | catch (InternalFailure& e) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 690 | { |
Patrick Venture | e0ad43a | 2017-11-29 18:19:54 -0800 | [diff] [blame] | 691 | log<level::ERR>("Exception occurred during getting of MAC " |
| 692 | "address from Inventory"); |
Gunnar Mills | ce26282 | 2018-06-19 16:21:34 -0500 | [diff] [blame] | 693 | elog<InternalFailure>(); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | auto interface = interfaceName(); |
| 697 | execute("/sbin/fw_setenv", "fw_setenv", "ethaddr", value.c_str()); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 698 | // TODO: would replace below three calls |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 699 | // with restarting of systemd-netwokd |
| 700 | // through https://github.com/systemd/systemd/issues/6696 |
| 701 | execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "down"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 702 | execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), |
| 703 | "address", value.c_str()); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 704 | |
| 705 | execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "up"); |
| 706 | |
| 707 | auto mac = MacAddressIntf::mACAddress(std::move(value)); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 708 | // update all the vlan interfaces |
| 709 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 710 | { |
| 711 | intf.second->updateMacAddress(); |
| 712 | } |
Ratan Gupta | 677ae12 | 2017-09-18 16:28:50 +0530 | [diff] [blame] | 713 | |
| 714 | // restart the systemd networkd so that dhcp client gets the |
| 715 | // ip for the changed mac address. |
| 716 | if (dHCPEnabled()) |
| 717 | { |
Ratan Gupta | 895f9e5 | 2018-11-26 20:57:34 +0530 | [diff] [blame] | 718 | manager.restartSystemdUnit(networkdService); |
Ratan Gupta | 677ae12 | 2017-09-18 16:28:50 +0530 | [diff] [blame] | 719 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 720 | return mac; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 721 | } |
| 722 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 723 | void EthernetInterface::deleteAll() |
| 724 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 725 | if (EthernetInterfaceIntf::dHCPEnabled()) |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 726 | { |
| 727 | log<level::INFO>("DHCP enabled on the interface"), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 728 | entry("INTERFACE=%s", interfaceName().c_str()); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | // clear all the ip on the interface |
| 732 | addrs.clear(); |
| 733 | manager.writeToConfigurationFile(); |
| 734 | } |
| 735 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 736 | } // namespace network |
| 737 | } // namespace phosphor |