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, |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 49 | stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr, |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 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, |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 57 | stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr, |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 58 | AddressOrigin origin) : |
| 59 | IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit), |
| 60 | parent(parent), objPath(std::move(objPath)) |
| 61 | { |
William A. Kennington III | d99e6db | 2022-11-15 20:39:45 -0800 | [diff] [blame] | 62 | IP::address(std::to_string(addr.getAddr()), true); |
| 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 | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 94 | log<level::ERR>("Tried to delete a non-static address"), |
| 95 | entry("ADDRESS=%s", address().c_str()), |
| 96 | entry("PREFIX=%" PRIu8, prefixLength()), |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 97 | entry("INTERFACE=%s", parent.get().interfaceName().c_str()); |
William A. Kennington III | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 98 | elog<InternalFailure>(); |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 99 | } |
| 100 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 101 | std::unique_ptr<IPAddress> ptr; |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 102 | auto& addrs = parent.get().addrs; |
| 103 | for (auto it = addrs.begin(); it != addrs.end(); ++it) |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 104 | { |
| 105 | if (it->second.get() == this) |
| 106 | { |
| 107 | ptr = std::move(it->second); |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 108 | addrs.erase(it); |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 113 | parent.get().writeConfigurationFile(); |
| 114 | parent.get().manager.get().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 |