| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 1 | #include "config.h" | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 2 | #include "ethernet_interface.hpp" | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 3 | #include "ipaddress.hpp" | 
| Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" | 
| Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 5 | #include "routing_table.hpp" | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 6 | #include "vlan_interface.hpp" | 
|  | 7 | #include "xyz/openbmc_project/Common/error.hpp" | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 8 |  | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 10 | #include <phosphor-logging/log.hpp> | 
|  | 11 |  | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 12 | #include <arpa/inet.h> | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 13 | #include <linux/ethtool.h> | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 14 | #include <linux/sockios.h> | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 15 | #include <net/if.h> | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 16 | #include <netinet/in.h> | 
|  | 17 | #include <sys/ioctl.h> | 
|  | 18 | #include <sys/socket.h> | 
|  | 19 | #include <unistd.h> | 
|  | 20 |  | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 21 |  | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 22 | #include <algorithm> | 
|  | 23 | #include <experimental/filesystem> | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 24 | #include <fstream> | 
|  | 25 | #include <sstream> | 
|  | 26 | #include <string> | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 27 |  | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 28 | namespace phosphor | 
|  | 29 | { | 
|  | 30 | namespace network | 
|  | 31 | { | 
|  | 32 |  | 
|  | 33 | using namespace phosphor::logging; | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 34 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; | 
|  | 35 |  | 
| Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame^] | 36 | constexpr auto MAC_ADDRESS_FORMAT = "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"; | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 37 | constexpr size_t SIZE_MAC = 18; | 
|  | 38 | constexpr size_t SIZE_BUFF = 512; | 
|  | 39 |  | 
|  | 40 | EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus, | 
|  | 41 | const std::string& objPath, | 
| Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 42 | bool dhcpEnabled, | 
| Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 43 | Manager& parent, | 
|  | 44 | bool emitSignal) : | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 45 | Ifaces(bus, objPath.c_str(), true), | 
| Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 46 | bus(bus), | 
| Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 47 | manager(parent), | 
|  | 48 | objPath(objPath) | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 49 | { | 
|  | 50 | auto intfName = objPath.substr(objPath.rfind("/") + 1); | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 51 | std::replace(intfName.begin(), intfName.end(), '_', '.'); | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 52 | interfaceName(intfName); | 
|  | 53 | dHCPEnabled(dhcpEnabled); | 
| Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame^] | 54 | mACAddress(getMACAddress(intfName)); | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 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 | } | 
|  | 84 | else if (isLinkLocal(addr.ipaddress)) | 
|  | 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 | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 106 | } | 
| Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 107 |  | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 110 | void EthernetInterface::iP(IP::Protocol protType, | 
|  | 111 | std::string ipaddress, | 
|  | 112 | uint8_t prefixLength, | 
|  | 113 | std::string gateway) | 
|  | 114 | { | 
| Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 115 |  | 
|  | 116 | if (dHCPEnabled()) | 
|  | 117 | { | 
| Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 118 | log<level::INFO>("DHCP enabled on the interface"), | 
|  | 119 | entry("INTERFACE=%s",interfaceName()); | 
| Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 120 | return; | 
|  | 121 | } | 
|  | 122 |  | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 123 | IP::AddressOrigin origin = IP::AddressOrigin::Static; | 
|  | 124 |  | 
| Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 125 | std::string objectPath = generateObjectPath(protType, | 
|  | 126 | ipaddress, | 
|  | 127 | prefixLength, | 
|  | 128 | gateway); | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 129 | this->addrs.emplace( | 
| Ratan Gupta | e578d56 | 2017-08-02 07:04:16 +0530 | [diff] [blame] | 130 | std::move(ipaddress), | 
|  | 131 | std::make_shared<phosphor::network::IPAddress>( | 
|  | 132 | bus, | 
|  | 133 | objectPath.c_str(), | 
|  | 134 | *this, | 
|  | 135 | protType, | 
|  | 136 | ipaddress, | 
|  | 137 | origin, | 
|  | 138 | prefixLength, | 
|  | 139 | gateway)); | 
| Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 140 |  | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 141 | writeConfigurationFile(); | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
|  | 144 |  | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 145 | /* | 
|  | 146 | Note: We don't have support for  ethtool now | 
|  | 147 | will enable this code once we bring the ethtool | 
|  | 148 | in the image. | 
|  | 149 | TODO: https://github.com/openbmc/openbmc/issues/1484 | 
|  | 150 | */ | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 151 |  | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 152 | InterfaceInfo EthernetInterface::getInterfaceInfo() const | 
|  | 153 | { | 
|  | 154 | int sock{-1}; | 
|  | 155 | struct ifreq ifr{0}; | 
|  | 156 | struct ethtool_cmd edata{0}; | 
|  | 157 | LinkSpeed speed {0}; | 
|  | 158 | Autoneg autoneg {0}; | 
|  | 159 | DuplexMode duplex {0}; | 
|  | 160 | do | 
|  | 161 | { | 
|  | 162 | sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); | 
|  | 163 | if (sock < 0) | 
|  | 164 | { | 
|  | 165 | log<level::ERR>("socket creation  failed:", | 
|  | 166 | entry("ERROR=%s", strerror(errno))); | 
|  | 167 | break; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | strncpy(ifr.ifr_name, interfaceName().c_str(), sizeof(ifr.ifr_name)); | 
|  | 171 | ifr.ifr_data = reinterpret_cast<char*>(&edata); | 
|  | 172 |  | 
|  | 173 | edata.cmd = ETHTOOL_GSET; | 
|  | 174 |  | 
|  | 175 | if (ioctl(sock, SIOCETHTOOL, &ifr) < 0) | 
|  | 176 | { | 
|  | 177 | log<level::ERR>("ioctl failed for SIOCETHTOOL:", | 
|  | 178 | entry("ERROR=%s", strerror(errno))); | 
|  | 179 | break; | 
|  | 180 |  | 
|  | 181 | } | 
|  | 182 | speed = edata.speed; | 
|  | 183 | duplex = edata.duplex; | 
|  | 184 | autoneg = edata.autoneg; | 
|  | 185 | } | 
|  | 186 | while (0); | 
|  | 187 |  | 
|  | 188 | if (sock) | 
|  | 189 | { | 
|  | 190 | close(sock); | 
|  | 191 | } | 
|  | 192 | return std::make_tuple(speed, duplex, autoneg); | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | /** @brief get the mac address of the interface. | 
|  | 196 | *  @return macaddress on success | 
|  | 197 | */ | 
|  | 198 |  | 
| Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame^] | 199 | std::string EthernetInterface::getMACAddress( | 
|  | 200 | const std::string& interfaceName) const | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 201 | { | 
| Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame^] | 202 | struct ifreq ifr{}; | 
|  | 203 | char macAddress[SIZE_MAC] {}; | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 204 |  | 
|  | 205 | int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); | 
|  | 206 | if (sock < 0) | 
|  | 207 | { | 
|  | 208 | log<level::ERR>("socket creation  failed:", | 
|  | 209 | entry("ERROR=%s", strerror(errno))); | 
|  | 210 | return macAddress; | 
|  | 211 | } | 
|  | 212 |  | 
| Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame^] | 213 | strcpy(ifr.ifr_name, interfaceName.c_str()); | 
|  | 214 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 215 | { | 
| Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame^] | 216 | log<level::ERR>("ioctl failed for SIOCGIFHWADDR:", | 
|  | 217 | entry("ERROR=%s", strerror(errno))); | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 218 | return macAddress; | 
|  | 219 | } | 
|  | 220 |  | 
| Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame^] | 221 | snprintf(macAddress, SIZE_MAC, MAC_ADDRESS_FORMAT, | 
|  | 222 | ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], | 
|  | 223 | ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3], | 
|  | 224 | ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 225 |  | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 226 | return macAddress; | 
|  | 227 | } | 
| Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 228 |  | 
| Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 229 | std::string EthernetInterface::generateId(const std::string& ipaddress, | 
|  | 230 | uint8_t prefixLength, | 
|  | 231 | const std::string& gateway) | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 232 | { | 
| Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 233 | std::stringstream hexId; | 
|  | 234 | std::string hashString = ipaddress; | 
|  | 235 | hashString += std::to_string(prefixLength); | 
|  | 236 | hashString += gateway; | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 237 |  | 
| Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 238 | // Only want 8 hex digits. | 
|  | 239 | hexId << std::hex << ((std::hash<std::string> {}( | 
| Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 240 | hashString)) & 0xFFFFFFFF); | 
| Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 241 | return hexId.str(); | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
| Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 244 | void EthernetInterface::deleteObject(const std::string& ipaddress) | 
|  | 245 | { | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 246 | auto it = addrs.find(ipaddress); | 
| Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 247 | if (it == addrs.end()) | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 248 | { | 
| Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 249 | log<level::ERR>("DeleteObject:Unable to find the object."); | 
|  | 250 | return; | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 251 | } | 
|  | 252 | this->addrs.erase(it); | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 253 | writeConfigurationFile(); | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 254 | } | 
|  | 255 |  | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 256 | void EthernetInterface::deleteVLANObject(const std::string& interface) | 
|  | 257 | { | 
|  | 258 | using namespace std::string_literals; | 
|  | 259 |  | 
|  | 260 | auto it = vlanInterfaces.find(interface); | 
|  | 261 | if (it == vlanInterfaces.end()) | 
|  | 262 | { | 
|  | 263 | log<level::ERR>("DeleteVLANObject:Unable to find the object", | 
|  | 264 | entry("INTERFACE=%s",interface.c_str())); | 
|  | 265 | return; | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | auto confDir = manager.getConfDir(); | 
|  | 269 | fs::path networkFile = confDir; | 
|  | 270 | networkFile /= systemd::config::networkFilePrefix + interface + | 
|  | 271 | systemd::config::networkFileSuffix; | 
|  | 272 |  | 
|  | 273 | fs::path deviceFile = confDir; | 
|  | 274 | deviceFile /= interface + systemd::config::deviceFileSuffix; | 
|  | 275 |  | 
|  | 276 | // delete the vlan network file | 
|  | 277 | if (fs::is_regular_file(networkFile)) | 
|  | 278 | { | 
|  | 279 | fs::remove(networkFile); | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | // delete the vlan device file | 
|  | 283 | if (fs::is_regular_file(deviceFile)) | 
|  | 284 | { | 
|  | 285 | fs::remove(deviceFile); | 
|  | 286 | } | 
|  | 287 | // delete the interface | 
|  | 288 | vlanInterfaces.erase(it); | 
|  | 289 | // restart the systemd-networkd | 
|  | 290 |  | 
|  | 291 | restartSystemdUnit("systemd-networkd.service"); | 
|  | 292 |  | 
|  | 293 | // TODO  systemd doesn't delete the virtual network interface | 
|  | 294 | // even after deleting all the related configuartion. | 
|  | 295 | // https://github.com/systemd/systemd/issues/6600 | 
|  | 296 | try | 
|  | 297 | { | 
|  | 298 | deleteInterface(interface); | 
|  | 299 | } | 
|  | 300 | catch (InternalFailure& e) | 
|  | 301 | { | 
|  | 302 | commit<InternalFailure>(); | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 |  | 
| Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 306 | std::string EthernetInterface::generateObjectPath(IP::Protocol addressType, | 
|  | 307 | const std::string& ipaddress, | 
|  | 308 | uint8_t prefixLength, | 
|  | 309 | const std::string& gateway) const | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 310 | { | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 311 | std::string type = convertForMessage(addressType); | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 312 | type = type.substr(type.rfind('.') + 1); | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 313 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); | 
|  | 314 |  | 
|  | 315 | std::experimental::filesystem::path objectPath; | 
| Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 316 | objectPath /= objPath; | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 317 | objectPath /= type; | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 318 | objectPath /= generateId(ipaddress, prefixLength, gateway); | 
| Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 319 | return objectPath.string(); | 
| Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 322 | bool EthernetInterface::dHCPEnabled(bool value) | 
|  | 323 | { | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 324 | if (value == EthernetInterfaceIntf::dHCPEnabled()) | 
|  | 325 | { | 
|  | 326 | return value; | 
|  | 327 | } | 
|  | 328 |  | 
| Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 329 | EthernetInterfaceIntf::dHCPEnabled(value); | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 330 | if (value) | 
| Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 331 | { | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 332 | writeConfigurationFile(); | 
| Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 333 | createIPAddressObjects(); | 
|  | 334 | } | 
| Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 335 |  | 
| Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 336 | return value; | 
|  | 337 | } | 
|  | 338 |  | 
| Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 339 | void EthernetInterface::loadVLAN(VlanId id) | 
|  | 340 | { | 
|  | 341 | std::string vlanInterfaceName = interfaceName() + "." + | 
|  | 342 | std::to_string(id); | 
|  | 343 | std::string path = objPath; | 
|  | 344 | path += "_" + std::to_string(id); | 
|  | 345 |  | 
| Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 346 | auto dhcpEnabled = getDHCPValue(manager.getConfDir().string(), | 
|  | 347 | vlanInterfaceName); | 
|  | 348 |  | 
| Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 349 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( | 
|  | 350 | bus, | 
|  | 351 | path.c_str(), | 
| Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 352 | dhcpEnabled, | 
| Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 353 | id, | 
|  | 354 | *this, | 
|  | 355 | manager); | 
|  | 356 |  | 
|  | 357 | // Fetch the ip address from the system | 
|  | 358 | // and create the dbus object. | 
|  | 359 | vlanIntf->createIPAddressObjects(); | 
|  | 360 |  | 
|  | 361 | this->vlanInterfaces.emplace(std::move(vlanInterfaceName), | 
|  | 362 | std::move(vlanIntf)); | 
|  | 363 | } | 
|  | 364 |  | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 365 | void EthernetInterface::createVLAN(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 |  | 
|  | 372 |  | 
|  | 373 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( | 
|  | 374 | bus, | 
|  | 375 | path.c_str(), | 
|  | 376 | EthernetInterfaceIntf::dHCPEnabled(), | 
|  | 377 | id, | 
|  | 378 | *this, | 
|  | 379 | manager); | 
|  | 380 |  | 
|  | 381 | // write the device file for the vlan interface. | 
|  | 382 | vlanIntf->writeDeviceFile(); | 
|  | 383 |  | 
| Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 384 | this->vlanInterfaces.emplace(vlanInterfaceName, | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 385 | std::move(vlanIntf)); | 
|  | 386 | // write the new vlan device entry to the configuration(network) file. | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 387 | writeConfigurationFile(); | 
| Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 388 |  | 
|  | 389 | // Create the dbus object for the link local ipv6 address. | 
|  | 390 | vlanInterfaces[vlanInterfaceName]->createIPAddressObjects(); | 
|  | 391 |  | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 392 | } | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 393 |  | 
|  | 394 | // Need to merge the below function with the code which writes the | 
|  | 395 | // config file during factory reset. | 
|  | 396 | // TODO openbmc/openbmc#1751 | 
|  | 397 |  | 
|  | 398 | void EthernetInterface::writeConfigurationFile() | 
|  | 399 | { | 
|  | 400 | // write all the static ip address in the systemd-network conf file | 
|  | 401 |  | 
|  | 402 | using namespace std::string_literals; | 
|  | 403 | using AddressOrigin = | 
|  | 404 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; | 
|  | 405 | namespace fs = std::experimental::filesystem; | 
|  | 406 | fs::path confPath = manager.getConfDir(); | 
|  | 407 |  | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 408 | std::string fileName = systemd::config::networkFilePrefix + interfaceName() + | 
|  | 409 | systemd::config::networkFileSuffix; | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 410 | confPath /= fileName; | 
|  | 411 | std::fstream stream; | 
|  | 412 |  | 
|  | 413 | stream.open(confPath.c_str(), std::fstream::out); | 
|  | 414 | if (!stream.is_open()) | 
|  | 415 | { | 
|  | 416 | log<level::ERR>("Unable to open the file", | 
|  | 417 | entry("FILE=%s", confPath.c_str())); | 
|  | 418 | elog<InternalFailure>(); | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | // Write the device | 
|  | 422 | stream << "[" << "Match" << "]\n"; | 
|  | 423 | stream << "Name=" << interfaceName() << "\n"; | 
|  | 424 |  | 
|  | 425 | auto addrs = getAddresses(); | 
|  | 426 |  | 
|  | 427 | // write the network section | 
|  | 428 | stream << "[" << "Network" << "]\n"; | 
|  | 429 | // DHCP | 
|  | 430 | if (dHCPEnabled() == true) | 
|  | 431 | { | 
|  | 432 | // write the dhcp section if interface is | 
|  | 433 | // configured as dhcp. | 
|  | 434 | writeDHCPSection(stream); | 
|  | 435 | stream.close(); | 
|  | 436 | return; | 
|  | 437 | } | 
|  | 438 | // Add the Vlan entry | 
|  | 439 | for(const auto& intf: vlanInterfaces) | 
|  | 440 | { | 
|  | 441 | stream << "VLAN=" << intf.second->EthernetInterface::interfaceName() << "\n"; | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | // Static | 
|  | 445 | for (const auto& addr : addrs) | 
|  | 446 | { | 
|  | 447 | if (addr.second->origin() == AddressOrigin::Static) | 
|  | 448 | { | 
|  | 449 | std::string address = addr.second->address() + "/" + std::to_string( | 
|  | 450 | addr.second->prefixLength()); | 
|  | 451 |  | 
|  | 452 | stream << "Address=" << address << "\n"; | 
|  | 453 | } | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | if (manager.getSystemConf()) | 
|  | 457 | { | 
|  | 458 | stream << "Gateway=" << manager.getSystemConf()->defaultGateway() | 
|  | 459 | << "\n"; | 
|  | 460 | } | 
|  | 461 | // write the route section | 
|  | 462 | stream << "[" << "Route" << "]\n"; | 
|  | 463 | for(const auto& addr : addrs) | 
|  | 464 | { | 
|  | 465 | if (addr.second->origin() == AddressOrigin::Static) | 
|  | 466 | { | 
|  | 467 | int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6; | 
|  | 468 | std::string destination = getNetworkID( | 
|  | 469 | addressFamily, | 
|  | 470 | addr.second->address(), | 
|  | 471 | addr.second->prefixLength()); | 
|  | 472 |  | 
|  | 473 | if (addr.second->gateway() != "0.0.0.0" && | 
|  | 474 | addr.second->gateway() != "" && | 
|  | 475 | destination != "0.0.0.0" && | 
|  | 476 | destination != "") | 
|  | 477 | { | 
|  | 478 | stream << "Gateway=" << addr.second->gateway() << "\n"; | 
|  | 479 | stream << "Destination=" << destination << "\n"; | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | } | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | stream.close(); | 
|  | 486 | restartSystemdUnit("systemd-networkd.service"); | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | void EthernetInterface::writeDHCPSection(std::fstream& stream) | 
|  | 490 | { | 
|  | 491 | using namespace std::string_literals; | 
|  | 492 | stream << "DHCP=true\n"; | 
|  | 493 | // write the dhcp section | 
|  | 494 | stream << "[DHCP]\n"; | 
|  | 495 |  | 
|  | 496 | // Hardcoding the client identifier to mac, to address below issue | 
|  | 497 | // https://github.com/openbmc/openbmc/issues/1280 | 
|  | 498 | stream << "ClientIdentifier=mac\n"; | 
|  | 499 | if (manager.getDHCPConf()) | 
|  | 500 | { | 
|  | 501 | auto value = manager.getDHCPConf()->dNSEnabled() ? "true"s : "false"s; | 
|  | 502 | stream << "UseDNS="s + value + "\n"; | 
|  | 503 |  | 
|  | 504 | value = manager.getDHCPConf()->nTPEnabled() ? "true"s : "false"s; | 
|  | 505 | stream << "UseNTP="s + value + "\n"; | 
|  | 506 |  | 
|  | 507 | value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s; | 
|  | 508 | stream << "UseHostname="s + value + "\n"; | 
|  | 509 | } | 
|  | 510 | } | 
|  | 511 |  | 
| Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 512 | }//namespace network | 
|  | 513 | }//namespace phosphor |