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