1 #include "rtnetlink_server.hpp" 2 3 #include "netlink.hpp" 4 #include "network_manager.hpp" 5 #include "rtnetlink.hpp" 6 #include "types.hpp" 7 8 #include <linux/netlink.h> 9 #include <linux/rtnetlink.h> 10 #include <netinet/in.h> 11 12 #include <memory> 13 #include <phosphor-logging/log.hpp> 14 #include <stdplus/fd/create.hpp> 15 #include <stdplus/fd/ops.hpp> 16 #include <string_view> 17 18 namespace phosphor 19 { 20 namespace network 21 { 22 23 extern std::unique_ptr<Timer> refreshObjectTimer; 24 25 namespace netlink 26 { 27 28 using phosphor::logging::entry; 29 using phosphor::logging::level; 30 using phosphor::logging::log; 31 32 static bool shouldRefresh(const struct nlmsghdr& hdr, std::string_view) noexcept 33 { 34 switch (hdr.nlmsg_type) 35 { 36 case RTM_NEWLINK: 37 case RTM_DELLINK: 38 return true; 39 } 40 return false; 41 } 42 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 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 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 } 126 else 127 { 128 it->second->staticNeighbors.erase(info.addr); 129 } 130 } 131 132 static void handler(Manager& m, const nlmsghdr& hdr, std::string_view data) 133 { 134 if (shouldRefresh(hdr, data) && !refreshObjectTimer->isEnabled()) 135 { 136 refreshObjectTimer->restartOnce(refreshTimeout); 137 } 138 switch (hdr.nlmsg_type) 139 { 140 case RTM_NEWROUTE: 141 rthandler(m, true, data); 142 break; 143 case RTM_DELROUTE: 144 rthandler(m, false, data); 145 break; 146 case RTM_NEWADDR: 147 addrhandler(m, true, data); 148 break; 149 case RTM_DELADDR: 150 addrhandler(m, false, data); 151 break; 152 case RTM_NEWNEIGH: 153 neighhandler(m, true, data); 154 break; 155 case RTM_DELNEIGH: 156 neighhandler(m, false, data); 157 break; 158 } 159 } 160 161 static void eventHandler(Manager& m, sdeventplus::source::IO&, int fd, uint32_t) 162 { 163 auto cb = [&](auto&&... args) { 164 return handler(m, std::forward<decltype(args)>(args)...); 165 }; 166 while (receive(fd, cb) > 0) 167 ; 168 } 169 170 static stdplus::ManagedFd makeSock() 171 { 172 using namespace stdplus::fd; 173 174 auto sock = socket(SocketDomain::Netlink, SocketType::Raw, 175 static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE)); 176 177 sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock)); 178 179 sockaddr_nl local{}; 180 local.nl_family = AF_NETLINK; 181 local.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | 182 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; 183 bind(sock, local); 184 185 return sock; 186 } 187 188 Server::Server(sdeventplus::Event& event, Manager& manager) : 189 sock(makeSock()), 190 io(event, sock.get(), EPOLLIN | EPOLLET, [&](auto&&... args) { 191 return eventHandler(manager, std::forward<decltype(args)>(args)...); 192 }) 193 { 194 } 195 196 } // namespace netlink 197 } // namespace network 198 } // namespace phosphor 199