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}; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 161 | struct ifreq ifr |
| 162 | { |
| 163 | 0 |
| 164 | }; |
| 165 | struct ethtool_cmd edata |
| 166 | { |
| 167 | 0 |
| 168 | }; |
| 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 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 212 | struct ifreq ifr |
| 213 | { |
| 214 | }; |
| 215 | char macAddress[mac_address::size]{}; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 216 | |
| 217 | int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 218 | if (sock < 0) |
| 219 | { |
| 220 | log<level::ERR>("socket creation failed:", |
| 221 | entry("ERROR=%s", strerror(errno))); |
| 222 | return macAddress; |
| 223 | } |
| 224 | |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 225 | strcpy(ifr.ifr_name, interfaceName.c_str()); |
| 226 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 227 | { |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 228 | log<level::ERR>("ioctl failed for SIOCGIFHWADDR:", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 229 | entry("ERROR=%s", strerror(errno))); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 230 | return macAddress; |
| 231 | } |
| 232 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 233 | snprintf(macAddress, mac_address::size, mac_address::format, |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 234 | ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], |
| 235 | ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3], |
| 236 | ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 237 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 238 | return macAddress; |
| 239 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 240 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 241 | std::string EthernetInterface::generateId(const std::string& ipaddress, |
| 242 | uint8_t prefixLength, |
| 243 | const std::string& gateway) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 244 | { |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 245 | std::stringstream hexId; |
| 246 | std::string hashString = ipaddress; |
| 247 | hashString += std::to_string(prefixLength); |
| 248 | hashString += gateway; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 249 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 250 | // Only want 8 hex digits. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 251 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 252 | return hexId.str(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 253 | } |
| 254 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 255 | void EthernetInterface::deleteObject(const std::string& ipaddress) |
| 256 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 257 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 258 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 259 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 260 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 261 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 262 | } |
| 263 | this->addrs.erase(it); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 264 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 267 | void EthernetInterface::deleteVLANFromSystem(const std::string& interface) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 268 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 269 | auto confDir = manager.getConfDir(); |
| 270 | fs::path networkFile = confDir; |
| 271 | networkFile /= systemd::config::networkFilePrefix + interface + |
| 272 | systemd::config::networkFileSuffix; |
| 273 | |
| 274 | fs::path deviceFile = confDir; |
| 275 | deviceFile /= interface + systemd::config::deviceFileSuffix; |
| 276 | |
| 277 | // delete the vlan network file |
| 278 | if (fs::is_regular_file(networkFile)) |
| 279 | { |
| 280 | fs::remove(networkFile); |
| 281 | } |
| 282 | |
| 283 | // delete the vlan device file |
| 284 | if (fs::is_regular_file(deviceFile)) |
| 285 | { |
| 286 | fs::remove(deviceFile); |
| 287 | } |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 288 | |
| 289 | // TODO systemd doesn't delete the virtual network interface |
| 290 | // even after deleting all the related configuartion. |
| 291 | // https://github.com/systemd/systemd/issues/6600 |
| 292 | try |
| 293 | { |
| 294 | deleteInterface(interface); |
| 295 | } |
| 296 | catch (InternalFailure& e) |
| 297 | { |
| 298 | commit<InternalFailure>(); |
| 299 | } |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void EthernetInterface::deleteVLANObject(const std::string& interface) |
| 303 | { |
| 304 | auto it = vlanInterfaces.find(interface); |
| 305 | if (it == vlanInterfaces.end()) |
| 306 | { |
| 307 | log<level::ERR>("DeleteVLANObject:Unable to find the object", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 308 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
| 312 | deleteVLANFromSystem(interface); |
| 313 | // delete the interface |
| 314 | vlanInterfaces.erase(it); |
| 315 | |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 316 | manager.writeToConfigurationFile(); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 317 | } |
| 318 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 319 | std::string EthernetInterface::generateObjectPath( |
| 320 | IP::Protocol addressType, const std::string& ipaddress, |
| 321 | uint8_t prefixLength, const std::string& gateway) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 322 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 323 | std::string type = convertForMessage(addressType); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 324 | type = type.substr(type.rfind('.') + 1); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 325 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); |
| 326 | |
| 327 | std::experimental::filesystem::path objectPath; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 328 | objectPath /= objPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 329 | objectPath /= type; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 330 | objectPath /= generateId(ipaddress, prefixLength, gateway); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 331 | return objectPath.string(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 332 | } |
| 333 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 334 | bool EthernetInterface::dHCPEnabled(bool value) |
| 335 | { |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 336 | if (value == EthernetInterfaceIntf::dHCPEnabled()) |
| 337 | { |
| 338 | return value; |
| 339 | } |
| 340 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 341 | EthernetInterfaceIntf::dHCPEnabled(value); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 342 | manager.writeToConfigurationFile(); |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 343 | return value; |
| 344 | } |
| 345 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 346 | ServerList EthernetInterface::nameservers(ServerList value) |
| 347 | { |
| 348 | try |
| 349 | { |
| 350 | EthernetInterfaceIntf::nameservers(value); |
| 351 | |
| 352 | writeConfigurationFile(); |
| 353 | |
| 354 | // Currently we don't have systemd-resolved enabled |
| 355 | // in the openbmc. Once we update the network conf file, |
| 356 | // it should be read by systemd-resolved.service. |
| 357 | |
| 358 | // The other reason to write the resolv conf is, |
| 359 | // we don't want to restart the networkd for nameserver change. |
| 360 | // as restarting of systemd-networkd takes more then 2 secs |
| 361 | writeDNSEntries(value, resolvConfFile); |
| 362 | } |
| 363 | catch (InternalFailure& e) |
| 364 | { |
| 365 | log<level::ERR>("Exception processing DNS entries"); |
| 366 | } |
| 367 | return EthernetInterfaceIntf::nameservers(); |
| 368 | } |
| 369 | |
| 370 | ServerList EthernetInterface::getNameServerFromConf() |
| 371 | { |
| 372 | fs::path confPath = manager.getConfDir(); |
| 373 | |
| 374 | std::string fileName = systemd::config::networkFilePrefix + |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 375 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 376 | confPath /= fileName; |
| 377 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 378 | config::Parser parser(confPath.string()); |
| 379 | auto rc = config::ReturnCode::SUCCESS; |
| 380 | |
| 381 | std::tie(rc, servers) = parser.getValues("Network", "DNS"); |
| 382 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 383 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 384 | log<level::DEBUG>("Unable to get the value for network[DNS]", |
| 385 | entry("RC=%d", rc)); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 386 | } |
| 387 | return servers; |
| 388 | } |
| 389 | |
| 390 | void EthernetInterface::writeDNSEntries(const ServerList& dnsList, |
| 391 | const std::string& file) |
| 392 | { |
| 393 | std::fstream outStream(file, std::fstream::out); |
| 394 | if (!outStream.is_open()) |
| 395 | { |
| 396 | log<level::ERR>("Unable to open the file", |
| 397 | entry("FILE=%s", file.c_str())); |
| 398 | elog<InternalFailure>(); |
| 399 | } |
| 400 | |
Ratan Gupta | fe11691 | 2017-11-10 16:00:59 +0530 | [diff] [blame] | 401 | outStream << "### Generated manually via dbus settings ###\n"; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 402 | for (const auto& server : dnsList) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 403 | { |
| 404 | outStream << "nameserver " << server << "\n"; |
| 405 | } |
| 406 | } |
| 407 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 408 | void EthernetInterface::loadVLAN(VlanId id) |
| 409 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 410 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 411 | std::string path = objPath; |
| 412 | path += "_" + std::to_string(id); |
| 413 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 414 | auto dhcpEnabled = |
| 415 | getDHCPValue(manager.getConfDir().string(), vlanInterfaceName); |
Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 416 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 417 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 418 | bus, path.c_str(), dhcpEnabled, id, *this, manager); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 419 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 420 | // Fetch the ip address from the system |
| 421 | // and create the dbus object. |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 422 | vlanIntf->createIPAddressObjects(); |
| 423 | |
| 424 | this->vlanInterfaces.emplace(std::move(vlanInterfaceName), |
| 425 | std::move(vlanIntf)); |
| 426 | } |
| 427 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 428 | void EthernetInterface::createVLAN(VlanId id) |
| 429 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 430 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 431 | std::string path = objPath; |
| 432 | path += "_" + std::to_string(id); |
| 433 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 434 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 435 | bus, path.c_str(), false, id, *this, manager); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 436 | |
| 437 | // write the device file for the vlan interface. |
| 438 | vlanIntf->writeDeviceFile(); |
| 439 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 440 | this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf)); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 441 | // write the new vlan device entry to the configuration(network) file. |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 442 | manager.writeToConfigurationFile(); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 443 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 444 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 445 | ServerList EthernetInterface::getNTPServersFromConf() |
| 446 | { |
| 447 | fs::path confPath = manager.getConfDir(); |
| 448 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 449 | std::string fileName = systemd::config::networkFilePrefix + |
| 450 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 451 | confPath /= fileName; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 452 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 453 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 454 | config::Parser parser(confPath.string()); |
| 455 | auto rc = config::ReturnCode::SUCCESS; |
| 456 | |
| 457 | std::tie(rc, servers) = parser.getValues("Network", "NTP"); |
| 458 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 459 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 460 | log<level::DEBUG>("Unable to get the value for Network[NTP]", |
| 461 | entry("rc=%d", rc)); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 462 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 463 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 464 | return servers; |
| 465 | } |
| 466 | |
| 467 | ServerList EthernetInterface::nTPServers(ServerList servers) |
| 468 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 469 | auto ntpServers = EthernetInterfaceIntf::nTPServers(servers); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 470 | |
| 471 | writeConfigurationFile(); |
| 472 | // timesynchd reads the NTP server configuration from the |
| 473 | // network file. |
Lei YU | 250011e | 2018-02-01 14:07:06 +0800 | [diff] [blame] | 474 | restartSystemdUnit(networkdService); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 475 | return ntpServers; |
| 476 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 477 | // Need to merge the below function with the code which writes the |
| 478 | // config file during factory reset. |
| 479 | // TODO openbmc/openbmc#1751 |
| 480 | |
| 481 | void EthernetInterface::writeConfigurationFile() |
| 482 | { |
| 483 | // write all the static ip address in the systemd-network conf file |
| 484 | |
| 485 | using namespace std::string_literals; |
| 486 | using AddressOrigin = |
| 487 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; |
| 488 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 489 | |
| 490 | // if there is vlan interafce then write the configuration file |
| 491 | // for vlan also. |
| 492 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 493 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 494 | { |
| 495 | intf.second->writeConfigurationFile(); |
| 496 | } |
| 497 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 498 | fs::path confPath = manager.getConfDir(); |
| 499 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 500 | std::string fileName = systemd::config::networkFilePrefix + |
| 501 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 502 | confPath /= fileName; |
| 503 | std::fstream stream; |
| 504 | |
| 505 | stream.open(confPath.c_str(), std::fstream::out); |
| 506 | if (!stream.is_open()) |
| 507 | { |
| 508 | log<level::ERR>("Unable to open the file", |
| 509 | entry("FILE=%s", confPath.c_str())); |
| 510 | elog<InternalFailure>(); |
| 511 | } |
| 512 | |
| 513 | // Write the device |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 514 | stream << "[" |
| 515 | << "Match" |
| 516 | << "]\n"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 517 | stream << "Name=" << interfaceName() << "\n"; |
| 518 | |
| 519 | auto addrs = getAddresses(); |
| 520 | |
| 521 | // write the network section |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 522 | stream << "[" |
| 523 | << "Network" |
| 524 | << "]\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 525 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
Nagaraju Goruganti | 24afe36 | 2017-09-21 07:40:26 -0500 | [diff] [blame] | 526 | stream << "LinkLocalAddressing=yes\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 527 | #else |
| 528 | stream << "LinkLocalAddressing=no\n"; |
| 529 | #endif |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 530 | stream << "IPv6AcceptRA=false\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 531 | |
| 532 | // Add the VLAN entry |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 533 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 534 | { |
| 535 | stream << "VLAN=" << intf.second->EthernetInterface::interfaceName() |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 536 | << "\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 537 | } |
Nagaraju Goruganti | e8b83ec | 2018-03-26 05:21:45 -0500 | [diff] [blame] | 538 | // Add the DHCP entry |
| 539 | auto value = dHCPEnabled() ? "true"s : "false"s; |
| 540 | stream << "DHCP="s + value + "\n"; |
| 541 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 542 | // When the interface configured as dhcp, we don't need below given entries |
| 543 | // in config file. |
| 544 | if (dHCPEnabled() == false) |
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 NTP server |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 547 | for (const auto& ntp : EthernetInterfaceIntf::nTPServers()) |
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 << "NTP=" << ntp << "\n"; |
| 550 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 551 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 552 | // Add the DNS entry |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 553 | for (const auto& dns : EthernetInterfaceIntf::nameservers()) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 554 | { |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 555 | stream << "DNS=" << dns << "\n"; |
| 556 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 557 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 558 | // Static |
| 559 | for (const auto& addr : addrs) |
| 560 | { |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 561 | if (addr.second->origin() == AddressOrigin::Static |
| 562 | #ifndef LINK_LOCAL_AUTOCONFIGURATION |
| 563 | || addr.second->origin() == AddressOrigin::LinkLocal |
| 564 | #endif |
| 565 | ) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 566 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 567 | std::string address = |
| 568 | addr.second->address() + "/" + |
| 569 | std::to_string(addr.second->prefixLength()); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 570 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 571 | stream << "Address=" << address << "\n"; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (manager.getSystemConf()) |
| 576 | { |
| 577 | stream << "Gateway=" << manager.getSystemConf()->defaultGateway() |
| 578 | << "\n"; |
| 579 | } |
| 580 | |
| 581 | // write the route section |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 582 | for (const auto& addr : addrs) |
| 583 | { |
| 584 | if (addr.second->origin() == AddressOrigin::Static) |
| 585 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 586 | int addressFamily = addr.second->type() == IP::Protocol::IPv4 |
| 587 | ? AF_INET |
| 588 | : AF_INET6; |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 589 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 590 | std::string destination = |
| 591 | getNetworkID(addressFamily, addr.second->address(), |
| 592 | addr.second->prefixLength()); |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 593 | |
| 594 | if (addr.second->gateway() != "0.0.0.0" && |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 595 | addr.second->gateway() != "" && destination != "0.0.0.0" && |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 596 | destination != "") |
| 597 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 598 | stream << "[" |
| 599 | << "Route" |
| 600 | << "]\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 | { |
Lei YU | 250011e | 2018-02-01 14:07:06 +0800 | [diff] [blame] | 718 | 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 |