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