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 | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame^] | 5 | #include "network_manager.hpp" |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 6 | #include "util.hpp" |
| 7 | |
| 8 | #include <linux/neighbour.h> |
| 9 | #include <linux/netlink.h> |
| 10 | #include <linux/rtnetlink.h> |
| 11 | #include <net/if.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <sys/types.h> |
| 14 | |
William A. Kennington III | 8e61ca9 | 2022-09-01 13:28:33 -0700 | [diff] [blame] | 15 | #include <phosphor-logging/elog-errors.hpp> |
| 16 | #include <phosphor-logging/elog.hpp> |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 17 | #include <stdexcept> |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 18 | #include <stdplus/raw.hpp> |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 19 | #include <string> |
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 | f1289a0 | 2022-10-24 18:30:36 -0700 | [diff] [blame] | 39 | const auto& ndm = netlink::extractRtData<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 | |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame^] | 91 | static auto makeObjPath(std::string_view root, InAddrAny addr) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 92 | { |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame^] | 93 | auto ret = sdbusplus::message::object_path(std::string(root)); |
| 94 | ret /= std::to_string(addr); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | Neighbor::Neighbor(sdbusplus::bus_t& bus, std::string_view objRoot, |
| 99 | EthernetInterface& parent, InAddrAny addr, ether_addr lladdr, |
| 100 | State state) : |
| 101 | Neighbor(bus, makeObjPath(objRoot, addr), parent, addr, lladdr, state) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | Neighbor::Neighbor(sdbusplus::bus_t& bus, |
| 106 | sdbusplus::message::object_path objPath, |
| 107 | EthernetInterface& parent, InAddrAny addr, ether_addr lladdr, |
| 108 | State state) : |
| 109 | NeighborObj(bus, objPath.str.c_str(), NeighborObj::action::defer_emit), |
| 110 | parent(parent), objPath(std::move(objPath)) |
| 111 | { |
| 112 | NeighborObj::ipAddress(std::to_string(addr)); |
| 113 | NeighborObj::macAddress(std::to_string(lladdr)); |
William A. Kennington III | 1919462 | 2022-09-06 02:13:34 -0700 | [diff] [blame] | 114 | NeighborObj::state(state); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 115 | |
| 116 | // Emit deferred signal. |
| 117 | emit_object_added(); |
| 118 | } |
| 119 | |
| 120 | void Neighbor::delete_() |
| 121 | { |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame^] | 122 | auto& neighbors = parent.staticNeighbors; |
| 123 | std::unique_ptr<Neighbor> ptr; |
| 124 | for (auto it = neighbors.begin(); it != neighbors.end(); ++it) |
| 125 | { |
| 126 | if (it->second.get() == this) |
| 127 | { |
| 128 | ptr = std::move(it->second); |
| 129 | neighbors.erase(it); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | parent.writeConfigurationFile(); |
| 135 | parent.manager.reloadConfigs(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 136 | } |
| 137 | |
William A. Kennington III | 8e61ca9 | 2022-09-01 13:28:33 -0700 | [diff] [blame] | 138 | using sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 139 | using REASON = |
| 140 | phosphor::logging::xyz::openbmc_project::Common::NotAllowed::REASON; |
| 141 | using phosphor::logging::elog; |
| 142 | |
| 143 | std::string Neighbor::ipAddress(std::string /*ipAddress*/) |
| 144 | { |
| 145 | elog<NotAllowed>(REASON("Property update is not allowed")); |
| 146 | } |
| 147 | |
| 148 | std::string Neighbor::macAddress(std::string /*macAddress*/) |
| 149 | { |
| 150 | elog<NotAllowed>(REASON("Property update is not allowed")); |
| 151 | } |
| 152 | |
| 153 | Neighbor::State Neighbor::state(State /*state*/) |
| 154 | { |
| 155 | elog<NotAllowed>(REASON("Property update is not allowed")); |
| 156 | } |
| 157 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 158 | } // namespace network |
| 159 | } // namespace phosphor |