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