blob: 268ea27a27671d08dfcabb4e1642b49052b863c4 [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);
42 SystemConfigIntf::defaultGateway(routingTable.getDefaultGateway());
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080043 SystemConfigIntf::defaultGateway6(routingTable.getDefaultGateway6());
Ratan Gupta82e1ef92017-06-15 08:39:15 +053044
45 this->emit_object_added();
46}
47
48std::string SystemConfiguration::hostName(std::string name)
49{
50 if (SystemConfigIntf::hostName() == name)
51 {
52 return name;
53 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
55 HOSTNAMED_INTERFACE, METHOD_SET);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053056
57 method.append(name, true);
58
59 if (!bus.call(method))
60 {
61 log<level::ERR>("Failed to set the hostname");
62 report<InternalFailure>();
63 return SystemConfigIntf::hostName();
64 }
65
66 return SystemConfigIntf::hostName(name);
67}
68
69std::string SystemConfiguration::getHostNameFromSystem() const
70{
Ratan Guptad30adf82020-06-12 09:47:32 +053071 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053072 {
Ratan Guptad30adf82020-06-12 09:47:32 +053073 std::variant<std::string> name;
74 auto method =
75 bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
76 PROPERTY_INTERFACE, METHOD_GET);
77
78 method.append(HOSTNAMED_INTERFACE, "Hostname");
79
80 auto reply = bus.call(method);
81
Ratan Gupta82e1ef92017-06-15 08:39:15 +053082 reply.read(name);
Ratan Guptad30adf82020-06-12 09:47:32 +053083 return std::get<std::string>(name);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053084 }
Ratan Guptad30adf82020-06-12 09:47:32 +053085 catch (const sdbusplus::exception::SdBusError& ex)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053086 {
Ratan Guptad30adf82020-06-12 09:47:32 +053087 log<level::ERR>(
88 "Failed to get the hostname from systemd-hostnamed service",
89 entry("ERR=%s", ex.what()));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053090 }
Ratan Guptad30adf82020-06-12 09:47:32 +053091 return "";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053092}
93
Ratan Gupta82e1ef92017-06-15 08:39:15 +053094std::string SystemConfiguration::defaultGateway(std::string gateway)
95{
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -050096 auto gw = SystemConfigIntf::defaultGateway();
97 if (gw == gateway)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053098 {
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -050099 return gw;
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530100 }
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500101
102 if (!isValidIP(AF_INET, gateway))
103 {
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800104 log<level::ERR>("Not a valid v4 Gateway",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105 entry("GATEWAY=%s", gateway.c_str()));
106 elog<InvalidArgument>(
107 InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
108 InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500109 }
110 gw = SystemConfigIntf::defaultGateway(gateway);
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530111 manager.writeToConfigurationFile();
112 return gw;
113}
114
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800115std::string SystemConfiguration::defaultGateway6(std::string gateway)
116{
117 auto gw = SystemConfigIntf::defaultGateway6();
118 if (gw == gateway)
119 {
120 return gw;
121 }
122
123 if (!isValidIP(AF_INET6, gateway))
124 {
125 log<level::ERR>("Not a valid v6 Gateway",
126 entry("GATEWAY=%s", gateway.c_str()));
127 elog<InvalidArgument>(
128 InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
129 InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
130 }
131 gw = SystemConfigIntf::defaultGateway6(gateway);
132 manager.writeToConfigurationFile();
133 return gw;
134}
135
Gunnar Mills57d9c502018-09-14 14:42:34 -0500136} // namespace network
137} // namespace phosphor