blob: c37cfc6508df1c269e61fe532c30fac4d7fb2ff2 [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"
6#include "routing_table.hpp"
Ratan Gupta82e1ef92017-06-15 08:39:15 +05307
Ratan Gupta82e1ef92017-06-15 08:39:15 +05308#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07009#include <phosphor-logging/log.hpp>
10#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82e1ef92017-06-15 08:39:15 +053011
12namespace phosphor
13{
14namespace network
15{
16
17// systemd service to kick start a target.
Gunnar Mills57d9c502018-09-14 14:42:34 -050018constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1";
19constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1";
20constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053021constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
22constexpr auto METHOD_GET = "Get";
23constexpr auto METHOD_SET = "SetStaticHostname";
24
25using namespace phosphor::logging;
26using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Millsa3629222018-06-19 16:32:04 -050027using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta82e1ef92017-06-15 08:39:15 +053028
29using SystemConfigIntf =
30 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
31
32SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus,
33 const std::string& objPath,
34 Manager& parent) :
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 Iface(bus, objPath.c_str(), true),
36 bus(bus), manager(parent)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053037{
38 auto name = getHostNameFromSystem();
39 route::Table routingTable;
40
41 SystemConfigIntf::hostName(name);
Ravi Tejaa5a09442020-07-17 00:57:33 -050042 auto gatewayList = routingTable.getDefaultGateway();
43 auto gateway6List = routingTable.getDefaultGateway6();
44 // Assign first entry of gateway list
45 std::string gateway;
46 std::string gateway6;
47 if (!gatewayList.empty())
48 {
49 gateway = gatewayList.begin()->second;
50 }
51 if (!gateway6List.empty())
52 {
53 gateway6 = gateway6List.begin()->second;
54 }
55
56 SystemConfigIntf::defaultGateway(gateway);
57 SystemConfigIntf::defaultGateway6(gateway6);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053058
59 this->emit_object_added();
60}
61
62std::string SystemConfiguration::hostName(std::string name)
63{
64 if (SystemConfigIntf::hostName() == name)
65 {
66 return name;
67 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
69 HOSTNAMED_INTERFACE, METHOD_SET);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053070
71 method.append(name, true);
72
73 if (!bus.call(method))
74 {
75 log<level::ERR>("Failed to set the hostname");
76 report<InternalFailure>();
77 return SystemConfigIntf::hostName();
78 }
79
80 return SystemConfigIntf::hostName(name);
81}
82
83std::string SystemConfiguration::getHostNameFromSystem() const
84{
Ratan Guptad30adf82020-06-12 09:47:32 +053085 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053086 {
Ratan Guptad30adf82020-06-12 09:47:32 +053087 std::variant<std::string> name;
88 auto method =
89 bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
90 PROPERTY_INTERFACE, METHOD_GET);
91
92 method.append(HOSTNAMED_INTERFACE, "Hostname");
93
94 auto reply = bus.call(method);
95
Ratan Gupta82e1ef92017-06-15 08:39:15 +053096 reply.read(name);
Ratan Guptad30adf82020-06-12 09:47:32 +053097 return std::get<std::string>(name);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053098 }
Patrick Williamsb108fd72021-09-02 09:45:39 -050099 catch (const sdbusplus::exception::exception& ex)
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530100 {
Ratan Guptad30adf82020-06-12 09:47:32 +0530101 log<level::ERR>(
102 "Failed to get the hostname from systemd-hostnamed service",
103 entry("ERR=%s", ex.what()));
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530104 }
Ratan Guptad30adf82020-06-12 09:47:32 +0530105 return "";
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530106}
107
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530108std::string SystemConfiguration::defaultGateway(std::string gateway)
109{
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500110 auto gw = SystemConfigIntf::defaultGateway();
111 if (gw == gateway)
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530112 {
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500113 return gw;
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530114 }
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500115
116 if (!isValidIP(AF_INET, gateway))
117 {
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800118 log<level::ERR>("Not a valid v4 Gateway",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500119 entry("GATEWAY=%s", gateway.c_str()));
120 elog<InvalidArgument>(
121 InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
122 InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500123 }
124 gw = SystemConfigIntf::defaultGateway(gateway);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700125
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530126 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700127 manager.reloadConfigs();
128
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530129 return gw;
130}
131
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800132std::string SystemConfiguration::defaultGateway6(std::string gateway)
133{
134 auto gw = SystemConfigIntf::defaultGateway6();
135 if (gw == gateway)
136 {
137 return gw;
138 }
139
140 if (!isValidIP(AF_INET6, gateway))
141 {
142 log<level::ERR>("Not a valid v6 Gateway",
143 entry("GATEWAY=%s", gateway.c_str()));
144 elog<InvalidArgument>(
145 InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
146 InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
147 }
148 gw = SystemConfigIntf::defaultGateway6(gateway);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700149
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800150 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700151 manager.reloadConfigs();
152
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800153 return gw;
154}
155
Gunnar Mills57d9c502018-09-14 14:42:34 -0500156} // namespace network
157} // namespace phosphor