Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 1 | #include "rtnetlink_server.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 3 | #include "netlink.hpp" |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 4 | #include "types.hpp" |
| 5 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 6 | #include <linux/netlink.h> |
| 7 | #include <linux/rtnetlink.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include <netinet/in.h> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 9 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 10 | #include <memory> |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 11 | #include <stdplus/fd/create.hpp> |
| 12 | #include <stdplus/fd/ops.hpp> |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 13 | #include <string_view> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 19 | |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 20 | extern std::unique_ptr<Timer> refreshObjectTimer; |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 21 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 22 | namespace netlink |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 23 | { |
| 24 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 25 | static bool shouldRefresh(const struct nlmsghdr& hdr, |
| 26 | std::string_view data) noexcept |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 27 | { |
| 28 | switch (hdr.nlmsg_type) |
| 29 | { |
William A. Kennington III | 226cada | 2022-10-28 16:33:11 -0700 | [diff] [blame] | 30 | case RTM_NEWLINK: |
| 31 | case RTM_DELLINK: |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 32 | case RTM_NEWADDR: |
| 33 | case RTM_DELADDR: |
William A. Kennington III | 00506d8 | 2019-02-01 21:51:41 -0800 | [diff] [blame] | 34 | case RTM_NEWROUTE: |
| 35 | case RTM_DELROUTE: |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 36 | return true; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 37 | case RTM_NEWNEIGH: |
| 38 | case RTM_DELNEIGH: |
| 39 | { |
William A. Kennington III | c8e992f | 2022-10-24 16:15:32 -0700 | [diff] [blame] | 40 | if (data.size() < sizeof(ndmsg)) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 41 | { |
| 42 | return false; |
| 43 | } |
William A. Kennington III | c8e992f | 2022-10-24 16:15:32 -0700 | [diff] [blame] | 44 | const auto& ndm = *reinterpret_cast<const ndmsg*>(data.data()); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 45 | // We only want to refresh for static neighbors |
| 46 | return ndm.ndm_state & NUD_PERMANENT; |
| 47 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 48 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 52 | static 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 III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 60 | static void eventHandler(sdeventplus::source::IO&, int fd, uint32_t) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 61 | { |
William A. Kennington III | 29418ef | 2022-11-07 22:59:47 -0800 | [diff] [blame] | 62 | while (receive(fd, handler) > 0) |
| 63 | ; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 64 | } |
| 65 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 66 | static 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 III | 226cada | 2022-10-28 16:33:11 -0700 | [diff] [blame] | 77 | local.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 78 | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; |
| 79 | bind(sock, local); |
| 80 | |
| 81 | return sock; |
| 82 | } |
| 83 | |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 84 | Server::Server(sdeventplus::Event& event) : |
| 85 | sock(makeSock()), io(event, sock.get(), EPOLLIN | EPOLLET, eventHandler) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 86 | { |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 87 | } |
| 88 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 89 | } // namespace netlink |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 90 | } // namespace network |
| 91 | } // namespace phosphor |