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 | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 3 | #include "util.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 5 | #include "network_config.hpp" |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 10 | |
| 11 | #include <algorithm> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 12 | #include <bitset> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 13 | #include <map> |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 14 | #include <fstream> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 15 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 16 | #include <arpa/inet.h> |
| 17 | #include <dirent.h> |
| 18 | #include <net/if.h> |
| 19 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 20 | #include <string> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace network |
| 25 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 26 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 29 | |
| 30 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath): |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 31 | details::VLANCreateIface(bus, objPath, true), |
| 32 | bus(bus), |
| 33 | objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 34 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 35 | confDir = NETWORK_CONF_DIR; |
| 36 | } |
| 37 | |
| 38 | void Manager::setConfDir(const fs::path& dir) |
| 39 | { |
| 40 | confDir = dir; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void Manager::createInterfaces() |
| 44 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 45 | //clear all the interfaces first |
| 46 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 47 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 48 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 49 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 50 | for (const auto& intfInfo : interfaceInfoList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 51 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 52 | fs::path objPath = objectPath; |
| 53 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 54 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 55 | auto dhcp = getDHCPValue(intfInfo.first); |
| 56 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 57 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 58 | intfInfo.first, |
| 59 | std::make_unique< |
| 60 | phosphor::network::EthernetInterface> |
| 61 | (bus, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 62 | objPath.string(), |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 63 | dhcp, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 64 | *this))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 65 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 66 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 67 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 68 | } |
| 69 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 70 | void Manager::createChildObjects() |
| 71 | { |
| 72 | // creates the ethernet interface dbus object. |
| 73 | createInterfaces(); |
| 74 | // create the system conf object. |
| 75 | fs::path objPath = objectPath; |
| 76 | objPath /= "config"; |
| 77 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
| 78 | bus, objPath.string(), *this); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 79 | // create the dhcp conf object. |
| 80 | objPath /= "dhcp"; |
| 81 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
| 82 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 83 | |
| 84 | } |
| 85 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 86 | void Manager::vLAN(IntfName interfaceName, uint16_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 87 | { |
| 88 | } |
| 89 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 90 | void Manager::reset() |
| 91 | { |
| 92 | const std::string networkConfig = "/etc/systemd/network/"; |
| 93 | bool filesExist, interfacesMapped = false; |
| 94 | |
| 95 | if(fs::is_directory(networkConfig)) |
| 96 | { |
| 97 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 98 | { |
| 99 | std::string filename = file.path().filename().c_str(); |
| 100 | |
| 101 | if(filename.substr(filename.find_last_of(".") + 1) == "network") |
| 102 | { |
| 103 | fs::remove(file.path()); |
| 104 | filesExist = true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if(!filesExist) |
| 109 | { |
| 110 | log<level::INFO>("No existing network configuration was found."); |
| 111 | } |
| 112 | |
| 113 | for (auto& intf : interfaces) |
| 114 | { |
| 115 | std::string filename = networkConfig + "00-bmc-" + intf.first + |
| 116 | ".network"; |
| 117 | |
| 118 | bmc::writeDHCPDefault(filename, intf.first); |
| 119 | interfacesMapped = true; |
| 120 | } |
| 121 | |
| 122 | if(interfacesMapped) |
| 123 | { |
| 124 | log<level::INFO>("Network configuration reset to DHCP."); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | log<level::ERR>("No network interfaces are mapped."); |
| 129 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | log<level::ERR>("Network configuration directory not found!"); |
| 135 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 136 | } |
| 137 | |
| 138 | return; |
| 139 | } |
| 140 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 141 | // Need to merge the below function with the code which writes the |
| 142 | // config file during factory reset. |
| 143 | //TODO openbmc/openbmc#1751 |
| 144 | void Manager::writeToConfigurationFile() |
| 145 | { |
| 146 | // write all the static ip address in the systemd-network conf file |
| 147 | |
| 148 | using namespace std::string_literals; |
| 149 | using AddressOrigin = |
| 150 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; |
| 151 | namespace fs = std::experimental::filesystem; |
| 152 | |
| 153 | for (const auto& intf : interfaces) |
| 154 | { |
| 155 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 156 | fs::path confPath = confDir; |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 157 | std::string fileName = "00-bmc-"s + intf.first + ".network"s; |
| 158 | confPath /= fileName; |
| 159 | std::fstream stream; |
| 160 | stream.open(confPath.c_str(), std::fstream::out); |
| 161 | |
| 162 | // Write the device |
| 163 | stream << "[" << "Match" << "]\n"; |
| 164 | stream << "Name=" << intf.first << "\n"; |
| 165 | |
| 166 | auto addrs = intf.second->getAddresses(); |
| 167 | |
| 168 | // write the network section |
| 169 | stream << "[" << "Network" << "]\n"; |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 170 | // DHCP |
| 171 | if (intf.second->dHCPEnabled() == true) |
| 172 | { |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame^] | 173 | // write the dhcp section if interface is |
| 174 | // configured as dhcp. |
| 175 | writeDHCPSection(stream); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 176 | stream.close(); |
| 177 | continue; |
| 178 | } |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame^] | 179 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 180 | // Static |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 181 | for (const auto& addr : addrs) |
| 182 | { |
| 183 | if (addr.second->origin() == AddressOrigin::Static) |
| 184 | { |
| 185 | std::string address = addr.second->address() + "/" + std::to_string( |
| 186 | addr.second->prefixLength()); |
| 187 | |
| 188 | stream << "Address=" << address << "\n"; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 189 | if (addr.second->gateway() != "0.0.0.0" && |
| 190 | addr.second->gateway() != "") |
| 191 | { |
| 192 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 193 | } |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 194 | |
| 195 | } |
| 196 | } |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 197 | stream << "Gateway=" << systemConf->defaultGateway() << "\n"; |
| 198 | // write the route section |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 199 | stream << "[" << "Route" << "]\n"; |
| 200 | for(const auto& addr : addrs) |
| 201 | { |
| 202 | if (addr.second->origin() == AddressOrigin::Static) |
| 203 | { |
| 204 | int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6; |
| 205 | std::string destination = getNetworkID( |
| 206 | addressFamily, |
| 207 | addr.second->address(), |
| 208 | addr.second->prefixLength()); |
| 209 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 210 | if (addr.second->gateway() != "0.0.0.0" && |
| 211 | addr.second->gateway() != "" && |
| 212 | destination != "0.0.0.0" && |
| 213 | destination != "") |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 214 | { |
| 215 | |
| 216 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 217 | stream << "Destination=" << destination << "\n"; |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | } |
| 222 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 223 | stream.close(); |
| 224 | } |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 225 | restartSystemdUnit("systemd-networkd.service"); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 226 | } |
| 227 | |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame^] | 228 | void Manager::writeDHCPSection(std::fstream& stream) |
| 229 | { |
| 230 | using namespace std::string_literals; |
| 231 | stream << "DHCP=true\n"; |
| 232 | // write the dhcp section |
| 233 | stream << "[DHCP]\n"; |
| 234 | |
| 235 | // Hardcoding the client identifier to mac, to address below issue |
| 236 | // https://github.com/openbmc/openbmc/issues/1280 |
| 237 | stream << "ClientIdentifier=mac\n"; |
| 238 | |
| 239 | auto value = dhcpConf->dNSEnabled() ? "true"s : "false"s; |
| 240 | stream << "UseDNS="s + value + "\n"; |
| 241 | |
| 242 | value = dhcpConf->nTPEnabled() ? "true"s : "false"s; |
| 243 | stream << "UseNTP="s + value + "\n"; |
| 244 | |
| 245 | value = dhcpConf->hostNameEnabled() ? "true"s : "false"s; |
| 246 | stream << "UseHostname="s + value + "\n"; |
| 247 | } |
| 248 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 249 | bool Manager::getDHCPValue(const std::string& intf) |
| 250 | { |
| 251 | bool dhcp = false; |
| 252 | // Get the interface mode value from systemd conf |
| 253 | using namespace std::string_literals; |
| 254 | fs::path confPath = confDir; |
| 255 | std::string fileName = "00-bmc-"s + intf + ".network"s; |
| 256 | confPath /= fileName; |
| 257 | |
| 258 | try |
| 259 | { |
| 260 | config::Parser parser(confPath.string()); |
| 261 | auto values = parser.getValues("Network","DHCP"); |
| 262 | // There will be only single value for DHCP key. |
| 263 | if (values[0] == "true") |
| 264 | { |
| 265 | dhcp = true; |
| 266 | } |
| 267 | } |
| 268 | catch (InternalFailure& e) |
| 269 | { |
| 270 | commit<InternalFailure>(); |
| 271 | } |
| 272 | return dhcp; |
| 273 | } |
| 274 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 275 | }//namespace network |
| 276 | }//namespace phosphor |