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