blob: aff86a0f5a48317115e6a0f42ae809c3f6838814 [file] [log] [blame]
Ratan Gupta6811f822017-04-14 16:34:56 +05301#include "config.h"
2#include "network_manager.hpp"
Michael Tritz29f2fd62017-05-22 15:27:26 -05003#include "network_config.hpp"
Ratan Gupta44ae86e2017-05-15 21:52:14 +05304#include "xyz/openbmc_project/Common/error.hpp"
Ratan Gupta6811f822017-04-14 16:34:56 +05305
6#include <phosphor-logging/log.hpp>
Ratan Gupta44ae86e2017-05-15 21:52:14 +05307#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +05308
9#include <algorithm>
Ratan Gupta738a67f2017-04-21 10:38:05 +053010#include <bitset>
Ratan Gupta738a67f2017-04-21 10:38:05 +053011#include <map>
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053012#include <fstream>
Ratan Gupta738a67f2017-04-21 10:38:05 +053013
Ratan Gupta6811f822017-04-14 16:34:56 +053014#include <arpa/inet.h>
15#include <dirent.h>
16#include <net/if.h>
17
Michael Tritz29f2fd62017-05-22 15:27:26 -050018#include <string>
Ratan Gupta6811f822017-04-14 16:34:56 +053019
20namespace phosphor
21{
22namespace network
23{
Ratan Gupta82549cc2017-04-21 08:45:23 +053024
Ratan Gupta6811f822017-04-14 16:34:56 +053025using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053026using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053027
28Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath):
Ratan Gupta29b0e432017-05-25 12:51:40 +053029 details::VLANCreateIface(bus, objPath, true),
30 bus(bus),
31 objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053032{
Ratan Guptaef85eb92017-06-15 08:57:54 +053033 confDir = NETWORK_CONF_DIR;
34}
35
36void Manager::setConfDir(const fs::path& dir)
37{
38 confDir = dir;
Ratan Gupta29b0e432017-05-25 12:51:40 +053039}
40
41void Manager::createInterfaces()
42{
Ratan Guptaef85eb92017-06-15 08:57:54 +053043 //clear all the interfaces first
44 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +053045
Ratan Gupta82549cc2017-04-21 08:45:23 +053046 auto interfaceInfoList = getInterfaceAddrs();
Ratan Gupta6811f822017-04-14 16:34:56 +053047
Ratan Gupta738a67f2017-04-21 10:38:05 +053048 for (const auto& intfInfo : interfaceInfoList)
Ratan Gupta6811f822017-04-14 16:34:56 +053049 {
Ratan Gupta29b0e432017-05-25 12:51:40 +053050 fs::path objPath = objectPath;
51 objPath /= intfInfo.first;
Ratan Gupta6811f822017-04-14 16:34:56 +053052
53 this->interfaces.emplace(std::make_pair(
Ratan Gupta738a67f2017-04-21 10:38:05 +053054 intfInfo.first,
55 std::make_unique<
56 phosphor::network::EthernetInterface>
57 (bus,
Ratan Gupta29b0e432017-05-25 12:51:40 +053058 objPath.string(),
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053059 false,
60 *this)));
Ratan Gupta29b0e432017-05-25 12:51:40 +053061
62 interfaces[intfInfo.first]->setAddressList(intfInfo.second);
Ratan Gupta6811f822017-04-14 16:34:56 +053063 }
64}
65
Ratan Guptaef85eb92017-06-15 08:57:54 +053066void Manager::createChildObjects()
67{
68 // creates the ethernet interface dbus object.
69 createInterfaces();
70 // create the system conf object.
71 fs::path objPath = objectPath;
72 objPath /= "config";
73 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
74 bus, objPath.string(), *this);
75
76}
77
Ratan Gupta82549cc2017-04-21 08:45:23 +053078void Manager::vLAN(IntfName interfaceName, uint16_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +053079{
80}
81
Michael Tritz29f2fd62017-05-22 15:27:26 -050082void Manager::reset()
83{
84 const std::string networkConfig = "/etc/systemd/network/";
85 bool filesExist, interfacesMapped = false;
86
87 if(fs::is_directory(networkConfig))
88 {
89 for(auto& file : fs::directory_iterator(networkConfig))
90 {
91 std::string filename = file.path().filename().c_str();
92
93 if(filename.substr(filename.find_last_of(".") + 1) == "network")
94 {
95 fs::remove(file.path());
96 filesExist = true;
97 }
98 }
99
100 if(!filesExist)
101 {
102 log<level::INFO>("No existing network configuration was found.");
103 }
104
105 for (auto& intf : interfaces)
106 {
107 std::string filename = networkConfig + "00-bmc-" + intf.first +
108 ".network";
109
110 bmc::writeDHCPDefault(filename, intf.first);
111 interfacesMapped = true;
112 }
113
114 if(interfacesMapped)
115 {
116 log<level::INFO>("Network configuration reset to DHCP.");
117 }
118 else
119 {
120 log<level::ERR>("No network interfaces are mapped.");
121 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
122 }
123 }
124 else
125 {
126 log<level::ERR>("Network configuration directory not found!");
127 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
128 }
129
130 return;
131}
132
Ratan Gupta82549cc2017-04-21 08:45:23 +0530133IntfAddrMap Manager::getInterfaceAddrs() const
Ratan Gupta6811f822017-04-14 16:34:56 +0530134{
Ratan Gupta8804feb2017-05-25 10:49:57 +0530135 IntfAddrMap intfMap{};
136 AddrList addrList{};
137 struct ifaddrs* ifaddr = nullptr;
Ratan Gupta44ae86e2017-05-15 21:52:14 +0530138
139 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +0530140 // attempt to fill struct with ifaddrs
141 if (getifaddrs(&ifaddr) == -1)
142 {
Ratan Gupta44ae86e2017-05-15 21:52:14 +0530143 auto error = errno;
144 log<level::ERR>("Error occurred during the getifaddrs call",
145 entry("ERRNO=%s", strerror(error)));
146 elog<InternalFailure>();
Ratan Gupta6811f822017-04-14 16:34:56 +0530147 }
148
149 details::AddrPtr ifaddrPtr(ifaddr);
150 ifaddr = nullptr;
151
Ratan Gupta8804feb2017-05-25 10:49:57 +0530152 std::string intfName{};
Ratan Gupta6811f822017-04-14 16:34:56 +0530153
154 for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
155 {
156 // walk interfaces
157 if (ifa->ifa_addr == nullptr)
158 {
159 continue;
160 }
161
162 // get only INET interfaces not ipv6
163 if (ifa->ifa_addr->sa_family == AF_INET ||
164 ifa->ifa_addr->sa_family == AF_INET6)
165 {
166 // if loopback, or not running ignore
167 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
168 !(ifa->ifa_flags & IFF_RUNNING))
169 {
170 continue;
171 }
172 // if the interface name is not same as the previous
173 // iteration then add the addr list into
174 // the map.
175 if (intfName != "" && intfName != std::string(ifa->ifa_name))
176 {
177 intfMap.emplace(intfName, addrList);
178 addrList.clear();
179 }
180 intfName = ifa->ifa_name;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530181 AddrInfo info{};
Ratan Gupta82549cc2017-04-21 08:45:23 +0530182 char ip[INET6_ADDRSTRLEN] = { 0 };
Ratan Gupta738a67f2017-04-21 10:38:05 +0530183 char subnetMask[INET6_ADDRSTRLEN] = { 0 };
Ratan Gupta6811f822017-04-14 16:34:56 +0530184
185 if (ifa->ifa_addr->sa_family == AF_INET)
186 {
187
188 inet_ntop(ifa->ifa_addr->sa_family,
189 &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr),
Ratan Gupta82549cc2017-04-21 08:45:23 +0530190 ip,
191 sizeof(ip));
Ratan Gupta738a67f2017-04-21 10:38:05 +0530192
193 inet_ntop(ifa->ifa_addr->sa_family,
194 &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr),
195 subnetMask,
196 sizeof(subnetMask));
197
Ratan Gupta6811f822017-04-14 16:34:56 +0530198 }
199 else
200 {
201 inet_ntop(ifa->ifa_addr->sa_family,
202 &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr),
Ratan Gupta82549cc2017-04-21 08:45:23 +0530203 ip,
204 sizeof(ip));
Ratan Gupta6811f822017-04-14 16:34:56 +0530205
Ratan Gupta738a67f2017-04-21 10:38:05 +0530206 inet_ntop(ifa->ifa_addr->sa_family,
207 &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr),
208 subnetMask,
209 sizeof(subnetMask));
210
Ratan Gupta6811f822017-04-14 16:34:56 +0530211 }
212
213 info.addrType = ifa->ifa_addr->sa_family;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530214 info.ipaddress = ip;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530215 info.prefix = toCidr(info.addrType, std::string(subnetMask));
Ratan Gupta6811f822017-04-14 16:34:56 +0530216 addrList.emplace_back(info);
217 }
218 }
219 intfMap.emplace(intfName, addrList);
220 return intfMap;
221}
Ratan Gupta738a67f2017-04-21 10:38:05 +0530222
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530223// Need to merge the below function with the code which writes the
224// config file during factory reset.
225//TODO openbmc/openbmc#1751
226void Manager::writeToConfigurationFile()
227{
228 // write all the static ip address in the systemd-network conf file
229
230 using namespace std::string_literals;
231 using AddressOrigin =
232 sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin;
233 namespace fs = std::experimental::filesystem;
234
235 for (const auto& intf : interfaces)
236 {
237
Ratan Guptaef85eb92017-06-15 08:57:54 +0530238 fs::path confPath = confDir;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530239 std::string fileName = "00-bmc-"s + intf.first + ".network"s;
240 confPath /= fileName;
241 std::fstream stream;
242 stream.open(confPath.c_str(), std::fstream::out);
243
244 // Write the device
245 stream << "[" << "Match" << "]\n";
246 stream << "Name=" << intf.first << "\n";
247
248 auto addrs = intf.second->getAddresses();
249
250 // write the network section
251 stream << "[" << "Network" << "]\n";
252 for (const auto& addr : addrs)
253 {
254 if (addr.second->origin() == AddressOrigin::Static)
255 {
256 std::string address = addr.second->address() + "/" + std::to_string(
257 addr.second->prefixLength());
258
259 stream << "Address=" << address << "\n";
Ratan Guptaef85eb92017-06-15 08:57:54 +0530260 if (addr.second->gateway() != "0.0.0.0" &&
261 addr.second->gateway() != "")
262 {
263 stream << "Gateway=" << addr.second->gateway() << "\n";
264 }
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530265
266 }
267 }
Ratan Guptaef85eb92017-06-15 08:57:54 +0530268 stream << "Gateway=" << systemConf->defaultGateway() << "\n";
269 // write the route section
Ratan Guptafc2c7242017-05-29 08:46:06 +0530270 stream << "[" << "Route" << "]\n";
271 for(const auto& addr : addrs)
272 {
273 if (addr.second->origin() == AddressOrigin::Static)
274 {
275 int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6;
276 std::string destination = getNetworkID(
277 addressFamily,
278 addr.second->address(),
279 addr.second->prefixLength());
280
Ratan Guptaef85eb92017-06-15 08:57:54 +0530281 if (addr.second->gateway() != "0.0.0.0" &&
282 addr.second->gateway() != "" &&
283 destination != "0.0.0.0" &&
284 destination != "")
Ratan Guptafc2c7242017-05-29 08:46:06 +0530285 {
286
287 stream << "Gateway=" << addr.second->gateway() << "\n";
288 stream << "Destination=" << destination << "\n";
289 }
290
291 }
292 }
293
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530294 stream.close();
295 }
296 restartSystemdNetworkd();
297}
298
299void Manager::restartSystemdNetworkd()
300{
301 constexpr auto systemdNetworkdService = "systemd-networkd.service";
302
303 auto method = bus.new_method_call(
304 SYSTEMD_BUSNAME,
305 SYSTEMD_PATH,
306 SYSTEMD_INTERFACE,
307 "RestartUnit");
308
309 method.append(systemdNetworkdService,
310 "replace");
311
312 bus.call_noreply(method);
313}
314
Ratan Gupta6811f822017-04-14 16:34:56 +0530315}//namespace network
316}//namespace phosphor