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