blob: d1c1ed0550665a51f0d689d78320b0dbc5ed7b14 [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Ratan Gupta82e1ef92017-06-15 08:39:15 +05303#include "system_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
Ratan Gupta82e1ef92017-06-15 08:39:15 +05305#include "network_manager.hpp"
Ratan Gupta82e1ef92017-06-15 08:39:15 +05306
Ratan Gupta82e1ef92017-06-15 08:39:15 +05307#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <phosphor-logging/log.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82e1ef92017-06-15 08:39:15 +053010
11namespace phosphor
12{
13namespace network
14{
15
16// systemd service to kick start a target.
Gunnar Mills57d9c502018-09-14 14:42:34 -050017constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1";
18constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1";
19constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053020constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
21constexpr auto METHOD_GET = "Get";
22constexpr auto METHOD_SET = "SetStaticHostname";
23
24using namespace phosphor::logging;
25using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Millsa3629222018-06-19 16:32:04 -050026using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta82e1ef92017-06-15 08:39:15 +053027
28using SystemConfigIntf =
29 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
30
31SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus,
32 const std::string& objPath,
33 Manager& parent) :
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 Iface(bus, objPath.c_str(), true),
35 bus(bus), manager(parent)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053036{
37 auto name = getHostNameFromSystem();
Ratan Gupta82e1ef92017-06-15 08:39:15 +053038
39 SystemConfigIntf::hostName(name);
William A. Kennington IIIe0564842021-10-23 16:02:22 -070040 const auto& gatewayList = manager.getRouteTable().getDefaultGateway();
41 const auto& gateway6List = manager.getRouteTable().getDefaultGateway6();
Ravi Tejaa5a09442020-07-17 00:57:33 -050042 // Assign first entry of gateway list
43 std::string gateway;
44 std::string gateway6;
45 if (!gatewayList.empty())
46 {
47 gateway = gatewayList.begin()->second;
48 }
49 if (!gateway6List.empty())
50 {
51 gateway6 = gateway6List.begin()->second;
52 }
53
54 SystemConfigIntf::defaultGateway(gateway);
55 SystemConfigIntf::defaultGateway6(gateway6);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053056
57 this->emit_object_added();
58}
59
60std::string SystemConfiguration::hostName(std::string name)
61{
62 if (SystemConfigIntf::hostName() == name)
63 {
64 return name;
65 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
67 HOSTNAMED_INTERFACE, METHOD_SET);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053068
69 method.append(name, true);
70
71 if (!bus.call(method))
72 {
73 log<level::ERR>("Failed to set the hostname");
74 report<InternalFailure>();
75 return SystemConfigIntf::hostName();
76 }
77
78 return SystemConfigIntf::hostName(name);
79}
80
81std::string SystemConfiguration::getHostNameFromSystem() const
82{
Ratan Guptad30adf82020-06-12 09:47:32 +053083 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053084 {
Ratan Guptad30adf82020-06-12 09:47:32 +053085 std::variant<std::string> name;
86 auto method =
87 bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
88 PROPERTY_INTERFACE, METHOD_GET);
89
90 method.append(HOSTNAMED_INTERFACE, "Hostname");
91
92 auto reply = bus.call(method);
93
Ratan Gupta82e1ef92017-06-15 08:39:15 +053094 reply.read(name);
Ratan Guptad30adf82020-06-12 09:47:32 +053095 return std::get<std::string>(name);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053096 }
Patrick Williamsb108fd72021-09-02 09:45:39 -050097 catch (const sdbusplus::exception::exception& ex)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053098 {
Ratan Guptad30adf82020-06-12 09:47:32 +053099 log<level::ERR>(
100 "Failed to get the hostname from systemd-hostnamed service",
101 entry("ERR=%s", ex.what()));
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530102 }
Ratan Guptad30adf82020-06-12 09:47:32 +0530103 return "";
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530104}
105
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530106std::string SystemConfiguration::defaultGateway(std::string gateway)
107{
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500108 auto gw = SystemConfigIntf::defaultGateway();
109 if (gw == gateway)
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530110 {
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500111 return gw;
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530112 }
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500113
114 if (!isValidIP(AF_INET, gateway))
115 {
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800116 log<level::ERR>("Not a valid v4 Gateway",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500117 entry("GATEWAY=%s", gateway.c_str()));
118 elog<InvalidArgument>(
119 InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
120 InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500121 }
122 gw = SystemConfigIntf::defaultGateway(gateway);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700123
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530124 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700125 manager.reloadConfigs();
126
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530127 return gw;
128}
129
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800130std::string SystemConfiguration::defaultGateway6(std::string gateway)
131{
132 auto gw = SystemConfigIntf::defaultGateway6();
133 if (gw == gateway)
134 {
135 return gw;
136 }
137
138 if (!isValidIP(AF_INET6, gateway))
139 {
140 log<level::ERR>("Not a valid v6 Gateway",
141 entry("GATEWAY=%s", gateway.c_str()));
142 elog<InvalidArgument>(
143 InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
144 InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
145 }
146 gw = SystemConfigIntf::defaultGateway6(gateway);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700147
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800148 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700149 manager.reloadConfigs();
150
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800151 return gw;
152}
153
Gunnar Mills57d9c502018-09-14 14:42:34 -0500154} // namespace network
155} // namespace phosphor