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" |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 6 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace network |
| 14 | { |
| 15 | |
| 16 | // systemd service to kick start a target. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 17 | constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1"; |
| 18 | constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1"; |
| 19 | constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1"; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 20 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 21 | constexpr auto METHOD_GET = "Get"; |
| 22 | constexpr auto METHOD_SET = "SetStaticHostname"; |
| 23 | |
| 24 | using namespace phosphor::logging; |
| 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Gunnar Mills | a362922 | 2018-06-19 16:32:04 -0500 | [diff] [blame] | 26 | using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 27 | |
| 28 | using SystemConfigIntf = |
| 29 | sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration; |
| 30 | |
| 31 | SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus, |
| 32 | const std::string& objPath, |
| 33 | Manager& parent) : |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | Iface(bus, objPath.c_str(), true), |
| 35 | bus(bus), manager(parent) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 36 | { |
| 37 | auto name = getHostNameFromSystem(); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 38 | |
| 39 | SystemConfigIntf::hostName(name); |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame^] | 40 | const auto& gatewayList = manager.getRouteTable().getDefaultGateway(); |
| 41 | const auto& gateway6List = manager.getRouteTable().getDefaultGateway6(); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 42 | // 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 Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 56 | |
| 57 | this->emit_object_added(); |
| 58 | } |
| 59 | |
| 60 | std::string SystemConfiguration::hostName(std::string name) |
| 61 | { |
| 62 | if (SystemConfigIntf::hostName() == name) |
| 63 | { |
| 64 | return name; |
| 65 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 66 | auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH, |
| 67 | HOSTNAMED_INTERFACE, METHOD_SET); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 68 | |
| 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 | |
| 81 | std::string SystemConfiguration::getHostNameFromSystem() const |
| 82 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 83 | try |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 84 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 85 | 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 Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 94 | reply.read(name); |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 95 | return std::get<std::string>(name); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 96 | } |
Patrick Williams | b108fd7 | 2021-09-02 09:45:39 -0500 | [diff] [blame] | 97 | catch (const sdbusplus::exception::exception& ex) |
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 | log<level::ERR>( |
| 100 | "Failed to get the hostname from systemd-hostnamed service", |
| 101 | entry("ERR=%s", ex.what())); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 102 | } |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 103 | return ""; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 104 | } |
| 105 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 106 | std::string SystemConfiguration::defaultGateway(std::string gateway) |
| 107 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 108 | auto gw = SystemConfigIntf::defaultGateway(); |
| 109 | if (gw == gateway) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 110 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 111 | return gw; |
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 | |
| 114 | if (!isValidIP(AF_INET, gateway)) |
| 115 | { |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 116 | log<level::ERR>("Not a valid v4 Gateway", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 117 | entry("GATEWAY=%s", gateway.c_str())); |
| 118 | elog<InvalidArgument>( |
| 119 | InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"), |
| 120 | InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str())); |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 121 | } |
| 122 | gw = SystemConfigIntf::defaultGateway(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 123 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 124 | manager.writeToConfigurationFile(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 125 | manager.reloadConfigs(); |
| 126 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 127 | return gw; |
| 128 | } |
| 129 | |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 130 | std::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 III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 147 | |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 148 | manager.writeToConfigurationFile(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 149 | manager.reloadConfigs(); |
| 150 | |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 151 | return gw; |
| 152 | } |
| 153 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 154 | } // namespace network |
| 155 | } // namespace phosphor |