blob: 185589e0affe5da46615bdc0819a933264dce43b [file] [log] [blame]
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05301#include "rtnetlink_server.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
William A. Kennington III5f165dc2022-10-24 15:58:55 -07003#include "netlink.hpp"
William A. Kennington III217bb3f2022-09-29 13:43:13 -07004#include "types.hpp"
5
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05306#include <linux/netlink.h>
7#include <linux/rtnetlink.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <netinet/in.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05309
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053010#include <memory>
William A. Kennington III32eef712022-01-24 17:03:03 -080011#include <stdplus/fd/create.hpp>
12#include <stdplus/fd/ops.hpp>
William A. Kennington III08505792019-01-30 16:00:04 -080013#include <string_view>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053014
15namespace phosphor
16{
17namespace network
18{
Ratan Guptaa54d8f82017-09-08 17:05:46 +053019
William A. Kennington III3a70fa22018-09-20 18:48:20 -070020extern std::unique_ptr<Timer> refreshObjectTimer;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053021
William A. Kennington III5f165dc2022-10-24 15:58:55 -070022namespace netlink
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053023{
24
William A. Kennington III5f165dc2022-10-24 15:58:55 -070025static bool shouldRefresh(const struct nlmsghdr& hdr,
26 std::string_view data) noexcept
William A. Kennington IIId2d05942019-01-30 16:04:11 -080027{
28 switch (hdr.nlmsg_type)
29 {
William A. Kennington III226cada2022-10-28 16:33:11 -070030 case RTM_NEWLINK:
31 case RTM_DELLINK:
William A. Kennington IIId2d05942019-01-30 16:04:11 -080032 case RTM_NEWADDR:
33 case RTM_DELADDR:
William A. Kennington III00506d82019-02-01 21:51:41 -080034 case RTM_NEWROUTE:
35 case RTM_DELROUTE:
William A. Kennington IIId2d05942019-01-30 16:04:11 -080036 return true;
William A. Kennington III08505792019-01-30 16:00:04 -080037 case RTM_NEWNEIGH:
38 case RTM_DELNEIGH:
39 {
William A. Kennington IIIc8e992f2022-10-24 16:15:32 -070040 if (data.size() < sizeof(ndmsg))
William A. Kennington III08505792019-01-30 16:00:04 -080041 {
42 return false;
43 }
William A. Kennington IIIc8e992f2022-10-24 16:15:32 -070044 const auto& ndm = *reinterpret_cast<const ndmsg*>(data.data());
William A. Kennington III08505792019-01-30 16:00:04 -080045 // We only want to refresh for static neighbors
46 return ndm.ndm_state & NUD_PERMANENT;
47 }
William A. Kennington IIId2d05942019-01-30 16:04:11 -080048 }
William A. Kennington IIId2d05942019-01-30 16:04:11 -080049 return false;
50}
51
William A. Kennington III5f165dc2022-10-24 15:58:55 -070052static void handler(const nlmsghdr& hdr, std::string_view data)
53{
54 if (shouldRefresh(hdr, data) && !refreshObjectTimer->isEnabled())
55 {
56 refreshObjectTimer->restartOnce(refreshTimeout);
57 }
58}
59
William A. Kennington III217bb3f2022-09-29 13:43:13 -070060static void eventHandler(sdeventplus::source::IO&, int fd, uint32_t)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053061{
William A. Kennington III29418ef2022-11-07 22:59:47 -080062 while (receive(fd, handler) > 0)
63 ;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053064}
65
William A. Kennington III32eef712022-01-24 17:03:03 -080066static stdplus::ManagedFd makeSock()
67{
68 using namespace stdplus::fd;
69
70 auto sock = socket(SocketDomain::Netlink, SocketType::Raw,
71 static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE));
72
73 sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock));
74
75 sockaddr_nl local{};
76 local.nl_family = AF_NETLINK;
William A. Kennington III226cada2022-10-28 16:33:11 -070077 local.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
William A. Kennington III32eef712022-01-24 17:03:03 -080078 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH;
79 bind(sock, local);
80
81 return sock;
82}
83
William A. Kennington III217bb3f2022-09-29 13:43:13 -070084Server::Server(sdeventplus::Event& event) :
85 sock(makeSock()), io(event, sock.get(), EPOLLIN | EPOLLET, eventHandler)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053086{
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053087}
88
William A. Kennington III5f165dc2022-10-24 15:58:55 -070089} // namespace netlink
Gunnar Mills57d9c502018-09-14 14:42:34 -050090} // namespace network
91} // namespace phosphor