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 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 3 | #include <linux/netlink.h> |
| 4 | #include <linux/rtnetlink.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 5 | #include <netinet/in.h> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 6 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 7 | #include <memory> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 10 | #include <stdplus/fd/create.hpp> |
| 11 | #include <stdplus/fd/ops.hpp> |
| 12 | #include <stdplus/signal.hpp> |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 13 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 20 | |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 21 | extern std::unique_ptr<Timer> refreshObjectTimer; |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 22 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 23 | namespace rtnetlink |
| 24 | { |
| 25 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 26 | static bool shouldRefresh(const struct nlmsghdr& hdr, std::string_view data) |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 27 | { |
| 28 | switch (hdr.nlmsg_type) |
| 29 | { |
| 30 | case RTM_NEWADDR: |
| 31 | case RTM_DELADDR: |
William A. Kennington III | 00506d8 | 2019-02-01 21:51:41 -0800 | [diff] [blame] | 32 | case RTM_NEWROUTE: |
| 33 | case RTM_DELROUTE: |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 34 | { |
| 35 | return true; |
| 36 | } |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 37 | case RTM_NEWNEIGH: |
| 38 | case RTM_DELNEIGH: |
| 39 | { |
| 40 | struct ndmsg ndm; |
| 41 | if (data.size() < sizeof(ndm)) |
| 42 | { |
| 43 | return false; |
| 44 | } |
| 45 | memcpy(&ndm, data.data(), sizeof(ndm)); |
| 46 | // We only want to refresh for static neighbors |
| 47 | return ndm.ndm_state & NUD_PERMANENT; |
| 48 | } |
William A. Kennington III | d2d0594 | 2019-01-30 16:04:11 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 54 | /* Call Back for the sd event loop */ |
Manojkiran Eda | aa57fa5 | 2020-06-13 14:59:53 +0530 | [diff] [blame] | 55 | static int eventHandler(sd_event_source* /*es*/, int fd, uint32_t /*revents*/, |
| 56 | void* /*userdata*/) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 57 | { |
sureshvijayv1 | 205cc10 | 2021-09-22 13:05:22 +0530 | [diff] [blame] | 58 | std::array<char, phosphor::network::rtnetlink::BUFSIZE> buffer = {}; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 59 | int len{}; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 60 | |
sureshvijayv1 | 205cc10 | 2021-09-22 13:05:22 +0530 | [diff] [blame] | 61 | auto netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer.data()); |
| 62 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 63 | while ((len = recv(fd, netLinkHeader, phosphor::network::rtnetlink::BUFSIZE, |
| 64 | 0)) > 0) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 65 | { |
| 66 | for (; (NLMSG_OK(netLinkHeader, len)) && |
| 67 | (netLinkHeader->nlmsg_type != NLMSG_DONE); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 68 | netLinkHeader = NLMSG_NEXT(netLinkHeader, len)) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 69 | { |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 70 | std::string_view data( |
| 71 | reinterpret_cast<const char*>(NLMSG_DATA(netLinkHeader)), |
| 72 | netLinkHeader->nlmsg_len - NLMSG_HDRLEN); |
| 73 | if (shouldRefresh(*netLinkHeader, data)) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 74 | { |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 75 | // starting the timer here to make sure that we don't want |
| 76 | // create the child objects multiple times. |
William A. Kennington III | cb500dc | 2018-12-03 15:32:12 -0800 | [diff] [blame] | 77 | if (!refreshObjectTimer->isEnabled()) |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 78 | { |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 79 | // if start timer throws exception then let the application |
| 80 | // crash |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 81 | refreshObjectTimer->restartOnce(refreshTimeout); |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 82 | } // end if |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 83 | } // end if |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 84 | |
| 85 | } // end for |
| 86 | |
sureshvijayv1 | 205cc10 | 2021-09-22 13:05:22 +0530 | [diff] [blame] | 87 | buffer.fill('\0'); |
| 88 | |
| 89 | netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer.data()); |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 90 | } // end while |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 95 | static stdplus::ManagedFd makeSock() |
| 96 | { |
| 97 | using namespace stdplus::fd; |
| 98 | |
| 99 | auto sock = socket(SocketDomain::Netlink, SocketType::Raw, |
| 100 | static_cast<stdplus::fd::SocketProto>(NETLINK_ROUTE)); |
| 101 | |
| 102 | sock.fcntlSetfl(sock.fcntlGetfl().set(FileFlag::NonBlock)); |
| 103 | |
| 104 | sockaddr_nl local{}; |
| 105 | local.nl_family = AF_NETLINK; |
| 106 | local.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | |
| 107 | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_NEIGH; |
| 108 | bind(sock, local); |
| 109 | |
| 110 | return sock; |
| 111 | } |
| 112 | |
| 113 | Server::Server(EventPtr& eventPtr) : sock(makeSock()) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 114 | { |
| 115 | using namespace phosphor::logging; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 116 | using InternalFailure = |
| 117 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 118 | int r{}; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 119 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 120 | /* Let's make use of the default handler and "floating" |
| 121 | reference features of sd_event_add_signal() */ |
| 122 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 123 | stdplus::signal::block(SIGTERM); |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 124 | r = sd_event_add_signal(eventPtr.get(), NULL, SIGTERM, NULL, NULL); |
| 125 | if (r < 0) |
| 126 | { |
| 127 | goto finish; |
| 128 | } |
| 129 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 130 | stdplus::signal::block(SIGINT); |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 131 | r = sd_event_add_signal(eventPtr.get(), NULL, SIGINT, NULL, NULL); |
| 132 | if (r < 0) |
| 133 | { |
| 134 | goto finish; |
| 135 | } |
| 136 | |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 137 | r = sd_event_add_io(eventPtr.get(), nullptr, sock.get(), EPOLLIN, |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 138 | eventHandler, nullptr); |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 139 | if (r < 0) |
| 140 | { |
| 141 | goto finish; |
| 142 | } |
| 143 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 144 | finish: |
| 145 | |
| 146 | if (r < 0) |
| 147 | { |
Gunnar Mills | d75f049 | 2017-10-25 20:33:32 -0500 | [diff] [blame] | 148 | log<level::ERR>("Failure Occurred in starting of server:", |
Gunnar Mills | fc7df19 | 2017-10-19 16:11:05 -0500 | [diff] [blame] | 149 | entry("ERRNO=%d", errno)); |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 150 | elog<InternalFailure>(); |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 151 | } |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 152 | } |
| 153 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 154 | } // namespace rtnetlink |
| 155 | } // namespace network |
| 156 | } // namespace phosphor |