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