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