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