blob: ed5f8c97aa7001e33766ba8b61468d9d5de4e6ad [file] [log] [blame]
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05301#include "rtnetlink_server.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05303#include "types.hpp"
4#include "util.hpp"
5
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05306#include <linux/netlink.h>
7#include <linux/rtnetlink.h>
8#include <net/if.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07009#include <netinet/in.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053010#include <sys/types.h>
11#include <systemd/sd-daemon.h>
12#include <unistd.h>
13
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053014#include <memory>
Patrick Venture189d44e2018-07-09 12:30:59 -070015#include <phosphor-logging/elog-errors.hpp>
16#include <phosphor-logging/log.hpp>
17#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053018
19namespace phosphor
20{
21namespace network
22{
Ratan Guptaa54d8f82017-09-08 17:05:46 +053023
William A. Kennington III3a70fa22018-09-20 18:48:20 -070024extern std::unique_ptr<Timer> refreshObjectTimer;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053025
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053026namespace rtnetlink
27{
28
29/* Call Back for the sd event loop */
30static int eventHandler(sd_event_source* es, int fd, uint32_t revents,
31 void* userdata)
32{
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 char buffer[phosphor::network::rtnetlink::BUFSIZE]{};
34 int len{};
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053035
36 auto netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer);
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 while ((len = recv(fd, netLinkHeader, phosphor::network::rtnetlink::BUFSIZE,
38 0)) > 0)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053039 {
40 for (; (NLMSG_OK(netLinkHeader, len)) &&
41 (netLinkHeader->nlmsg_type != NLMSG_DONE);
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 netLinkHeader = NLMSG_NEXT(netLinkHeader, len))
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053043 {
44 if (netLinkHeader->nlmsg_type == RTM_NEWADDR ||
45 netLinkHeader->nlmsg_type == RTM_DELADDR)
46 {
Ratan Guptaa54d8f82017-09-08 17:05:46 +053047 // starting the timer here to make sure that we don't want
48 // create the child objects multiple times.
William A. Kennington III3a70fa22018-09-20 18:48:20 -070049 if (refreshObjectTimer->hasExpired())
Ratan Guptaa54d8f82017-09-08 17:05:46 +053050 {
Ratan Guptaa54d8f82017-09-08 17:05:46 +053051 // if start timer throws exception then let the application
52 // crash
William A. Kennington III3a70fa22018-09-20 18:48:20 -070053 refreshObjectTimer->restartOnce(refreshTimeout);
Ratan Guptaa54d8f82017-09-08 17:05:46 +053054 } // end if
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 } // end if
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053056
57 } // end for
58
59 } // end while
60
61 return 0;
62}
63
Ratan Guptaf6657382017-11-10 17:58:17 +053064Server::Server(EventPtr& eventPtr, const phosphor::Descriptor& smartSock)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053065{
66 using namespace phosphor::logging;
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 using InternalFailure =
68 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
69 struct sockaddr_nl addr
70 {
71 };
72 int r{};
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053073
Gunnar Mills57d9c502018-09-14 14:42:34 -050074 sigset_t ss{};
Ratan Guptaf6657382017-11-10 17:58:17 +053075 // check that the given socket is valid or not.
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 if (smartSock() < 0)
Ratan Guptaf6657382017-11-10 17:58:17 +053077 {
78 r = -EBADF;
79 goto finish;
80 }
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053081
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
Patrick Venture836c91d2018-09-11 17:36:03 -0700110 std::memset(&addr, 0, sizeof(addr));
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530111 addr.nl_family = AF_NETLINK;
Ratan Gupta16f12882017-09-22 18:26:11 +0530112 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530113
114 if (bind(smartSock(), (struct sockaddr*)&addr, sizeof(addr)) < 0)
115 {
116 r = -errno;
117 goto finish;
118 }
119
Gunnar Mills57d9c502018-09-14 14:42:34 -0500120 r = sd_event_add_io(eventPtr.get(), nullptr, smartSock(), EPOLLIN,
121 eventHandler, nullptr);
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530122 if (r < 0)
123 {
124 goto finish;
125 }
126
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530127finish:
128
129 if (r < 0)
130 {
Gunnar Millsd75f0492017-10-25 20:33:32 -0500131 log<level::ERR>("Failure Occurred in starting of server:",
Gunnar Millsfc7df192017-10-19 16:11:05 -0500132 entry("ERRNO=%d", errno));
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530133 elog<InternalFailure>();
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530134 }
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530135}
136
Gunnar Mills57d9c502018-09-14 14:42:34 -0500137} // namespace rtnetlink
138} // namespace network
139} // namespace phosphor