Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 1 | #include "config.h" |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame^] | 2 | #include "config_parser.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 3 | #include "network_manager.hpp" |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 4 | #include "network_config.hpp" |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 5 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 6 | |
| 7 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 9 | |
| 10 | #include <algorithm> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 11 | #include <bitset> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 12 | #include <map> |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 13 | #include <fstream> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 14 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 15 | #include <arpa/inet.h> |
| 16 | #include <dirent.h> |
| 17 | #include <net/if.h> |
| 18 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 19 | #include <string> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace network |
| 24 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 25 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 26 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 27 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 28 | |
| 29 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath): |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 30 | details::VLANCreateIface(bus, objPath, true), |
| 31 | bus(bus), |
| 32 | objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 33 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 34 | confDir = NETWORK_CONF_DIR; |
| 35 | } |
| 36 | |
| 37 | void Manager::setConfDir(const fs::path& dir) |
| 38 | { |
| 39 | confDir = dir; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void Manager::createInterfaces() |
| 43 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 44 | //clear all the interfaces first |
| 45 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 46 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 47 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 48 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 49 | for (const auto& intfInfo : interfaceInfoList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 50 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 51 | fs::path objPath = objectPath; |
| 52 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 53 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame^] | 54 | auto dhcp = getDHCPValue(intfInfo.first); |
| 55 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 56 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 57 | intfInfo.first, |
| 58 | std::make_unique< |
| 59 | phosphor::network::EthernetInterface> |
| 60 | (bus, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 61 | objPath.string(), |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame^] | 62 | dhcp, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 63 | *this))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 64 | |
| 65 | interfaces[intfInfo.first]->setAddressList(intfInfo.second); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 69 | void Manager::createChildObjects() |
| 70 | { |
| 71 | // creates the ethernet interface dbus object. |
| 72 | createInterfaces(); |
| 73 | // create the system conf object. |
| 74 | fs::path objPath = objectPath; |
| 75 | objPath /= "config"; |
| 76 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
| 77 | bus, objPath.string(), *this); |
| 78 | |
| 79 | } |
| 80 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 81 | void Manager::vLAN(IntfName interfaceName, uint16_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 82 | { |
| 83 | } |
| 84 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 85 | void Manager::reset() |
| 86 | { |
| 87 | const std::string networkConfig = "/etc/systemd/network/"; |
| 88 | bool filesExist, interfacesMapped = false; |
| 89 | |
| 90 | if(fs::is_directory(networkConfig)) |
| 91 | { |
| 92 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 93 | { |
| 94 | std::string filename = file.path().filename().c_str(); |
| 95 | |
| 96 | if(filename.substr(filename.find_last_of(".") + 1) == "network") |
| 97 | { |
| 98 | fs::remove(file.path()); |
| 99 | filesExist = true; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if(!filesExist) |
| 104 | { |
| 105 | log<level::INFO>("No existing network configuration was found."); |
| 106 | } |
| 107 | |
| 108 | for (auto& intf : interfaces) |
| 109 | { |
| 110 | std::string filename = networkConfig + "00-bmc-" + intf.first + |
| 111 | ".network"; |
| 112 | |
| 113 | bmc::writeDHCPDefault(filename, intf.first); |
| 114 | interfacesMapped = true; |
| 115 | } |
| 116 | |
| 117 | if(interfacesMapped) |
| 118 | { |
| 119 | log<level::INFO>("Network configuration reset to DHCP."); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | log<level::ERR>("No network interfaces are mapped."); |
| 124 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | log<level::ERR>("Network configuration directory not found!"); |
| 130 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 131 | } |
| 132 | |
| 133 | return; |
| 134 | } |
| 135 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 136 | IntfAddrMap Manager::getInterfaceAddrs() const |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 137 | { |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 138 | IntfAddrMap intfMap{}; |
| 139 | AddrList addrList{}; |
| 140 | struct ifaddrs* ifaddr = nullptr; |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 141 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 142 | // attempt to fill struct with ifaddrs |
| 143 | if (getifaddrs(&ifaddr) == -1) |
| 144 | { |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 145 | auto error = errno; |
| 146 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 147 | entry("ERRNO=%s", strerror(error))); |
| 148 | elog<InternalFailure>(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | details::AddrPtr ifaddrPtr(ifaddr); |
| 152 | ifaddr = nullptr; |
| 153 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 154 | std::string intfName{}; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 155 | |
| 156 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 157 | { |
| 158 | // walk interfaces |
| 159 | if (ifa->ifa_addr == nullptr) |
| 160 | { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | // get only INET interfaces not ipv6 |
| 165 | if (ifa->ifa_addr->sa_family == AF_INET || |
| 166 | ifa->ifa_addr->sa_family == AF_INET6) |
| 167 | { |
| 168 | // if loopback, or not running ignore |
| 169 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 170 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 171 | { |
| 172 | continue; |
| 173 | } |
| 174 | // if the interface name is not same as the previous |
| 175 | // iteration then add the addr list into |
| 176 | // the map. |
| 177 | if (intfName != "" && intfName != std::string(ifa->ifa_name)) |
| 178 | { |
| 179 | intfMap.emplace(intfName, addrList); |
| 180 | addrList.clear(); |
| 181 | } |
| 182 | intfName = ifa->ifa_name; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 183 | AddrInfo info{}; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 184 | char ip[INET6_ADDRSTRLEN] = { 0 }; |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 185 | char subnetMask[INET6_ADDRSTRLEN] = { 0 }; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 186 | |
| 187 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 188 | { |
| 189 | |
| 190 | inet_ntop(ifa->ifa_addr->sa_family, |
| 191 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 192 | ip, |
| 193 | sizeof(ip)); |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 194 | |
| 195 | inet_ntop(ifa->ifa_addr->sa_family, |
| 196 | &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr), |
| 197 | subnetMask, |
| 198 | sizeof(subnetMask)); |
| 199 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 200 | } |
| 201 | else |
| 202 | { |
| 203 | inet_ntop(ifa->ifa_addr->sa_family, |
| 204 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 205 | ip, |
| 206 | sizeof(ip)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 207 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 208 | inet_ntop(ifa->ifa_addr->sa_family, |
| 209 | &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), |
| 210 | subnetMask, |
| 211 | sizeof(subnetMask)); |
| 212 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | info.addrType = ifa->ifa_addr->sa_family; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 216 | info.ipaddress = ip; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 217 | info.prefix = toCidr(info.addrType, std::string(subnetMask)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 218 | addrList.emplace_back(info); |
| 219 | } |
| 220 | } |
| 221 | intfMap.emplace(intfName, addrList); |
| 222 | return intfMap; |
| 223 | } |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 224 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 225 | // Need to merge the below function with the code which writes the |
| 226 | // config file during factory reset. |
| 227 | //TODO openbmc/openbmc#1751 |
| 228 | void Manager::writeToConfigurationFile() |
| 229 | { |
| 230 | // write all the static ip address in the systemd-network conf file |
| 231 | |
| 232 | using namespace std::string_literals; |
| 233 | using AddressOrigin = |
| 234 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; |
| 235 | namespace fs = std::experimental::filesystem; |
| 236 | |
| 237 | for (const auto& intf : interfaces) |
| 238 | { |
| 239 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 240 | fs::path confPath = confDir; |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 241 | std::string fileName = "00-bmc-"s + intf.first + ".network"s; |
| 242 | confPath /= fileName; |
| 243 | std::fstream stream; |
| 244 | stream.open(confPath.c_str(), std::fstream::out); |
| 245 | |
| 246 | // Write the device |
| 247 | stream << "[" << "Match" << "]\n"; |
| 248 | stream << "Name=" << intf.first << "\n"; |
| 249 | |
| 250 | auto addrs = intf.second->getAddresses(); |
| 251 | |
| 252 | // write the network section |
| 253 | stream << "[" << "Network" << "]\n"; |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame^] | 254 | // DHCP |
| 255 | if (intf.second->dHCPEnabled() == true) |
| 256 | { |
| 257 | stream << "DHCP=true\n"; |
| 258 | // write the dhcp section |
| 259 | stream << "[DHCP]\n"; |
| 260 | stream << "ClientIdentifier=mac\n"; |
| 261 | stream.close(); |
| 262 | continue; |
| 263 | } |
| 264 | // Static |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 265 | for (const auto& addr : addrs) |
| 266 | { |
| 267 | if (addr.second->origin() == AddressOrigin::Static) |
| 268 | { |
| 269 | std::string address = addr.second->address() + "/" + std::to_string( |
| 270 | addr.second->prefixLength()); |
| 271 | |
| 272 | stream << "Address=" << address << "\n"; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 273 | if (addr.second->gateway() != "0.0.0.0" && |
| 274 | addr.second->gateway() != "") |
| 275 | { |
| 276 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 277 | } |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 278 | |
| 279 | } |
| 280 | } |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 281 | stream << "Gateway=" << systemConf->defaultGateway() << "\n"; |
| 282 | // write the route section |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 283 | stream << "[" << "Route" << "]\n"; |
| 284 | for(const auto& addr : addrs) |
| 285 | { |
| 286 | if (addr.second->origin() == AddressOrigin::Static) |
| 287 | { |
| 288 | int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6; |
| 289 | std::string destination = getNetworkID( |
| 290 | addressFamily, |
| 291 | addr.second->address(), |
| 292 | addr.second->prefixLength()); |
| 293 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 294 | if (addr.second->gateway() != "0.0.0.0" && |
| 295 | addr.second->gateway() != "" && |
| 296 | destination != "0.0.0.0" && |
| 297 | destination != "") |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 298 | { |
| 299 | |
| 300 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 301 | stream << "Destination=" << destination << "\n"; |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | } |
| 306 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 307 | stream.close(); |
| 308 | } |
| 309 | restartSystemdNetworkd(); |
| 310 | } |
| 311 | |
| 312 | void Manager::restartSystemdNetworkd() |
| 313 | { |
| 314 | constexpr auto systemdNetworkdService = "systemd-networkd.service"; |
| 315 | |
| 316 | auto method = bus.new_method_call( |
| 317 | SYSTEMD_BUSNAME, |
| 318 | SYSTEMD_PATH, |
| 319 | SYSTEMD_INTERFACE, |
| 320 | "RestartUnit"); |
| 321 | |
| 322 | method.append(systemdNetworkdService, |
| 323 | "replace"); |
| 324 | |
| 325 | bus.call_noreply(method); |
| 326 | } |
| 327 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame^] | 328 | bool Manager::getDHCPValue(const std::string& intf) |
| 329 | { |
| 330 | bool dhcp = false; |
| 331 | // Get the interface mode value from systemd conf |
| 332 | using namespace std::string_literals; |
| 333 | fs::path confPath = confDir; |
| 334 | std::string fileName = "00-bmc-"s + intf + ".network"s; |
| 335 | confPath /= fileName; |
| 336 | |
| 337 | try |
| 338 | { |
| 339 | config::Parser parser(confPath.string()); |
| 340 | auto values = parser.getValues("Network","DHCP"); |
| 341 | // There will be only single value for DHCP key. |
| 342 | if (values[0] == "true") |
| 343 | { |
| 344 | dhcp = true; |
| 345 | } |
| 346 | } |
| 347 | catch (InternalFailure& e) |
| 348 | { |
| 349 | commit<InternalFailure>(); |
| 350 | } |
| 351 | return dhcp; |
| 352 | } |
| 353 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 354 | }//namespace network |
| 355 | }//namespace phosphor |