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 | return true; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 40 | case RTM_NEWNEIGH: |
| 41 | case RTM_DELNEIGH: |
| 42 | { |
William A. Kennington III | c8e992f | 2022-10-24 16:15:32 -0700 | [diff] [blame] | 43 | if (data.size() < sizeof(ndmsg)) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 44 | { |
| 45 | return false; |
| 46 | } |
William A. Kennington III | c8e992f | 2022-10-24 16:15:32 -0700 | [diff] [blame] | 47 | const auto& ndm = *reinterpret_cast<const ndmsg*>(data.data()); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 48 | // We only want to refresh for static neighbors |
| 49 | return ndm.ndm_state & NUD_PERMANENT; |
| 50 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 51 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 55 | static void rthandler(Manager& m, bool n, std::string_view data) |
| 56 | { |
| 57 | auto ret = netlink::gatewayFromRtm(data); |
| 58 | if (!ret) |
| 59 | { |
| 60 | return; |
| 61 | } |
| 62 | auto ifIdx = std::get<unsigned>(*ret); |
| 63 | auto it = m.interfacesByIdx.find(ifIdx); |
| 64 | if (it == m.interfacesByIdx.end()) |
| 65 | { |
| 66 | auto msg = fmt::format("Interface `{}` not found for route", ifIdx); |
| 67 | log<level::ERR>(msg.c_str(), entry("IFIDX=%u", ifIdx)); |
| 68 | return; |
| 69 | } |
| 70 | std::visit( |
| 71 | [&](auto addr) { |
| 72 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 73 | { |
| 74 | if (n) |
| 75 | { |
| 76 | it->second->EthernetInterfaceIntf::defaultGateway( |
| 77 | std::to_string(addr)); |
| 78 | } |
| 79 | else if (it->second->defaultGateway() == std::to_string(addr)) |
| 80 | { |
| 81 | it->second->EthernetInterfaceIntf::defaultGateway(""); |
| 82 | } |
| 83 | } |
| 84 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 85 | { |
| 86 | if (n) |
| 87 | { |
| 88 | it->second->EthernetInterfaceIntf::defaultGateway6( |
| 89 | std::to_string(addr)); |
| 90 | } |
| 91 | else if (it->second->defaultGateway6() == std::to_string(addr)) |
| 92 | { |
| 93 | it->second->EthernetInterfaceIntf::defaultGateway6(""); |
| 94 | } |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 99 | } |
| 100 | }, |
| 101 | std::get<InAddrAny>(*ret)); |
| 102 | } |
| 103 | |
William A. Kennington III | d6f5340 | 2022-11-07 14:48:53 -0800 | [diff] [blame^] | 104 | static void addrhandler(Manager& m, bool n, std::string_view data) |
| 105 | { |
| 106 | auto info = netlink::addrFromRtm(data); |
| 107 | auto it = m.interfacesByIdx.find(info.ifidx); |
| 108 | if (it == m.interfacesByIdx.end()) |
| 109 | { |
| 110 | auto msg = fmt::format("Interface `{}` not found for addr", info.ifidx); |
| 111 | log<level::ERR>(msg.c_str(), entry("IFIDX=%u", info.ifidx)); |
| 112 | return; |
| 113 | } |
| 114 | if (n) |
| 115 | { |
| 116 | it->second->addAddr(info); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | it->second->addrs.erase(info.ifaddr); |
| 121 | } |
| 122 | } |
| 123 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 124 | 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] | 125 | { |
| 126 | if (shouldRefresh(hdr, data) && !refreshObjectTimer->isEnabled()) |
| 127 | { |
| 128 | refreshObjectTimer->restartOnce(refreshTimeout); |
| 129 | } |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 130 | switch (hdr.nlmsg_type) |
| 131 | { |
| 132 | case RTM_NEWROUTE: |
| 133 | rthandler(m, true, data); |
| 134 | break; |
| 135 | case RTM_DELROUTE: |
| 136 | rthandler(m, false, data); |
| 137 | break; |
William A. Kennington III | d6f5340 | 2022-11-07 14:48:53 -0800 | [diff] [blame^] | 138 | case RTM_NEWADDR: |
| 139 | addrhandler(m, true, data); |
| 140 | break; |
| 141 | case RTM_DELADDR: |
| 142 | addrhandler(m, false, data); |
| 143 | break; |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 144 | } |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 145 | } |
| 146 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 147 | static void eventHandler(Manager& m, sdeventplus::source::IO&, int fd, uint32_t) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 148 | { |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 149 | auto cb = [&](auto&&... args) { |
| 150 | return handler(m, std::forward<decltype(args)>(args)...); |
| 151 | }; |
| 152 | while (receive(fd, cb) > 0) |
William A. Kennington III | 29418ef | 2022-11-07 22:59:47 -0800 | [diff] [blame] | 153 | ; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 154 | } |
| 155 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 156 | static stdplus::ManagedFd makeSock() |
| 157 | { |
| 158 | using namespace stdplus::fd; |
| 159 | |
| 160 | auto sock = socket(SocketDomain::Netlink, SocketType::Raw, |
| 161 | static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE)); |
| 162 | |
| 163 | sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock)); |
| 164 | |
| 165 | sockaddr_nl local{}; |
| 166 | local.nl_family = AF_NETLINK; |
William A. Kennington III | 226cada | 2022-10-28 16:33:11 -0700 | [diff] [blame] | 167 | 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] | 168 | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; |
| 169 | bind(sock, local); |
| 170 | |
| 171 | return sock; |
| 172 | } |
| 173 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 174 | Server::Server(sdeventplus::Event& event, Manager& manager) : |
| 175 | sock(makeSock()), |
| 176 | io(event, sock.get(), EPOLLIN | EPOLLET, [&](auto&&... args) { |
| 177 | return eventHandler(manager, std::forward<decltype(args)>(args)...); |
| 178 | }) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 179 | { |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 180 | } |
| 181 | |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 182 | } // namespace netlink |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 183 | } // namespace network |
| 184 | } // namespace phosphor |