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