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