William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 1 | #include "neighbor.hpp" |
| 2 | |
| 3 | #include "ethernet_interface.hpp" |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 4 | #include "netlink.hpp" |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 5 | #include "util.hpp" |
| 6 | |
| 7 | #include <linux/neighbour.h> |
| 8 | #include <linux/netlink.h> |
| 9 | #include <linux/rtnetlink.h> |
| 10 | #include <net/if.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/types.h> |
| 13 | |
William A. Kennington III | 8e61ca9 | 2022-09-01 13:28:33 -0700 | [diff] [blame] | 14 | #include <phosphor-logging/elog-errors.hpp> |
| 15 | #include <phosphor-logging/elog.hpp> |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 16 | #include <stdexcept> |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 17 | #include <stdplus/raw.hpp> |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 18 | #include <string_view> |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 19 | #include <utility> |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 20 | #include <vector> |
William A. Kennington III | 8e61ca9 | 2022-09-01 13:28:33 -0700 | [diff] [blame] | 21 | #include <xyz/openbmc_project/Common/error.hpp> |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace network |
| 26 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 27 | namespace detail |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 28 | { |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 29 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 30 | void parseNeighbor(const NeighborFilter& filter, const nlmsghdr& hdr, |
| 31 | std::string_view msg, std::vector<NeighborInfo>& neighbors) |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 32 | { |
| 33 | if (hdr.nlmsg_type != RTM_NEWNEIGH) |
| 34 | { |
| 35 | throw std::runtime_error("Not a neighbor msg"); |
| 36 | } |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 37 | auto ndm = stdplus::raw::extract<ndmsg>(msg); |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 38 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 39 | // Filter out neighbors we don't care about |
| 40 | unsigned ifindex = ndm.ndm_ifindex; |
| 41 | if (filter.interface != 0 && filter.interface != ifindex) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 42 | { |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 43 | return; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 44 | } |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 45 | if ((ndm.ndm_state & filter.state) == 0) |
| 46 | { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Build the neighbor info for our valid neighbor |
| 51 | NeighborInfo neighbor; |
| 52 | neighbor.interface = ifindex; |
| 53 | neighbor.state = ndm.ndm_state; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 54 | bool set_addr = false; |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 55 | while (!msg.empty()) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 56 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 57 | auto [hdr, data] = netlink::extractRtAttr(msg); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 58 | if (hdr.rta_type == NDA_LLADDR) |
| 59 | { |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 60 | neighbor.mac = stdplus::raw::copyFrom<ether_addr>(data); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 61 | } |
| 62 | else if (hdr.rta_type == NDA_DST) |
| 63 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 64 | neighbor.address = addrFromBuf(ndm.ndm_family, data); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 65 | set_addr = true; |
| 66 | } |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 67 | } |
| 68 | if (!set_addr) |
| 69 | { |
| 70 | throw std::runtime_error("Missing address"); |
| 71 | } |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 72 | neighbors.push_back(std::move(neighbor)); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 73 | } |
| 74 | |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 75 | } // namespace detail |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 76 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 77 | std::vector<NeighborInfo> getCurrentNeighbors(const NeighborFilter& filter) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 78 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 79 | std::vector<NeighborInfo> neighbors; |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 80 | auto cb = [&filter, &neighbors](const nlmsghdr& hdr, std::string_view msg) { |
| 81 | detail::parseNeighbor(filter, hdr, msg, neighbors); |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 82 | }; |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 83 | ndmsg msg{}; |
| 84 | msg.ndm_ifindex = filter.interface; |
| 85 | netlink::performRequest(NETLINK_ROUTE, RTM_GETNEIGH, NLM_F_DUMP, msg, cb); |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 86 | return neighbors; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 89 | Neighbor::Neighbor(sdbusplus::bus_t& bus, const char* objPath, |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 90 | EthernetInterface& parent, const std::string& ipAddress, |
| 91 | const std::string& macAddress, State state) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 92 | NeighborObj(bus, objPath, NeighborObj::action::defer_emit), |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 93 | parent(parent) |
| 94 | { |
William A. Kennington III | 1919462 | 2022-09-06 02:13:34 -0700 | [diff] [blame] | 95 | NeighborObj::ipAddress(ipAddress); |
| 96 | NeighborObj::macAddress(macAddress); |
| 97 | NeighborObj::state(state); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 98 | |
| 99 | // Emit deferred signal. |
| 100 | emit_object_added(); |
| 101 | } |
| 102 | |
| 103 | void Neighbor::delete_() |
| 104 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 105 | parent.deleteStaticNeighborObject(ipAddress()); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 106 | } |
| 107 | |
William A. Kennington III | 8e61ca9 | 2022-09-01 13:28:33 -0700 | [diff] [blame] | 108 | using sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 109 | using REASON = |
| 110 | phosphor::logging::xyz::openbmc_project::Common::NotAllowed::REASON; |
| 111 | using phosphor::logging::elog; |
| 112 | |
| 113 | std::string Neighbor::ipAddress(std::string /*ipAddress*/) |
| 114 | { |
| 115 | elog<NotAllowed>(REASON("Property update is not allowed")); |
| 116 | } |
| 117 | |
| 118 | std::string Neighbor::macAddress(std::string /*macAddress*/) |
| 119 | { |
| 120 | elog<NotAllowed>(REASON("Property update is not allowed")); |
| 121 | } |
| 122 | |
| 123 | Neighbor::State Neighbor::state(State /*state*/) |
| 124 | { |
| 125 | elog<NotAllowed>(REASON("Property update is not allowed")); |
| 126 | } |
| 127 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 128 | } // namespace network |
| 129 | } // namespace phosphor |