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 | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 4 | #include "network_manager.hpp" |
| 5 | #include "rtnetlink.hpp" |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 6 | #include "types.hpp" |
| 7 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 8 | #include <linux/netlink.h> |
| 9 | #include <linux/rtnetlink.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 10 | #include <netinet/in.h> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 11 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 12 | #include <memory> |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 13 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 14 | #include <stdplus/fd/create.hpp> |
| 15 | #include <stdplus/fd/ops.hpp> |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 16 | #include <string_view> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace network |
| 21 | { |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 22 | |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 23 | extern std::unique_ptr<Timer> refreshObjectTimer; |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 24 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 25 | namespace netlink |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 26 | { |
| 27 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 28 | using phosphor::logging::entry; |
| 29 | using phosphor::logging::level; |
| 30 | using phosphor::logging::log; |
| 31 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 32 | static bool shouldRefresh(const struct nlmsghdr& hdr, |
| 33 | std::string_view data) noexcept |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 34 | { |
| 35 | switch (hdr.nlmsg_type) |
| 36 | { |
William A. Kennington III | 226cada | 2022-10-28 16:33:11 -0700 | [diff] [blame] | 37 | case RTM_NEWLINK: |
| 38 | case RTM_DELLINK: |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 39 | case RTM_NEWADDR: |
| 40 | case RTM_DELADDR: |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 41 | return true; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 42 | case RTM_NEWNEIGH: |
| 43 | case RTM_DELNEIGH: |
| 44 | { |
William A. Kennington III | c8e992f | 2022-10-24 16:15:32 -0700 | [diff] [blame] | 45 | if (data.size() < sizeof(ndmsg)) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 46 | { |
| 47 | return false; |
| 48 | } |
William A. Kennington III | c8e992f | 2022-10-24 16:15:32 -0700 | [diff] [blame] | 49 | const auto& ndm = *reinterpret_cast<const ndmsg*>(data.data()); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 50 | // We only want to refresh for static neighbors |
| 51 | return ndm.ndm_state & NUD_PERMANENT; |
| 52 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 53 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 57 | static void rthandler(Manager& m, bool n, std::string_view data) |
| 58 | { |
| 59 | auto ret = netlink::gatewayFromRtm(data); |
| 60 | if (!ret) |
| 61 | { |
| 62 | return; |
| 63 | } |
| 64 | auto ifIdx = std::get<unsigned>(*ret); |
| 65 | auto it = m.interfacesByIdx.find(ifIdx); |
| 66 | if (it == m.interfacesByIdx.end()) |
| 67 | { |
| 68 | auto msg = fmt::format("Interface `{}` not found for route", ifIdx); |
| 69 | log<level::ERR>(msg.c_str(), entry("IFIDX=%u", ifIdx)); |
| 70 | return; |
| 71 | } |
| 72 | std::visit( |
| 73 | [&](auto addr) { |
| 74 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 75 | { |
| 76 | if (n) |
| 77 | { |
| 78 | it->second->EthernetInterfaceIntf::defaultGateway( |
| 79 | std::to_string(addr)); |
| 80 | } |
| 81 | else if (it->second->defaultGateway() == std::to_string(addr)) |
| 82 | { |
| 83 | it->second->EthernetInterfaceIntf::defaultGateway(""); |
| 84 | } |
| 85 | } |
| 86 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 87 | { |
| 88 | if (n) |
| 89 | { |
| 90 | it->second->EthernetInterfaceIntf::defaultGateway6( |
| 91 | std::to_string(addr)); |
| 92 | } |
| 93 | else if (it->second->defaultGateway6() == std::to_string(addr)) |
| 94 | { |
| 95 | it->second->EthernetInterfaceIntf::defaultGateway6(""); |
| 96 | } |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 101 | } |
| 102 | }, |
| 103 | std::get<InAddrAny>(*ret)); |
| 104 | } |
| 105 | |
| 106 | static void handler(Manager& m, const nlmsghdr& hdr, std::string_view data) |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 107 | { |
| 108 | if (shouldRefresh(hdr, data) && !refreshObjectTimer->isEnabled()) |
| 109 | { |
| 110 | refreshObjectTimer->restartOnce(refreshTimeout); |
| 111 | } |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 112 | switch (hdr.nlmsg_type) |
| 113 | { |
| 114 | case RTM_NEWROUTE: |
| 115 | rthandler(m, true, data); |
| 116 | break; |
| 117 | case RTM_DELROUTE: |
| 118 | rthandler(m, false, data); |
| 119 | break; |
| 120 | } |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 121 | } |
| 122 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 123 | static void eventHandler(Manager& m, sdeventplus::source::IO&, int fd, uint32_t) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 124 | { |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 125 | auto cb = [&](auto&&... args) { |
| 126 | return handler(m, std::forward<decltype(args)>(args)...); |
| 127 | }; |
| 128 | while (receive(fd, cb) > 0) |
William A. Kennington III | 29418ef | 2022-11-07 22:59:47 -0800 | [diff] [blame] | 129 | ; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 130 | } |
| 131 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 132 | static stdplus::ManagedFd makeSock() |
| 133 | { |
| 134 | using namespace stdplus::fd; |
| 135 | |
| 136 | auto sock = socket(SocketDomain::Netlink, SocketType::Raw, |
| 137 | static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE)); |
| 138 | |
| 139 | sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock)); |
| 140 | |
| 141 | sockaddr_nl local{}; |
| 142 | local.nl_family = AF_NETLINK; |
William A. Kennington III | 226cada | 2022-10-28 16:33:11 -0700 | [diff] [blame] | 143 | 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] | 144 | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; |
| 145 | bind(sock, local); |
| 146 | |
| 147 | return sock; |
| 148 | } |
| 149 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 150 | Server::Server(sdeventplus::Event& event, Manager& manager) : |
| 151 | sock(makeSock()), |
| 152 | io(event, sock.get(), EPOLLIN | EPOLLET, [&](auto&&... args) { |
| 153 | return eventHandler(manager, std::forward<decltype(args)>(args)...); |
| 154 | }) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 155 | { |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 156 | } |
| 157 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 158 | } // namespace netlink |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 159 | } // namespace network |
| 160 | } // namespace phosphor |