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