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