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 | 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 | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace network |
| 25 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 26 | namespace detail |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 27 | { |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 28 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 29 | void parseNeighbor(const NeighborFilter& filter, const nlmsghdr& hdr, |
| 30 | std::string_view msg, std::vector<NeighborInfo>& neighbors) |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 31 | { |
| 32 | if (hdr.nlmsg_type != RTM_NEWNEIGH) |
| 33 | { |
| 34 | throw std::runtime_error("Not a neighbor msg"); |
| 35 | } |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 36 | auto ndm = stdplus::raw::extract<ndmsg>(msg); |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 37 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 38 | // Filter out neighbors we don't care about |
| 39 | unsigned ifindex = ndm.ndm_ifindex; |
| 40 | if (filter.interface != 0 && filter.interface != ifindex) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 41 | { |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 42 | return; |
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 | if ((ndm.ndm_state & filter.state) == 0) |
| 45 | { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Build the neighbor info for our valid neighbor |
| 50 | NeighborInfo neighbor; |
| 51 | neighbor.interface = ifindex; |
| 52 | neighbor.state = ndm.ndm_state; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 53 | bool set_addr = false; |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 54 | while (!msg.empty()) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 55 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 56 | auto [hdr, data] = netlink::extractRtAttr(msg); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 57 | if (hdr.rta_type == NDA_LLADDR) |
| 58 | { |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 59 | neighbor.mac = stdplus::raw::copyFrom<ether_addr>(data); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 60 | } |
| 61 | else if (hdr.rta_type == NDA_DST) |
| 62 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 63 | neighbor.address = addrFromBuf(ndm.ndm_family, data); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 64 | set_addr = true; |
| 65 | } |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 66 | } |
| 67 | if (!set_addr) |
| 68 | { |
| 69 | throw std::runtime_error("Missing address"); |
| 70 | } |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 71 | neighbors.push_back(std::move(neighbor)); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 72 | } |
| 73 | |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 74 | } // namespace detail |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 75 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 76 | std::vector<NeighborInfo> getCurrentNeighbors(const NeighborFilter& filter) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 77 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 78 | std::vector<NeighborInfo> neighbors; |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 79 | auto cb = [&filter, &neighbors](const nlmsghdr& hdr, std::string_view msg) { |
| 80 | detail::parseNeighbor(filter, hdr, msg, neighbors); |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 81 | }; |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 82 | ndmsg msg{}; |
| 83 | msg.ndm_ifindex = filter.interface; |
| 84 | 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] | 85 | return neighbors; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Neighbor::Neighbor(sdbusplus::bus::bus& bus, const char* objPath, |
| 89 | EthernetInterface& parent, const std::string& ipAddress, |
| 90 | const std::string& macAddress, State state) : |
| 91 | NeighborObj(bus, objPath, true), |
| 92 | parent(parent) |
| 93 | { |
| 94 | this->iPAddress(ipAddress); |
| 95 | this->mACAddress(macAddress); |
| 96 | this->state(state); |
| 97 | |
| 98 | // Emit deferred signal. |
| 99 | emit_object_added(); |
| 100 | } |
| 101 | |
| 102 | void Neighbor::delete_() |
| 103 | { |
| 104 | parent.deleteStaticNeighborObject(iPAddress()); |
| 105 | } |
| 106 | |
| 107 | } // namespace network |
| 108 | } // namespace phosphor |