blob: a4266b7baa2e629fd4a9cf915a401cd0cec13898 [file] [log] [blame]
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05301#include "rtnetlink_server.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
William A. Kennington III5f165dc2022-10-24 15:58:55 -07003#include "netlink.hpp"
William A. Kennington III71590bf2022-10-31 14:14:16 -07004#include "network_manager.hpp"
5#include "rtnetlink.hpp"
William A. Kennington III217bb3f2022-09-29 13:43:13 -07006
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05307#include <linux/netlink.h>
8#include <linux/rtnetlink.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05309
William A. Kennington III71590bf2022-10-31 14:14:16 -070010#include <phosphor-logging/log.hpp>
William A. Kennington III32eef712022-01-24 17:03:03 -080011#include <stdplus/fd/create.hpp>
12#include <stdplus/fd/ops.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053013
William A. Kennington III5b179382022-11-15 15:23:26 -080014namespace phosphor::network::netlink
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053015{
16
William A. Kennington III71590bf2022-10-31 14:14:16 -070017using phosphor::logging::entry;
18using phosphor::logging::level;
19using phosphor::logging::log;
20
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080021inline void rthandler(std::string_view data, auto&& cb)
William A. Kennington III71590bf2022-10-31 14:14:16 -070022{
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080023 auto ret = gatewayFromRtm(data);
William A. Kennington III71590bf2022-10-31 14:14:16 -070024 if (!ret)
25 {
26 return;
27 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080028 cb(std::get<unsigned>(*ret), std::get<InAddrAny>(*ret));
William A. Kennington IIIbc2502c2022-11-07 16:31:54 -080029}
30
William A. Kennington III71590bf2022-10-31 14:14:16 -070031static void handler(Manager& m, const nlmsghdr& hdr, std::string_view data)
William A. Kennington III5f165dc2022-10-24 15:58:55 -070032{
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080033 try
William A. Kennington III71590bf2022-10-31 14:14:16 -070034 {
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080035 switch (hdr.nlmsg_type)
36 {
William A. Kennington IIIef2b28e2022-11-13 18:33:10 -080037 case RTM_NEWLINK:
38 m.addInterface(intfFromRtm(data));
39 break;
40 case RTM_DELLINK:
41 m.removeInterface(intfFromRtm(data));
42 break;
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080043 case RTM_NEWROUTE:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080044 rthandler(data, [&](auto ifidx, auto addr) {
45 m.addDefGw(ifidx, addr);
46 });
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080047 break;
48 case RTM_DELROUTE:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080049 rthandler(data, [&](auto ifidx, auto addr) {
50 m.removeDefGw(ifidx, addr);
51 });
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080052 break;
53 case RTM_NEWADDR:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080054 m.addAddress(addrFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080055 break;
56 case RTM_DELADDR:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080057 m.removeAddress(addrFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080058 break;
59 case RTM_NEWNEIGH:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080060 m.addNeighbor(neighFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080061 break;
62 case RTM_DELNEIGH:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080063 m.removeNeighbor(neighFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080064 break;
65 }
66 }
67 catch (const std::exception& e)
68 {
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080069 auto msg = fmt::format("Failed handling netlink event: {}", e.what());
70 log<level::ERR>(msg.c_str(), entry("ERROR=%s", e.what()));
William A. Kennington III71590bf2022-10-31 14:14:16 -070071 }
William A. Kennington III5f165dc2022-10-24 15:58:55 -070072}
73
William A. Kennington III71590bf2022-10-31 14:14:16 -070074static void eventHandler(Manager& m, sdeventplus::source::IO&, int fd, uint32_t)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053075{
William A. Kennington III71590bf2022-10-31 14:14:16 -070076 auto cb = [&](auto&&... args) {
77 return handler(m, std::forward<decltype(args)>(args)...);
78 };
79 while (receive(fd, cb) > 0)
William A. Kennington III29418ef2022-11-07 22:59:47 -080080 ;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053081}
82
William A. Kennington III32eef712022-01-24 17:03:03 -080083static stdplus::ManagedFd makeSock()
84{
85 using namespace stdplus::fd;
86
87 auto sock = socket(SocketDomain::Netlink, SocketType::Raw,
88 static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE));
89
90 sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock));
91
92 sockaddr_nl local{};
93 local.nl_family = AF_NETLINK;
William A. Kennington III226cada2022-10-28 16:33:11 -070094 local.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
William A. Kennington III32eef712022-01-24 17:03:03 -080095 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH;
96 bind(sock, local);
97
98 return sock;
99}
100
William A. Kennington III71590bf2022-10-31 14:14:16 -0700101Server::Server(sdeventplus::Event& event, Manager& manager) :
102 sock(makeSock()),
103 io(event, sock.get(), EPOLLIN | EPOLLET, [&](auto&&... args) {
104 return eventHandler(manager, std::forward<decltype(args)>(args)...);
105 })
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530106{
William A. Kennington IIIe1b50d52022-11-14 17:14:04 -0800107 auto cb = [&](const nlmsghdr& hdr, std::string_view data) {
108 handler(manager, hdr, data);
109 };
110 performRequest(NETLINK_ROUTE, RTM_GETLINK, NLM_F_DUMP, ifinfomsg{}, cb);
111 performRequest(NETLINK_ROUTE, RTM_GETADDR, NLM_F_DUMP, ifaddrmsg{}, cb);
112 performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, rtmsg{}, cb);
113 performRequest(NETLINK_ROUTE, RTM_GETNEIGH, NLM_F_DUMP, ndmsg{}, cb);
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530114}
115
William A. Kennington III5b179382022-11-15 15:23:26 -0800116} // namespace phosphor::network::netlink