blob: b3ca3dbdd0f7ed660daec18d8e438ae031bf6ca0 [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,
49 EthernetInterface& parent, IfAddr addr,
50 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,
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 Teja2fd2f7d2019-06-06 03:27:55 -050066 IP::origin(origin);
Ratan Gupta29b0e432017-05-25 12:51:40 +053067
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 // Emit deferred signal.
69 emit_object_added();
Ratan Gupta2eff84f2017-04-20 19:19:15 +053070}
William A. Kennington III5e72d082022-09-01 13:10:17 -070071std::string IPAddress::address(std::string /*ipAddress*/)
72{
73 elog<NotAllowed>(Reason("Property update is not allowed"));
74}
75uint8_t IPAddress::prefixLength(uint8_t /*value*/)
76{
77 elog<NotAllowed>(Reason("Property update is not allowed"));
78}
79std::string IPAddress::gateway(std::string /*gateway*/)
80{
81 elog<NotAllowed>(Reason("Property update is not allowed"));
82}
83IP::Protocol IPAddress::type(IP::Protocol /*type*/)
84{
85 elog<NotAllowed>(Reason("Property update is not allowed"));
86}
87IP::AddressOrigin IPAddress::origin(IP::AddressOrigin /*origin*/)
88{
89 elog<NotAllowed>(Reason("Property update is not allowed"));
90}
Ratan Gupta2eff84f2017-04-20 19:19:15 +053091void IPAddress::delete_()
92{
William A. Kennington III2dae9692019-04-22 02:18:41 -070093 if (origin() != IP::AddressOrigin::Static)
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050094 {
William A. Kennington III2dae9692019-04-22 02:18:41 -070095 log<level::ERR>("Tried to delete a non-static address"),
96 entry("ADDRESS=%s", address().c_str()),
97 entry("PREFIX=%" PRIu8, prefixLength()),
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050098 entry("INTERFACE=%s", parent.interfaceName().c_str());
William A. Kennington III2dae9692019-04-22 02:18:41 -070099 elog<InternalFailure>();
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500100 }
101
William A. Kennington III59e5b912022-11-02 02:49:46 -0700102 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 IIId6f53402022-11-07 14:48:53 -0800114 parent.manager.reloadConfigsNoRefresh();
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530115}
116
Gunnar Mills57d9c502018-09-14 14:42:34 -0500117} // namespace network
118} // namespace phosphor