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); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 42 | 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 Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 58 | |
| 59 | this->emit_object_added(); |
| 60 | } |
| 61 | |
| 62 | std::string SystemConfiguration::hostName(std::string name) |
| 63 | { |
| 64 | if (SystemConfigIntf::hostName() == name) |
| 65 | { |
| 66 | return name; |
| 67 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 68 | auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH, |
| 69 | HOSTNAMED_INTERFACE, METHOD_SET); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 70 | |
| 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 | |
| 83 | std::string SystemConfiguration::getHostNameFromSystem() const |
| 84 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 85 | try |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 86 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 87 | 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 Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 96 | reply.read(name); |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 97 | return std::get<std::string>(name); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 98 | } |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 99 | catch (const sdbusplus::exception::SdBusError& ex) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 100 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 101 | log<level::ERR>( |
| 102 | "Failed to get the hostname from systemd-hostnamed service", |
| 103 | entry("ERR=%s", ex.what())); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 104 | } |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 105 | return ""; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 106 | } |
| 107 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 108 | std::string SystemConfiguration::defaultGateway(std::string gateway) |
| 109 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 110 | auto gw = SystemConfigIntf::defaultGateway(); |
| 111 | if (gw == gateway) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 112 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 113 | return gw; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 114 | } |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 115 | |
| 116 | if (!isValidIP(AF_INET, gateway)) |
| 117 | { |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 118 | log<level::ERR>("Not a valid v4 Gateway", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 119 | entry("GATEWAY=%s", gateway.c_str())); |
| 120 | elog<InvalidArgument>( |
| 121 | InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"), |
| 122 | InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str())); |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 123 | } |
| 124 | gw = SystemConfigIntf::defaultGateway(gateway); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 125 | manager.writeToConfigurationFile(); |
| 126 | return gw; |
| 127 | } |
| 128 | |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 129 | std::string SystemConfiguration::defaultGateway6(std::string gateway) |
| 130 | { |
| 131 | auto gw = SystemConfigIntf::defaultGateway6(); |
| 132 | if (gw == gateway) |
| 133 | { |
| 134 | return gw; |
| 135 | } |
| 136 | |
| 137 | if (!isValidIP(AF_INET6, gateway)) |
| 138 | { |
| 139 | log<level::ERR>("Not a valid v6 Gateway", |
| 140 | entry("GATEWAY=%s", gateway.c_str())); |
| 141 | elog<InvalidArgument>( |
| 142 | InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"), |
| 143 | InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str())); |
| 144 | } |
| 145 | gw = SystemConfigIntf::defaultGateway6(gateway); |
| 146 | manager.writeToConfigurationFile(); |
| 147 | return gw; |
| 148 | } |
| 149 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 150 | } // namespace network |
| 151 | } // namespace phosphor |