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