blob: 717719cf25bcce0f59a54046cc261dffa3623936 [file] [log] [blame]
William A. Kennington III08505792019-01-30 16:00:04 -08001#include "config.h"
2
3#include "neighbor.hpp"
4
5#include "ethernet_interface.hpp"
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -07006#include "netlink.hpp"
William A. Kennington III08505792019-01-30 16:00:04 -08007#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 III08505792019-01-30 16:00:04 -080016#include <stdexcept>
17#include <string_view>
William A. Kennington IIId7946a72019-04-19 14:24:09 -070018#include <utility>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070019#include <vector>
William A. Kennington III08505792019-01-30 16:00:04 -080020
21namespace phosphor
22{
23namespace network
24{
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070025namespace detail
William A. Kennington III08505792019-01-30 16:00:04 -080026{
William A. Kennington III08505792019-01-30 16:00:04 -080027
William A. Kennington IIId7946a72019-04-19 14:24:09 -070028void parseNeighbor(const NeighborFilter& filter, const nlmsghdr& hdr,
29 std::string_view msg, std::vector<NeighborInfo>& neighbors)
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070030{
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 IIId7946a72019-04-19 14:24:09 -070037 // 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 III08505792019-01-30 16:00:04 -080040 {
William A. Kennington IIId7946a72019-04-19 14:24:09 -070041 return;
William A. Kennington III08505792019-01-30 16:00:04 -080042 }
William A. Kennington IIId7946a72019-04-19 14:24:09 -070043 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 III08505792019-01-30 16:00:04 -080052 bool set_addr = false;
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070053 while (!msg.empty())
William A. Kennington III08505792019-01-30 16:00:04 -080054 {
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070055 auto [hdr, data] = netlink::extractRtAttr(msg);
William A. Kennington III08505792019-01-30 16:00:04 -080056 if (hdr.rta_type == NDA_LLADDR)
57 {
William A. Kennington III6ca08d82019-04-20 16:04:18 -070058 neighbor.mac = copyFrom<ether_addr>(data, "Bad neighbor MAC");
William A. Kennington III08505792019-01-30 16:00:04 -080059 }
60 else if (hdr.rta_type == NDA_DST)
61 {
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070062 neighbor.address = addrFromBuf(ndm.ndm_family, data);
William A. Kennington III08505792019-01-30 16:00:04 -080063 set_addr = true;
64 }
William A. Kennington III08505792019-01-30 16:00:04 -080065 }
66 if (!set_addr)
67 {
68 throw std::runtime_error("Missing address");
69 }
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070070 neighbors.push_back(std::move(neighbor));
William A. Kennington III08505792019-01-30 16:00:04 -080071}
72
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070073} // namespace detail
William A. Kennington III08505792019-01-30 16:00:04 -080074
William A. Kennington IIId7946a72019-04-19 14:24:09 -070075std::vector<NeighborInfo> getCurrentNeighbors(const NeighborFilter& filter)
William A. Kennington III08505792019-01-30 16:00:04 -080076{
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070077 std::vector<NeighborInfo> neighbors;
William A. Kennington IIId7946a72019-04-19 14:24:09 -070078 auto cb = [&filter, &neighbors](const nlmsghdr& hdr, std::string_view msg) {
79 detail::parseNeighbor(filter, hdr, msg, neighbors);
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070080 };
William A. Kennington IIId7946a72019-04-19 14:24:09 -070081 ndmsg msg{};
82 msg.ndm_ifindex = filter.interface;
83 netlink::performRequest(NETLINK_ROUTE, RTM_GETNEIGH, NLM_F_DUMP, msg, cb);
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070084 return neighbors;
William A. Kennington III08505792019-01-30 16:00:04 -080085}
86
87Neighbor::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
101void Neighbor::delete_()
102{
103 parent.deleteStaticNeighborObject(iPAddress());
104}
105
106} // namespace network
107} // namespace phosphor