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 | { |
| 30 | case RTM_NEWADDR: |
| 31 | case RTM_DELADDR: |
William A. Kennington III | 00506d8 | 2019-02-01 21:51:41 -0800 | [diff] [blame] | 32 | case RTM_NEWROUTE: |
| 33 | case RTM_DELROUTE: |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 34 | { |
| 35 | return true; |
| 36 | } |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 37 | case RTM_NEWNEIGH: |
| 38 | case RTM_DELNEIGH: |
| 39 | { |
| 40 | struct ndmsg ndm; |
| 41 | if (data.size() < sizeof(ndm)) |
| 42 | { |
| 43 | return false; |
| 44 | } |
| 45 | memcpy(&ndm, data.data(), sizeof(ndm)); |
| 46 | // We only want to refresh for static neighbors |
| 47 | return ndm.ndm_state & NUD_PERMANENT; |
| 48 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 54 | static void handler(const nlmsghdr& hdr, std::string_view data) |
| 55 | { |
| 56 | if (shouldRefresh(hdr, data) && !refreshObjectTimer->isEnabled()) |
| 57 | { |
| 58 | refreshObjectTimer->restartOnce(refreshTimeout); |
| 59 | } |
| 60 | } |
| 61 | |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 62 | static void eventHandler(sdeventplus::source::IO&, int fd, uint32_t) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 63 | { |
William A. Kennington III | 29418ef | 2022-11-07 22:59:47 -0800 | [diff] [blame] | 64 | while (receive(fd, handler) > 0) |
| 65 | ; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 66 | } |
| 67 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 68 | static stdplus::ManagedFd makeSock() |
| 69 | { |
| 70 | using namespace stdplus::fd; |
| 71 | |
| 72 | auto sock = socket(SocketDomain::Netlink, SocketType::Raw, |
| 73 | static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE)); |
| 74 | |
| 75 | sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock)); |
| 76 | |
| 77 | sockaddr_nl local{}; |
| 78 | local.nl_family = AF_NETLINK; |
| 79 | local.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | |
| 80 | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; |
| 81 | bind(sock, local); |
| 82 | |
| 83 | return sock; |
| 84 | } |
| 85 | |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 86 | Server::Server(sdeventplus::Event& event) : |
| 87 | sock(makeSock()), io(event, sock.get(), EPOLLIN | EPOLLET, eventHandler) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 88 | { |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 89 | } |
| 90 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 91 | } // namespace netlink |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 92 | } // namespace network |
| 93 | } // namespace phosphor |