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 | 9b2a20d | 2023-06-17 14:05:48 -0700 | [diff] [blame] | 24 | cb(std::get<unsigned>(*ret), std::get<stdplus::InAnyAddr>(*ret)); |
William A. Kennington III | bc2502c | 2022-11-07 16:31:54 -0800 | [diff] [blame] | 25 | } |
| 26 | |
William A. Kennington III | 0d76382 | 2023-02-27 16:37:31 -0800 | [diff] [blame] | 27 | static unsigned getIfIdx(const nlmsghdr& hdr, std::string_view data) |
| 28 | { |
| 29 | switch (hdr.nlmsg_type) |
| 30 | { |
| 31 | case RTM_NEWLINK: |
| 32 | case RTM_DELLINK: |
| 33 | return extractRtData<ifinfomsg>(data).ifi_index; |
| 34 | case RTM_NEWADDR: |
| 35 | case RTM_DELADDR: |
| 36 | return extractRtData<ifaddrmsg>(data).ifa_index; |
| 37 | case RTM_NEWNEIGH: |
| 38 | case RTM_DELNEIGH: |
| 39 | return extractRtData<ndmsg>(data).ndm_ifindex; |
| 40 | } |
| 41 | throw std::runtime_error("Unknown nlmsg_type"); |
| 42 | } |
| 43 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 44 | 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] | 45 | { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 46 | try |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 47 | { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 48 | switch (hdr.nlmsg_type) |
| 49 | { |
William A. Kennington III | ef2b28e | 2022-11-13 18:33:10 -0800 | [diff] [blame] | 50 | case RTM_NEWLINK: |
| 51 | m.addInterface(intfFromRtm(data)); |
| 52 | break; |
| 53 | case RTM_DELLINK: |
| 54 | m.removeInterface(intfFromRtm(data)); |
| 55 | break; |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 56 | case RTM_NEWROUTE: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 57 | rthandler(data, [&](auto ifidx, auto addr) { |
| 58 | m.addDefGw(ifidx, addr); |
| 59 | }); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 60 | break; |
| 61 | case RTM_DELROUTE: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 62 | rthandler(data, [&](auto ifidx, auto addr) { |
| 63 | m.removeDefGw(ifidx, addr); |
| 64 | }); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 65 | break; |
| 66 | case RTM_NEWADDR: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 67 | m.addAddress(addrFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 68 | break; |
| 69 | case RTM_DELADDR: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 70 | m.removeAddress(addrFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 71 | break; |
| 72 | case RTM_NEWNEIGH: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 73 | m.addNeighbor(neighFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 74 | break; |
| 75 | case RTM_DELNEIGH: |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 76 | m.removeNeighbor(neighFromRtm(data)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | } |
| 80 | catch (const std::exception& e) |
| 81 | { |
William A. Kennington III | 0d76382 | 2023-02-27 16:37:31 -0800 | [diff] [blame] | 82 | try |
| 83 | { |
| 84 | if (m.ignoredIntf.contains(getIfIdx(hdr, data))) |
| 85 | { |
| 86 | // We don't want to log errors for ignored interfaces |
| 87 | return; |
| 88 | } |
| 89 | } |
| 90 | catch (...) |
| 91 | {} |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 92 | lg2::error("Failed handling netlink event: {ERROR}", "ERROR", e); |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 93 | } |
William A. Kennington III | 5f165dc | 2022-10-24 15:58:55 -0700 | [diff] [blame] | 94 | } |
| 95 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 96 | static void eventHandler(Manager& m, sdeventplus::source::IO&, int fd, uint32_t) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 97 | { |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 98 | auto cb = [&](auto&&... args) { |
| 99 | return handler(m, std::forward<decltype(args)>(args)...); |
| 100 | }; |
| 101 | while (receive(fd, cb) > 0) |
William A. Kennington III | 29418ef | 2022-11-07 22:59:47 -0800 | [diff] [blame] | 102 | ; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 103 | } |
| 104 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 105 | static stdplus::ManagedFd makeSock() |
| 106 | { |
| 107 | using namespace stdplus::fd; |
| 108 | |
| 109 | auto sock = socket(SocketDomain::Netlink, SocketType::Raw, |
| 110 | static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE)); |
| 111 | |
| 112 | sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock)); |
| 113 | |
| 114 | sockaddr_nl local{}; |
| 115 | local.nl_family = AF_NETLINK; |
William A. Kennington III | 226cada | 2022-10-28 16:33:11 -0700 | [diff] [blame] | 116 | 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] | 117 | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; |
| 118 | bind(sock, local); |
| 119 | |
| 120 | return sock; |
| 121 | } |
| 122 | |
William A. Kennington III | 71590bf | 2022-10-31 14:14:16 -0700 | [diff] [blame] | 123 | Server::Server(sdeventplus::Event& event, Manager& manager) : |
| 124 | sock(makeSock()), |
| 125 | io(event, sock.get(), EPOLLIN | EPOLLET, [&](auto&&... args) { |
Patrick Williams | 6f256bc | 2023-10-20 11:18:34 -0500 | [diff] [blame^] | 126 | return eventHandler(manager, std::forward<decltype(args)>(args)...); |
| 127 | }) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 128 | { |
William A. Kennington III | e1b50d5 | 2022-11-14 17:14:04 -0800 | [diff] [blame] | 129 | auto cb = [&](const nlmsghdr& hdr, std::string_view data) { |
| 130 | handler(manager, hdr, data); |
| 131 | }; |
| 132 | performRequest(NETLINK_ROUTE, RTM_GETLINK, NLM_F_DUMP, ifinfomsg{}, cb); |
| 133 | performRequest(NETLINK_ROUTE, RTM_GETADDR, NLM_F_DUMP, ifaddrmsg{}, cb); |
| 134 | performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, rtmsg{}, cb); |
| 135 | performRequest(NETLINK_ROUTE, RTM_GETNEIGH, NLM_F_DUMP, ndmsg{}, cb); |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 136 | } |
| 137 | |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 138 | } // namespace phosphor::network::netlink |