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 | |
| 16 | #include <cstring> |
| 17 | #include <stdexcept> |
| 18 | #include <string_view> |
| 19 | #include <system_error> |
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 | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 29 | void parseNeighbor(const nlmsghdr& hdr, std::string_view msg, |
| 30 | std::vector<NeighborInfo>& neighbors) |
| 31 | { |
| 32 | if (hdr.nlmsg_type != RTM_NEWNEIGH) |
| 33 | { |
| 34 | throw std::runtime_error("Not a neighbor msg"); |
| 35 | } |
| 36 | auto ndm = extract<ndmsg>(msg, "Bad neighbor msg"); |
| 37 | |
| 38 | NeighborInfo neighbor; |
| 39 | neighbor.interface.resize(IF_NAMESIZE); |
| 40 | if (if_indextoname(ndm.ndm_ifindex, neighbor.interface.data()) == nullptr) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 41 | { |
| 42 | throw std::system_error(errno, std::generic_category(), |
| 43 | "if_indextoname"); |
| 44 | } |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 45 | neighbor.interface.resize(strlen(neighbor.interface.c_str())); |
| 46 | neighbor.permanent = ndm.ndm_state & NUD_PERMANENT; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 47 | bool set_addr = false; |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 48 | while (!msg.empty()) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 49 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 50 | auto [hdr, data] = netlink::extractRtAttr(msg); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 51 | if (hdr.rta_type == NDA_LLADDR) |
| 52 | { |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame^] | 53 | neighbor.mac = copyFrom<ether_addr>(data, "Bad neighbor MAC"); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 54 | } |
| 55 | else if (hdr.rta_type == NDA_DST) |
| 56 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 57 | neighbor.address = addrFromBuf(ndm.ndm_family, data); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 58 | set_addr = true; |
| 59 | } |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 60 | } |
| 61 | if (!set_addr) |
| 62 | { |
| 63 | throw std::runtime_error("Missing address"); |
| 64 | } |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 65 | neighbors.push_back(std::move(neighbor)); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 66 | } |
| 67 | |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 68 | } // namespace detail |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 69 | |
| 70 | std::vector<NeighborInfo> getCurrentNeighbors() |
| 71 | { |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 72 | std::vector<NeighborInfo> neighbors; |
| 73 | auto cb = [&neighbors](const nlmsghdr& hdr, std::string_view msg) { |
| 74 | detail::parseNeighbor(hdr, msg, neighbors); |
| 75 | }; |
| 76 | netlink::performRequest(NETLINK_ROUTE, RTM_GETNEIGH, NLM_F_DUMP, ndmsg{}, |
| 77 | cb); |
| 78 | return neighbors; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | Neighbor::Neighbor(sdbusplus::bus::bus& bus, const char* objPath, |
| 82 | EthernetInterface& parent, const std::string& ipAddress, |
| 83 | const std::string& macAddress, State state) : |
| 84 | NeighborObj(bus, objPath, true), |
| 85 | parent(parent) |
| 86 | { |
| 87 | this->iPAddress(ipAddress); |
| 88 | this->mACAddress(macAddress); |
| 89 | this->state(state); |
| 90 | |
| 91 | // Emit deferred signal. |
| 92 | emit_object_added(); |
| 93 | } |
| 94 | |
| 95 | void Neighbor::delete_() |
| 96 | { |
| 97 | parent.deleteStaticNeighborObject(iPAddress()); |
| 98 | } |
| 99 | |
| 100 | } // namespace network |
| 101 | } // namespace phosphor |