blob: e678725788a09ca8a55d41322564f0ce264628fe [file] [log] [blame]
Ratan Gupta2eff84f2017-04-20 19:19:15 +05301#include "ipaddress.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
Ratan Gupta2eff84f2017-04-20 19:19:15 +05303#include "ethernet_interface.hpp"
William A. Kennington III59e5b912022-11-02 02:49:46 -07004#include "network_manager.hpp"
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -05005#include "util.hpp"
Ratan Gupta2eff84f2017-04-20 19:19:15 +05306
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -05007#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -07008#include <phosphor-logging/lg2.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -05009#include <xyz/openbmc_project/Common/error.hpp>
10
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -070011#include <stdexcept>
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -070012#include <string>
13#include <string_view>
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -070014
Ratan Gupta2eff84f2017-04-20 19:19:15 +053015namespace phosphor
16{
17namespace network
18{
19
20using namespace phosphor::logging;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050021using namespace sdbusplus::xyz::openbmc_project::Common::Error;
William A. Kennington III5e72d082022-09-01 13:10:17 -070022using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
23using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
Ratan Gupta2eff84f2017-04-20 19:19:15 +053024
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070025static auto makeObjPath(std::string_view root, stdplus::SubnetAny addr)
Ratan Gupta2eff84f2017-04-20 19:19:15 +053026{
William A. Kennington III59e5b912022-11-02 02:49:46 -070027 auto ret = sdbusplus::message::object_path(std::string(root));
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070028 stdplus::ToStrHandle<stdplus::ToStr<stdplus::SubnetAny>> tsh;
29 ret /= tsh(addr);
William A. Kennington III59e5b912022-11-02 02:49:46 -070030 return ret;
31}
Ravi Teja2fd2f7d2019-06-06 03:27:55 -050032
William A. Kennington III59e5b912022-11-02 02:49:46 -070033template <typename T>
34struct Proto
Patrick Williams89d734b2023-05-10 07:50:25 -050035{};
William A. Kennington III59e5b912022-11-02 02:49:46 -070036
37template <>
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070038struct Proto<stdplus::In4Addr>
William A. Kennington III59e5b912022-11-02 02:49:46 -070039{
40 static inline constexpr auto value = IP::Protocol::IPv4;
41};
42
43template <>
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070044struct Proto<stdplus::In6Addr>
William A. Kennington III59e5b912022-11-02 02:49:46 -070045{
46 static inline constexpr auto value = IP::Protocol::IPv6;
47};
48
49IPAddress::IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot,
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070050 stdplus::PinnedRef<EthernetInterface> parent,
51 stdplus::SubnetAny addr, AddressOrigin origin) :
William A. Kennington III59e5b912022-11-02 02:49:46 -070052 IPAddress(bus, makeObjPath(objRoot, addr), parent, addr, origin)
Patrick Williams89d734b2023-05-10 07:50:25 -050053{}
William A. Kennington III59e5b912022-11-02 02:49:46 -070054
55IPAddress::IPAddress(sdbusplus::bus_t& bus,
56 sdbusplus::message::object_path objPath,
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070057 stdplus::PinnedRef<EthernetInterface> parent,
58 stdplus::SubnetAny addr, AddressOrigin origin) :
William A. Kennington III59e5b912022-11-02 02:49:46 -070059 IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit),
60 parent(parent), objPath(std::move(objPath))
61{
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070062 IP::address(stdplus::toStr(addr.getAddr()), true);
William A. Kennington IIId99e6db2022-11-15 20:39:45 -080063 IP::prefixLength(addr.getPfx(), true);
William A. Kennington III59e5b912022-11-02 02:49:46 -070064 IP::type(std::visit([](auto v) { return Proto<decltype(v)>::value; },
William A. Kennington IIId99e6db2022-11-15 20:39:45 -080065 addr.getAddr()),
66 true);
67 IP::origin(origin, true);
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 emit_object_added();
Ratan Gupta2eff84f2017-04-20 19:19:15 +053069}
William A. Kennington III5e72d082022-09-01 13:10:17 -070070std::string IPAddress::address(std::string /*ipAddress*/)
71{
72 elog<NotAllowed>(Reason("Property update is not allowed"));
73}
74uint8_t IPAddress::prefixLength(uint8_t /*value*/)
75{
76 elog<NotAllowed>(Reason("Property update is not allowed"));
77}
78std::string IPAddress::gateway(std::string /*gateway*/)
79{
80 elog<NotAllowed>(Reason("Property update is not allowed"));
81}
82IP::Protocol IPAddress::type(IP::Protocol /*type*/)
83{
84 elog<NotAllowed>(Reason("Property update is not allowed"));
85}
86IP::AddressOrigin IPAddress::origin(IP::AddressOrigin /*origin*/)
87{
88 elog<NotAllowed>(Reason("Property update is not allowed"));
89}
Ratan Gupta2eff84f2017-04-20 19:19:15 +053090void IPAddress::delete_()
91{
William A. Kennington III2dae9692019-04-22 02:18:41 -070092 if (origin() != IP::AddressOrigin::Static)
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050093 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -070094 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());
William A. Kennington III2dae9692019-04-22 02:18:41 -070098 elog<InternalFailure>();
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050099 }
100
William A. Kennington III59e5b912022-11-02 02:49:46 -0700101 std::unique_ptr<IPAddress> ptr;
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800102 auto& addrs = parent.get().addrs;
103 for (auto it = addrs.begin(); it != addrs.end(); ++it)
William A. Kennington III59e5b912022-11-02 02:49:46 -0700104 {
105 if (it->second.get() == this)
106 {
107 ptr = std::move(it->second);
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800108 addrs.erase(it);
William A. Kennington III59e5b912022-11-02 02:49:46 -0700109 break;
110 }
111 }
112
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800113 parent.get().writeConfigurationFile();
114 parent.get().manager.get().reloadConfigs();
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530115}
116
Gunnar Mills57d9c502018-09-14 14:42:34 -0500117} // namespace network
118} // namespace phosphor