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