blob: 3fe997e44afd6d93269bf00014d58c036347ec2d [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
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070010#include <phosphor-logging/lg2.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 IIIed5ff472022-11-12 16:24:02 -080017inline void rthandler(std::string_view data, auto&& cb)
William A. Kennington III71590bf2022-10-31 14:14:16 -070018{
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080019 auto ret = gatewayFromRtm(data);
William A. Kennington III71590bf2022-10-31 14:14:16 -070020 if (!ret)
21 {
22 return;
23 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080024 cb(std::get<unsigned>(*ret), std::get<InAddrAny>(*ret));
William A. Kennington IIIbc2502c2022-11-07 16:31:54 -080025}
26
William A. Kennington III71590bf2022-10-31 14:14:16 -070027static void handler(Manager& m, const nlmsghdr& hdr, std::string_view data)
William A. Kennington III5f165dc2022-10-24 15:58:55 -070028{
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080029 try
William A. Kennington III71590bf2022-10-31 14:14:16 -070030 {
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080031 switch (hdr.nlmsg_type)
32 {
William A. Kennington IIIef2b28e2022-11-13 18:33:10 -080033 case RTM_NEWLINK:
34 m.addInterface(intfFromRtm(data));
35 break;
36 case RTM_DELLINK:
37 m.removeInterface(intfFromRtm(data));
38 break;
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080039 case RTM_NEWROUTE:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080040 rthandler(data, [&](auto ifidx, auto addr) {
41 m.addDefGw(ifidx, addr);
42 });
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080043 break;
44 case RTM_DELROUTE:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080045 rthandler(data, [&](auto ifidx, auto addr) {
46 m.removeDefGw(ifidx, addr);
47 });
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080048 break;
49 case RTM_NEWADDR:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080050 m.addAddress(addrFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080051 break;
52 case RTM_DELADDR:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080053 m.removeAddress(addrFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080054 break;
55 case RTM_NEWNEIGH:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080056 m.addNeighbor(neighFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080057 break;
58 case RTM_DELNEIGH:
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080059 m.removeNeighbor(neighFromRtm(data));
William A. Kennington IIIc0fdaaf2022-11-07 16:44:56 -080060 break;
61 }
62 }
63 catch (const std::exception& e)
64 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070065 lg2::error("Failed handling netlink event: {ERROR}", "ERROR", e);
William A. Kennington III71590bf2022-10-31 14:14:16 -070066 }
William A. Kennington III5f165dc2022-10-24 15:58:55 -070067}
68
William A. Kennington III71590bf2022-10-31 14:14:16 -070069static void eventHandler(Manager& m, sdeventplus::source::IO&, int fd, uint32_t)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053070{
William A. Kennington III71590bf2022-10-31 14:14:16 -070071 auto cb = [&](auto&&... args) {
72 return handler(m, std::forward<decltype(args)>(args)...);
73 };
74 while (receive(fd, cb) > 0)
William A. Kennington III29418ef2022-11-07 22:59:47 -080075 ;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053076}
77
William A. Kennington III32eef712022-01-24 17:03:03 -080078static stdplus::ManagedFd makeSock()
79{
80 using namespace stdplus::fd;
81
82 auto sock = socket(SocketDomain::Netlink, SocketType::Raw,
83 static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE));
84
85 sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock));
86
87 sockaddr_nl local{};
88 local.nl_family = AF_NETLINK;
William A. Kennington III226cada2022-10-28 16:33:11 -070089 local.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
William A. Kennington III32eef712022-01-24 17:03:03 -080090 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH;
91 bind(sock, local);
92
93 return sock;
94}
95
William A. Kennington III71590bf2022-10-31 14:14:16 -070096Server::Server(sdeventplus::Event& event, Manager& manager) :
97 sock(makeSock()),
98 io(event, sock.get(), EPOLLIN | EPOLLET, [&](auto&&... args) {
99 return eventHandler(manager, std::forward<decltype(args)>(args)...);
100 })
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530101{
William A. Kennington IIIe1b50d52022-11-14 17:14:04 -0800102 auto cb = [&](const nlmsghdr& hdr, std::string_view data) {
103 handler(manager, hdr, data);
104 };
105 performRequest(NETLINK_ROUTE, RTM_GETLINK, NLM_F_DUMP, ifinfomsg{}, cb);
106 performRequest(NETLINK_ROUTE, RTM_GETADDR, NLM_F_DUMP, ifaddrmsg{}, cb);
107 performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, rtmsg{}, cb);
108 performRequest(NETLINK_ROUTE, RTM_GETNEIGH, NLM_F_DUMP, ndmsg{}, cb);
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530109}
110
William A. Kennington III5b179382022-11-15 15:23:26 -0800111} // namespace phosphor::network::netlink