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> |
| 13 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 14 | #include <arpa/inet.h> |
| 15 | #include <dirent.h> |
| 16 | #include <net/if.h> |
| 17 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 18 | #include <string> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace network |
| 23 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 24 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 25 | using namespace phosphor::logging; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 26 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 27 | |
| 28 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath): |
| 29 | details::VLANCreateIface(bus, objPath, true) |
| 30 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 31 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 32 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 33 | for (const auto& intfInfo : interfaceInfoList) |
| 34 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 35 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 36 | |
| 37 | fs::path objectPath = std::string(OBJ_NETWORK); |
| 38 | objectPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 39 | |
| 40 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 41 | intfInfo.first, |
| 42 | std::make_unique< |
| 43 | phosphor::network::EthernetInterface> |
| 44 | (bus, |
| 45 | objectPath.string(), |
| 46 | false, |
| 47 | intfInfo.second))); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 51 | void Manager::vLAN(IntfName interfaceName, uint16_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 55 | void Manager::reset() |
| 56 | { |
| 57 | const std::string networkConfig = "/etc/systemd/network/"; |
| 58 | bool filesExist, interfacesMapped = false; |
| 59 | |
| 60 | if(fs::is_directory(networkConfig)) |
| 61 | { |
| 62 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 63 | { |
| 64 | std::string filename = file.path().filename().c_str(); |
| 65 | |
| 66 | if(filename.substr(filename.find_last_of(".") + 1) == "network") |
| 67 | { |
| 68 | fs::remove(file.path()); |
| 69 | filesExist = true; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if(!filesExist) |
| 74 | { |
| 75 | log<level::INFO>("No existing network configuration was found."); |
| 76 | } |
| 77 | |
| 78 | for (auto& intf : interfaces) |
| 79 | { |
| 80 | std::string filename = networkConfig + "00-bmc-" + intf.first + |
| 81 | ".network"; |
| 82 | |
| 83 | bmc::writeDHCPDefault(filename, intf.first); |
| 84 | interfacesMapped = true; |
| 85 | } |
| 86 | |
| 87 | if(interfacesMapped) |
| 88 | { |
| 89 | log<level::INFO>("Network configuration reset to DHCP."); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | log<level::ERR>("No network interfaces are mapped."); |
| 94 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 95 | } |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | log<level::ERR>("Network configuration directory not found!"); |
| 100 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 101 | } |
| 102 | |
| 103 | return; |
| 104 | } |
| 105 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 106 | IntfAddrMap Manager::getInterfaceAddrs() const |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 107 | { |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame^] | 108 | IntfAddrMap intfMap{}; |
| 109 | AddrList addrList{}; |
| 110 | struct ifaddrs* ifaddr = nullptr; |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 111 | |
| 112 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 113 | // attempt to fill struct with ifaddrs |
| 114 | if (getifaddrs(&ifaddr) == -1) |
| 115 | { |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 116 | auto error = errno; |
| 117 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 118 | entry("ERRNO=%s", strerror(error))); |
| 119 | elog<InternalFailure>(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | details::AddrPtr ifaddrPtr(ifaddr); |
| 123 | ifaddr = nullptr; |
| 124 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame^] | 125 | std::string intfName{}; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 126 | |
| 127 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 128 | { |
| 129 | // walk interfaces |
| 130 | if (ifa->ifa_addr == nullptr) |
| 131 | { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | // get only INET interfaces not ipv6 |
| 136 | if (ifa->ifa_addr->sa_family == AF_INET || |
| 137 | ifa->ifa_addr->sa_family == AF_INET6) |
| 138 | { |
| 139 | // if loopback, or not running ignore |
| 140 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 141 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 142 | { |
| 143 | continue; |
| 144 | } |
| 145 | // if the interface name is not same as the previous |
| 146 | // iteration then add the addr list into |
| 147 | // the map. |
| 148 | if (intfName != "" && intfName != std::string(ifa->ifa_name)) |
| 149 | { |
| 150 | intfMap.emplace(intfName, addrList); |
| 151 | addrList.clear(); |
| 152 | } |
| 153 | intfName = ifa->ifa_name; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame^] | 154 | AddrInfo info{}; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 155 | char ip[INET6_ADDRSTRLEN] = { 0 }; |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 156 | char subnetMask[INET6_ADDRSTRLEN] = { 0 }; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 157 | |
| 158 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 159 | { |
| 160 | |
| 161 | inet_ntop(ifa->ifa_addr->sa_family, |
| 162 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 163 | ip, |
| 164 | sizeof(ip)); |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 165 | |
| 166 | inet_ntop(ifa->ifa_addr->sa_family, |
| 167 | &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr), |
| 168 | subnetMask, |
| 169 | sizeof(subnetMask)); |
| 170 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 171 | } |
| 172 | else |
| 173 | { |
| 174 | inet_ntop(ifa->ifa_addr->sa_family, |
| 175 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 176 | ip, |
| 177 | sizeof(ip)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 178 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 179 | inet_ntop(ifa->ifa_addr->sa_family, |
| 180 | &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), |
| 181 | subnetMask, |
| 182 | sizeof(subnetMask)); |
| 183 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | info.addrType = ifa->ifa_addr->sa_family; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 187 | info.ipaddress = ip; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame^] | 188 | info.prefix = toCidr(info.addrType, std::string(subnetMask)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 189 | addrList.emplace_back(info); |
| 190 | } |
| 191 | } |
| 192 | intfMap.emplace(intfName, addrList); |
| 193 | return intfMap; |
| 194 | } |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 195 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 196 | }//namespace network |
| 197 | }//namespace phosphor |