Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 1 | #include "ipaddress.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 3 | #include "ethernet_interface.hpp" |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 4 | #include "network_manager.hpp" |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 5 | #include "util.hpp" |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 6 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [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> |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 9 | #include <stdexcept> |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 13 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | |
| 19 | using namespace phosphor::logging; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 20 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
William A. Kennington III | 5e72d08 | 2022-09-01 13:10:17 -0700 | [diff] [blame] | 21 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 22 | using Reason = xyz::openbmc_project::Common::NotAllowed::REASON; |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 23 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 24 | static auto makeObjPath(std::string_view root, IfAddr addr) |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 25 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 26 | auto ret = sdbusplus::message::object_path(std::string(root)); |
| 27 | ret /= std::to_string(addr); |
| 28 | return ret; |
| 29 | } |
Ravi Teja | 2fd2f7d | 2019-06-06 03:27:55 -0500 | [diff] [blame] | 30 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 31 | template <typename T> |
| 32 | struct Proto |
| 33 | { |
| 34 | }; |
| 35 | |
| 36 | template <> |
| 37 | struct Proto<in_addr> |
| 38 | { |
| 39 | static inline constexpr auto value = IP::Protocol::IPv4; |
| 40 | }; |
| 41 | |
| 42 | template <> |
| 43 | struct Proto<in6_addr> |
| 44 | { |
| 45 | static inline constexpr auto value = IP::Protocol::IPv6; |
| 46 | }; |
| 47 | |
| 48 | IPAddress::IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot, |
| 49 | EthernetInterface& parent, IfAddr addr, |
| 50 | AddressOrigin origin) : |
| 51 | IPAddress(bus, makeObjPath(objRoot, addr), parent, addr, origin) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | IPAddress::IPAddress(sdbusplus::bus_t& bus, |
| 56 | sdbusplus::message::object_path objPath, |
| 57 | EthernetInterface& parent, IfAddr addr, |
| 58 | AddressOrigin origin) : |
| 59 | IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit), |
| 60 | parent(parent), objPath(std::move(objPath)) |
| 61 | { |
| 62 | IP::address(std::to_string(addr.getAddr())); |
| 63 | IP::prefixLength(addr.getPfx()); |
| 64 | IP::type(std::visit([](auto v) { return Proto<decltype(v)>::value; }, |
| 65 | addr.getAddr())); |
Ravi Teja | 2fd2f7d | 2019-06-06 03:27:55 -0500 | [diff] [blame] | 66 | IP::origin(origin); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 67 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 68 | // Emit deferred signal. |
| 69 | emit_object_added(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 70 | } |
William A. Kennington III | 5e72d08 | 2022-09-01 13:10:17 -0700 | [diff] [blame] | 71 | std::string IPAddress::address(std::string /*ipAddress*/) |
| 72 | { |
| 73 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 74 | } |
| 75 | uint8_t IPAddress::prefixLength(uint8_t /*value*/) |
| 76 | { |
| 77 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 78 | } |
| 79 | std::string IPAddress::gateway(std::string /*gateway*/) |
| 80 | { |
| 81 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 82 | } |
| 83 | IP::Protocol IPAddress::type(IP::Protocol /*type*/) |
| 84 | { |
| 85 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 86 | } |
| 87 | IP::AddressOrigin IPAddress::origin(IP::AddressOrigin /*origin*/) |
| 88 | { |
| 89 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 90 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 91 | void IPAddress::delete_() |
| 92 | { |
William A. Kennington III | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 93 | if (origin() != IP::AddressOrigin::Static) |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 94 | { |
William A. Kennington III | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 95 | log<level::ERR>("Tried to delete a non-static address"), |
| 96 | entry("ADDRESS=%s", address().c_str()), |
| 97 | entry("PREFIX=%" PRIu8, prefixLength()), |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 98 | entry("INTERFACE=%s", parent.interfaceName().c_str()); |
William A. Kennington III | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 99 | elog<InternalFailure>(); |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 100 | } |
| 101 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 102 | std::unique_ptr<IPAddress> ptr; |
| 103 | for (auto it = parent.addrs.begin(); it != parent.addrs.end(); ++it) |
| 104 | { |
| 105 | if (it->second.get() == this) |
| 106 | { |
| 107 | ptr = std::move(it->second); |
| 108 | parent.addrs.erase(it); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | parent.writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 114 | parent.manager.reloadConfigs(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 115 | } |
| 116 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 117 | } // namespace network |
| 118 | } // namespace phosphor |