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