Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "network_manager.hpp" |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 3 | #include "network_config.hpp" |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 5 | |
| 6 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 8 | |
| 9 | #include <algorithm> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 10 | #include <bitset> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 11 | #include <experimental/filesystem> |
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 | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 27 | namespace fs = std::experimental::filesystem; |
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 | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void Manager::createInterfaces() |
| 37 | { |
| 38 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 39 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 40 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 41 | for (const auto& intfInfo : interfaceInfoList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 42 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 43 | fs::path objPath = objectPath; |
| 44 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 45 | |
| 46 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 47 | intfInfo.first, |
| 48 | std::make_unique< |
| 49 | phosphor::network::EthernetInterface> |
| 50 | (bus, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 51 | objPath.string(), |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 52 | false, |
| 53 | *this))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 54 | |
| 55 | interfaces[intfInfo.first]->setAddressList(intfInfo.second); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 59 | void Manager::vLAN(IntfName interfaceName, uint16_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 63 | void Manager::reset() |
| 64 | { |
| 65 | const std::string networkConfig = "/etc/systemd/network/"; |
| 66 | bool filesExist, interfacesMapped = false; |
| 67 | |
| 68 | if(fs::is_directory(networkConfig)) |
| 69 | { |
| 70 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 71 | { |
| 72 | std::string filename = file.path().filename().c_str(); |
| 73 | |
| 74 | if(filename.substr(filename.find_last_of(".") + 1) == "network") |
| 75 | { |
| 76 | fs::remove(file.path()); |
| 77 | filesExist = true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if(!filesExist) |
| 82 | { |
| 83 | log<level::INFO>("No existing network configuration was found."); |
| 84 | } |
| 85 | |
| 86 | for (auto& intf : interfaces) |
| 87 | { |
| 88 | std::string filename = networkConfig + "00-bmc-" + intf.first + |
| 89 | ".network"; |
| 90 | |
| 91 | bmc::writeDHCPDefault(filename, intf.first); |
| 92 | interfacesMapped = true; |
| 93 | } |
| 94 | |
| 95 | if(interfacesMapped) |
| 96 | { |
| 97 | log<level::INFO>("Network configuration reset to DHCP."); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | log<level::ERR>("No network interfaces are mapped."); |
| 102 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 103 | } |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | log<level::ERR>("Network configuration directory not found!"); |
| 108 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 109 | } |
| 110 | |
| 111 | return; |
| 112 | } |
| 113 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 114 | IntfAddrMap Manager::getInterfaceAddrs() const |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 115 | { |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 116 | IntfAddrMap intfMap{}; |
| 117 | AddrList addrList{}; |
| 118 | struct ifaddrs* ifaddr = nullptr; |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 119 | |
| 120 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 121 | // attempt to fill struct with ifaddrs |
| 122 | if (getifaddrs(&ifaddr) == -1) |
| 123 | { |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 124 | auto error = errno; |
| 125 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 126 | entry("ERRNO=%s", strerror(error))); |
| 127 | elog<InternalFailure>(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | details::AddrPtr ifaddrPtr(ifaddr); |
| 131 | ifaddr = nullptr; |
| 132 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 133 | std::string intfName{}; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 134 | |
| 135 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 136 | { |
| 137 | // walk interfaces |
| 138 | if (ifa->ifa_addr == nullptr) |
| 139 | { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | // get only INET interfaces not ipv6 |
| 144 | if (ifa->ifa_addr->sa_family == AF_INET || |
| 145 | ifa->ifa_addr->sa_family == AF_INET6) |
| 146 | { |
| 147 | // if loopback, or not running ignore |
| 148 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 149 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 150 | { |
| 151 | continue; |
| 152 | } |
| 153 | // if the interface name is not same as the previous |
| 154 | // iteration then add the addr list into |
| 155 | // the map. |
| 156 | if (intfName != "" && intfName != std::string(ifa->ifa_name)) |
| 157 | { |
| 158 | intfMap.emplace(intfName, addrList); |
| 159 | addrList.clear(); |
| 160 | } |
| 161 | intfName = ifa->ifa_name; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 162 | AddrInfo info{}; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 163 | char ip[INET6_ADDRSTRLEN] = { 0 }; |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 164 | char subnetMask[INET6_ADDRSTRLEN] = { 0 }; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 165 | |
| 166 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 167 | { |
| 168 | |
| 169 | inet_ntop(ifa->ifa_addr->sa_family, |
| 170 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 171 | ip, |
| 172 | sizeof(ip)); |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 173 | |
| 174 | inet_ntop(ifa->ifa_addr->sa_family, |
| 175 | &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr), |
| 176 | subnetMask, |
| 177 | sizeof(subnetMask)); |
| 178 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 179 | } |
| 180 | else |
| 181 | { |
| 182 | inet_ntop(ifa->ifa_addr->sa_family, |
| 183 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 184 | ip, |
| 185 | sizeof(ip)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 186 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 187 | inet_ntop(ifa->ifa_addr->sa_family, |
| 188 | &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), |
| 189 | subnetMask, |
| 190 | sizeof(subnetMask)); |
| 191 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | info.addrType = ifa->ifa_addr->sa_family; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 195 | info.ipaddress = ip; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 196 | info.prefix = toCidr(info.addrType, std::string(subnetMask)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 197 | addrList.emplace_back(info); |
| 198 | } |
| 199 | } |
| 200 | intfMap.emplace(intfName, addrList); |
| 201 | return intfMap; |
| 202 | } |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 203 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 204 | // Need to merge the below function with the code which writes the |
| 205 | // config file during factory reset. |
| 206 | //TODO openbmc/openbmc#1751 |
| 207 | void Manager::writeToConfigurationFile() |
| 208 | { |
| 209 | // write all the static ip address in the systemd-network conf file |
| 210 | |
| 211 | using namespace std::string_literals; |
| 212 | using AddressOrigin = |
| 213 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; |
| 214 | namespace fs = std::experimental::filesystem; |
| 215 | |
| 216 | for (const auto& intf : interfaces) |
| 217 | { |
| 218 | |
| 219 | fs::path confPath {NETWORK_CONF_DIR}; |
| 220 | std::string fileName = "00-bmc-"s + intf.first + ".network"s; |
| 221 | confPath /= fileName; |
| 222 | std::fstream stream; |
| 223 | stream.open(confPath.c_str(), std::fstream::out); |
| 224 | |
| 225 | // Write the device |
| 226 | stream << "[" << "Match" << "]\n"; |
| 227 | stream << "Name=" << intf.first << "\n"; |
| 228 | |
| 229 | auto addrs = intf.second->getAddresses(); |
| 230 | |
| 231 | // write the network section |
| 232 | stream << "[" << "Network" << "]\n"; |
| 233 | for (const auto& addr : addrs) |
| 234 | { |
| 235 | if (addr.second->origin() == AddressOrigin::Static) |
| 236 | { |
| 237 | std::string address = addr.second->address() + "/" + std::to_string( |
| 238 | addr.second->prefixLength()); |
| 239 | |
| 240 | stream << "Address=" << address << "\n"; |
| 241 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 242 | |
| 243 | } |
| 244 | } |
| 245 | stream.close(); |
| 246 | } |
| 247 | restartSystemdNetworkd(); |
| 248 | } |
| 249 | |
| 250 | void Manager::restartSystemdNetworkd() |
| 251 | { |
| 252 | constexpr auto systemdNetworkdService = "systemd-networkd.service"; |
| 253 | |
| 254 | auto method = bus.new_method_call( |
| 255 | SYSTEMD_BUSNAME, |
| 256 | SYSTEMD_PATH, |
| 257 | SYSTEMD_INTERFACE, |
| 258 | "RestartUnit"); |
| 259 | |
| 260 | method.append(systemdNetworkdService, |
| 261 | "replace"); |
| 262 | |
| 263 | bus.call_noreply(method); |
| 264 | } |
| 265 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 266 | }//namespace network |
| 267 | }//namespace phosphor |