Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 3 | #include "system_configuration.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 5 | #include "network_manager.hpp" |
| 6 | #include "routing_table.hpp" |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 7 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 9 | #include <phosphor-logging/log.hpp> |
| 10 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace network |
| 15 | { |
| 16 | |
| 17 | // systemd service to kick start a target. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 18 | constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1"; |
| 19 | constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1"; |
| 20 | constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1"; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 21 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 22 | constexpr auto METHOD_GET = "Get"; |
| 23 | constexpr auto METHOD_SET = "SetStaticHostname"; |
| 24 | |
| 25 | using namespace phosphor::logging; |
| 26 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Gunnar Mills | a362922 | 2018-06-19 16:32:04 -0500 | [diff] [blame] | 27 | using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 28 | |
| 29 | using SystemConfigIntf = |
| 30 | sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration; |
| 31 | |
| 32 | SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus, |
| 33 | const std::string& objPath, |
| 34 | Manager& parent) : |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | Iface(bus, objPath.c_str(), true), |
| 36 | bus(bus), manager(parent) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 37 | { |
| 38 | auto name = getHostNameFromSystem(); |
| 39 | route::Table routingTable; |
| 40 | |
| 41 | SystemConfigIntf::hostName(name); |
| 42 | SystemConfigIntf::defaultGateway(routingTable.getDefaultGateway()); |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 43 | SystemConfigIntf::defaultGateway6(routingTable.getDefaultGateway6()); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 44 | |
| 45 | this->emit_object_added(); |
| 46 | } |
| 47 | |
| 48 | std::string SystemConfiguration::hostName(std::string name) |
| 49 | { |
| 50 | if (SystemConfigIntf::hostName() == name) |
| 51 | { |
| 52 | return name; |
| 53 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 54 | auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH, |
| 55 | HOSTNAMED_INTERFACE, METHOD_SET); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 56 | |
| 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 | |
| 69 | std::string SystemConfiguration::getHostNameFromSystem() const |
| 70 | { |
| 71 | sdbusplus::message::variant<std::string> name; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 72 | auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH, |
| 73 | PROPERTY_INTERFACE, METHOD_GET); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 74 | |
| 75 | method.append(HOSTNAMED_INTERFACE, "Hostname"); |
| 76 | |
| 77 | auto reply = bus.call(method); |
| 78 | |
| 79 | if (reply) |
| 80 | { |
| 81 | reply.read(name); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | log<level::ERR>("Failed to get hostname"); |
| 86 | report<InternalFailure>(); |
| 87 | return ""; |
| 88 | } |
William A. Kennington III | 79e4415 | 2018-11-06 16:46:01 -0800 | [diff] [blame] | 89 | return sdbusplus::message::variant_ns::get<std::string>(name); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 90 | } |
| 91 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 92 | std::string SystemConfiguration::defaultGateway(std::string gateway) |
| 93 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 94 | auto gw = SystemConfigIntf::defaultGateway(); |
| 95 | if (gw == gateway) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 96 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 97 | return gw; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 98 | } |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 99 | |
| 100 | if (!isValidIP(AF_INET, gateway)) |
| 101 | { |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 102 | log<level::ERR>("Not a valid v4 Gateway", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 103 | entry("GATEWAY=%s", gateway.c_str())); |
| 104 | elog<InvalidArgument>( |
| 105 | InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"), |
| 106 | InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str())); |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 107 | } |
| 108 | gw = SystemConfigIntf::defaultGateway(gateway); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 109 | manager.writeToConfigurationFile(); |
| 110 | return gw; |
| 111 | } |
| 112 | |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 113 | std::string SystemConfiguration::defaultGateway6(std::string gateway) |
| 114 | { |
| 115 | auto gw = SystemConfigIntf::defaultGateway6(); |
| 116 | if (gw == gateway) |
| 117 | { |
| 118 | return gw; |
| 119 | } |
| 120 | |
| 121 | if (!isValidIP(AF_INET6, gateway)) |
| 122 | { |
| 123 | log<level::ERR>("Not a valid v6 Gateway", |
| 124 | entry("GATEWAY=%s", gateway.c_str())); |
| 125 | elog<InvalidArgument>( |
| 126 | InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"), |
| 127 | InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str())); |
| 128 | } |
| 129 | gw = SystemConfigIntf::defaultGateway6(gateway); |
| 130 | manager.writeToConfigurationFile(); |
| 131 | return gw; |
| 132 | } |
| 133 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 134 | } // namespace network |
| 135 | } // namespace phosphor |