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 | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 4 | #include "netlink.hpp" |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 5 | #include "network_manager.hpp" |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 6 | #include "util.hpp" |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 7 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 8 | #include <linux/netlink.h> |
| 9 | #include <linux/rtnetlink.h> |
| 10 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 13 | #include <stdexcept> |
| 14 | #include <stdplus/raw.hpp> |
| 15 | #include <string> |
| 16 | #include <string_view> |
| 17 | #include <vector> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 18 | #include <xyz/openbmc_project/Common/error.hpp> |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 19 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 20 | namespace phosphor |
| 21 | { |
| 22 | namespace network |
| 23 | { |
| 24 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 25 | std::vector<AddressInfo> getCurrentAddresses(const AddressFilter& filter) |
| 26 | { |
| 27 | std::vector<AddressInfo> addresses; |
| 28 | auto cb = [&filter, &addresses](const nlmsghdr& hdr, std::string_view msg) { |
| 29 | detail::parseAddress(filter, hdr, msg, addresses); |
| 30 | }; |
| 31 | ifaddrmsg msg{}; |
| 32 | msg.ifa_index = filter.interface; |
| 33 | netlink::performRequest(NETLINK_ROUTE, RTM_GETADDR, NLM_F_DUMP, msg, cb); |
| 34 | return addresses; |
| 35 | } |
| 36 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 37 | using namespace phosphor::logging; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 38 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
William A. Kennington III | 5e72d08 | 2022-09-01 13:10:17 -0700 | [diff] [blame] | 39 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 40 | using Reason = xyz::openbmc_project::Common::NotAllowed::REASON; |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 41 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 42 | static auto makeObjPath(std::string_view root, IfAddr addr) |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 43 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 44 | auto ret = sdbusplus::message::object_path(std::string(root)); |
| 45 | ret /= std::to_string(addr); |
| 46 | return ret; |
| 47 | } |
Ravi Teja | 2fd2f7d | 2019-06-06 03:27:55 -0500 | [diff] [blame] | 48 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 49 | template <typename T> |
| 50 | struct Proto |
| 51 | { |
| 52 | }; |
| 53 | |
| 54 | template <> |
| 55 | struct Proto<in_addr> |
| 56 | { |
| 57 | static inline constexpr auto value = IP::Protocol::IPv4; |
| 58 | }; |
| 59 | |
| 60 | template <> |
| 61 | struct Proto<in6_addr> |
| 62 | { |
| 63 | static inline constexpr auto value = IP::Protocol::IPv6; |
| 64 | }; |
| 65 | |
| 66 | IPAddress::IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot, |
| 67 | EthernetInterface& parent, IfAddr addr, |
| 68 | AddressOrigin origin) : |
| 69 | IPAddress(bus, makeObjPath(objRoot, addr), parent, addr, origin) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | IPAddress::IPAddress(sdbusplus::bus_t& bus, |
| 74 | sdbusplus::message::object_path objPath, |
| 75 | EthernetInterface& parent, IfAddr addr, |
| 76 | AddressOrigin origin) : |
| 77 | IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit), |
| 78 | parent(parent), objPath(std::move(objPath)) |
| 79 | { |
| 80 | IP::address(std::to_string(addr.getAddr())); |
| 81 | IP::prefixLength(addr.getPfx()); |
| 82 | IP::type(std::visit([](auto v) { return Proto<decltype(v)>::value; }, |
| 83 | addr.getAddr())); |
Ravi Teja | 2fd2f7d | 2019-06-06 03:27:55 -0500 | [diff] [blame] | 84 | IP::origin(origin); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 85 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 86 | // Emit deferred signal. |
| 87 | emit_object_added(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 88 | } |
William A. Kennington III | 5e72d08 | 2022-09-01 13:10:17 -0700 | [diff] [blame] | 89 | std::string IPAddress::address(std::string /*ipAddress*/) |
| 90 | { |
| 91 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 92 | } |
| 93 | uint8_t IPAddress::prefixLength(uint8_t /*value*/) |
| 94 | { |
| 95 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 96 | } |
| 97 | std::string IPAddress::gateway(std::string /*gateway*/) |
| 98 | { |
| 99 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 100 | } |
| 101 | IP::Protocol IPAddress::type(IP::Protocol /*type*/) |
| 102 | { |
| 103 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 104 | } |
| 105 | IP::AddressOrigin IPAddress::origin(IP::AddressOrigin /*origin*/) |
| 106 | { |
| 107 | elog<NotAllowed>(Reason("Property update is not allowed")); |
| 108 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 109 | void IPAddress::delete_() |
| 110 | { |
William A. Kennington III | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 111 | if (origin() != IP::AddressOrigin::Static) |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 112 | { |
William A. Kennington III | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 113 | log<level::ERR>("Tried to delete a non-static address"), |
| 114 | entry("ADDRESS=%s", address().c_str()), |
| 115 | entry("PREFIX=%" PRIu8, prefixLength()), |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 116 | entry("INTERFACE=%s", parent.interfaceName().c_str()); |
William A. Kennington III | 2dae969 | 2019-04-22 02:18:41 -0700 | [diff] [blame] | 117 | elog<InternalFailure>(); |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 118 | } |
| 119 | |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 120 | std::unique_ptr<IPAddress> ptr; |
| 121 | for (auto it = parent.addrs.begin(); it != parent.addrs.end(); ++it) |
| 122 | { |
| 123 | if (it->second.get() == this) |
| 124 | { |
| 125 | ptr = std::move(it->second); |
| 126 | parent.addrs.erase(it); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | parent.writeConfigurationFile(); |
| 132 | parent.manager.reloadConfigs(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 133 | } |
| 134 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 135 | namespace detail |
| 136 | { |
| 137 | |
| 138 | void parseAddress(const AddressFilter& filter, const nlmsghdr& hdr, |
| 139 | std::string_view msg, std::vector<AddressInfo>& addresses) |
| 140 | { |
| 141 | if (hdr.nlmsg_type != RTM_NEWADDR) |
| 142 | { |
| 143 | throw std::runtime_error("Not an address msg"); |
| 144 | } |
William A. Kennington III | f1289a0 | 2022-10-24 18:30:36 -0700 | [diff] [blame] | 145 | const auto& ifaddr = netlink::extractRtData<ifaddrmsg>(msg); |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 146 | |
| 147 | // Filter out addresses we don't care about |
| 148 | unsigned ifindex = ifaddr.ifa_index; |
| 149 | if (filter.interface != 0 && filter.interface != ifindex) |
| 150 | { |
| 151 | return; |
| 152 | } |
| 153 | if (filter.scope && *filter.scope != ifaddr.ifa_scope) |
| 154 | { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | // Build the info about the address we found |
| 159 | AddressInfo address; |
| 160 | address.interface = ifindex; |
| 161 | address.prefix = ifaddr.ifa_prefixlen; |
| 162 | address.flags = ifaddr.ifa_flags; |
| 163 | address.scope = ifaddr.ifa_scope; |
| 164 | bool set_addr = false; |
| 165 | while (!msg.empty()) |
| 166 | { |
| 167 | auto [hdr, data] = netlink::extractRtAttr(msg); |
| 168 | if (hdr.rta_type == IFA_ADDRESS) |
| 169 | { |
| 170 | address.address = addrFromBuf(ifaddr.ifa_family, data); |
| 171 | set_addr = true; |
| 172 | } |
| 173 | else if (hdr.rta_type == IFA_FLAGS) |
| 174 | { |
| 175 | address.flags = stdplus::raw::extract<uint32_t>(data); |
| 176 | } |
| 177 | } |
| 178 | if (!set_addr) |
| 179 | { |
| 180 | throw std::runtime_error("Missing address"); |
| 181 | } |
| 182 | addresses.push_back(std::move(address)); |
| 183 | } |
| 184 | |
| 185 | } // namespace detail |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 186 | } // namespace network |
| 187 | } // namespace phosphor |