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 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 7 | #include <linux/netlink.h> |
| 8 | #include <linux/rtnetlink.h> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 9 | |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 11 | #include <stdplus/fd/create.hpp> |
| 12 | #include <stdplus/fd/ops.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 13 | |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 14 | namespace phosphor::network::netlink |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 15 | { |
| 16 | |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 17 | inline void rthandler(std::string_view data, auto&& cb) |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 18 | { |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 19 | auto ret = gatewayFromRtm(data); |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 20 | if (!ret) |
| 21 | { |
| 22 | return; |
| 23 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 24 | cb(std::get<unsigned>(*ret), std::get<InAddrAny>(*ret)); |
William A. Kennington III | bc2502c | 2022-11-07 16:31:54 -0800 | [diff] [blame] | 25 | } |
| 26 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 27 | 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] | 28 | { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 29 | try |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 30 | { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 31 | switch (hdr.nlmsg_type) |
| 32 | { |
William A. Kennington III | ef2b28e | 2022-11-13 18:33:10 -0800 | [diff] [blame] | 33 | case RTM_NEWLINK: |
| 34 | m.addInterface(intfFromRtm(data)); |
| 35 | break; |
| 36 | case RTM_DELLINK: |
| 37 | m.removeInterface(intfFromRtm(data)); |
| 38 | break; |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 39 | case RTM_NEWROUTE: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 40 | rthandler(data, [&](auto ifidx, auto addr) { |
| 41 | m.addDefGw(ifidx, addr); |
| 42 | }); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 43 | break; |
| 44 | case RTM_DELROUTE: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 45 | rthandler(data, [&](auto ifidx, auto addr) { |
| 46 | m.removeDefGw(ifidx, addr); |
| 47 | }); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 48 | break; |
| 49 | case RTM_NEWADDR: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 50 | m.addAddress(addrFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 51 | break; |
| 52 | case RTM_DELADDR: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 53 | m.removeAddress(addrFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 54 | break; |
| 55 | case RTM_NEWNEIGH: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 56 | m.addNeighbor(neighFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 57 | break; |
| 58 | case RTM_DELNEIGH: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 59 | m.removeNeighbor(neighFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | } |
| 63 | catch (const std::exception& e) |
| 64 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 65 | lg2::error("Failed handling netlink event: {ERROR}", "ERROR", e); |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 66 | } |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 67 | } |
| 68 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 69 | static void eventHandler(Manager& m, sdeventplus::source::IO&, int fd, uint32_t) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 70 | { |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 71 | auto cb = [&](auto&&... args) { |
| 72 | return handler(m, std::forward<decltype(args)>(args)...); |
| 73 | }; |
| 74 | while (receive(fd, cb) > 0) |
William A. Kennington III | 29418ef | 2022-11-07 22:59:47 -0800 | [diff] [blame] | 75 | ; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 76 | } |
| 77 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 78 | static 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 III | 226cada | 2022-10-28 16:33:11 -0700 | [diff] [blame] | 89 | 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] | 90 | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; |
| 91 | bind(sock, local); |
| 92 | |
| 93 | return sock; |
| 94 | } |
| 95 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 96 | Server::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 Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 101 | { |
William A. Kennington III | e1b50d5 | 2022-11-14 17:14:04 -0800 | [diff] [blame] | 102 | 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 Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 109 | } |
| 110 | |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 111 | } // namespace phosphor::network::netlink |