blob: 62ac2201d32cca1e31f7a879828065d53b8f7d47 [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>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <phosphor-logging/log.hpp>
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -07009#include <stdexcept>
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -070010#include <string>
11#include <string_view>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <xyz/openbmc_project/Common/error.hpp>
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -070013
Ratan Gupta2eff84f2017-04-20 19:19:15 +053014namespace phosphor
15{
16namespace network
17{
18
19using namespace phosphor::logging;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050020using namespace sdbusplus::xyz::openbmc_project::Common::Error;
William A. Kennington III5e72d082022-09-01 13:10:17 -070021using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
22using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
Ratan Gupta2eff84f2017-04-20 19:19:15 +053023
William A. Kennington III59e5b912022-11-02 02:49:46 -070024static auto makeObjPath(std::string_view root, IfAddr addr)
Ratan Gupta2eff84f2017-04-20 19:19:15 +053025{
William A. Kennington III59e5b912022-11-02 02:49:46 -070026 auto ret = sdbusplus::message::object_path(std::string(root));
27 ret /= std::to_string(addr);
28 return ret;
29}
Ravi Teja2fd2f7d2019-06-06 03:27:55 -050030
William A. Kennington III59e5b912022-11-02 02:49:46 -070031template <typename T>
32struct Proto
33{
34};
35
36template <>
37struct Proto<in_addr>
38{
39 static inline constexpr auto value = IP::Protocol::IPv4;
40};
41
42template <>
43struct Proto<in6_addr>
44{
45 static inline constexpr auto value = IP::Protocol::IPv6;
46};
47
48IPAddress::IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot,
William A. Kennington III9ede1b72022-11-21 01:59:28 -080049 stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
William A. Kennington III59e5b912022-11-02 02:49:46 -070050 AddressOrigin origin) :
51 IPAddress(bus, makeObjPath(objRoot, addr), parent, addr, origin)
52{
53}
54
55IPAddress::IPAddress(sdbusplus::bus_t& bus,
56 sdbusplus::message::object_path objPath,
William A. Kennington III9ede1b72022-11-21 01:59:28 -080057 stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
William A. Kennington III59e5b912022-11-02 02:49:46 -070058 AddressOrigin origin) :
59 IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit),
60 parent(parent), objPath(std::move(objPath))
61{
William A. Kennington IIId99e6db2022-11-15 20:39:45 -080062 IP::address(std::to_string(addr.getAddr()), true);
63 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 III2dae9692019-04-22 02:18:41 -070094 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 III9ede1b72022-11-21 01:59:28 -080097 entry("INTERFACE=%s", parent.get().interfaceName().c_str());
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