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