blob: d0a11b9d98607faaf31ba2f1ccc97c18f729275c [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
William A. Kennington IIId2d05942019-01-30 16:04:11 -080029static bool shouldRefresh(const struct nlmsghdr& hdr)
30{
31 switch (hdr.nlmsg_type)
32 {
33 case RTM_NEWADDR:
34 case RTM_DELADDR:
35 {
36 return true;
37 }
38 }
39
40 return false;
41}
42
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053043/* Call Back for the sd event loop */
44static int eventHandler(sd_event_source* es, int fd, uint32_t revents,
45 void* userdata)
46{
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 char buffer[phosphor::network::rtnetlink::BUFSIZE]{};
48 int len{};
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053049
50 auto netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer);
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 while ((len = recv(fd, netLinkHeader, phosphor::network::rtnetlink::BUFSIZE,
52 0)) > 0)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053053 {
54 for (; (NLMSG_OK(netLinkHeader, len)) &&
55 (netLinkHeader->nlmsg_type != NLMSG_DONE);
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 netLinkHeader = NLMSG_NEXT(netLinkHeader, len))
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053057 {
William A. Kennington IIId2d05942019-01-30 16:04:11 -080058 if (shouldRefresh(*netLinkHeader))
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053059 {
Ratan Guptaa54d8f82017-09-08 17:05:46 +053060 // starting the timer here to make sure that we don't want
61 // create the child objects multiple times.
William A. Kennington IIIcb500dc2018-12-03 15:32:12 -080062 if (!refreshObjectTimer->isEnabled())
Ratan Guptaa54d8f82017-09-08 17:05:46 +053063 {
Ratan Guptaa54d8f82017-09-08 17:05:46 +053064 // if start timer throws exception then let the application
65 // crash
William A. Kennington III3a70fa22018-09-20 18:48:20 -070066 refreshObjectTimer->restartOnce(refreshTimeout);
Ratan Guptaa54d8f82017-09-08 17:05:46 +053067 } // end if
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 } // end if
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053069
70 } // end for
71
72 } // end while
73
74 return 0;
75}
76
Ratan Guptaf6657382017-11-10 17:58:17 +053077Server::Server(EventPtr& eventPtr, const phosphor::Descriptor& smartSock)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053078{
79 using namespace phosphor::logging;
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 using InternalFailure =
81 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
82 struct sockaddr_nl addr
83 {
84 };
85 int r{};
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053086
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 sigset_t ss{};
Ratan Guptaf6657382017-11-10 17:58:17 +053088 // check that the given socket is valid or not.
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 if (smartSock() < 0)
Ratan Guptaf6657382017-11-10 17:58:17 +053090 {
91 r = -EBADF;
92 goto finish;
93 }
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053094
95 if (sigemptyset(&ss) < 0 || sigaddset(&ss, SIGTERM) < 0 ||
96 sigaddset(&ss, SIGINT) < 0)
97 {
98 r = -errno;
99 goto finish;
100 }
101 /* Block SIGTERM first, so that the event loop can handle it */
102 if (sigprocmask(SIG_BLOCK, &ss, NULL) < 0)
103 {
104 r = -errno;
105 goto finish;
106 }
107
108 /* Let's make use of the default handler and "floating"
109 reference features of sd_event_add_signal() */
110
111 r = sd_event_add_signal(eventPtr.get(), NULL, SIGTERM, NULL, NULL);
112 if (r < 0)
113 {
114 goto finish;
115 }
116
117 r = sd_event_add_signal(eventPtr.get(), NULL, SIGINT, NULL, NULL);
118 if (r < 0)
119 {
120 goto finish;
121 }
122
Patrick Venture836c91d2018-09-11 17:36:03 -0700123 std::memset(&addr, 0, sizeof(addr));
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530124 addr.nl_family = AF_NETLINK;
Ratan Gupta16f12882017-09-22 18:26:11 +0530125 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530126
127 if (bind(smartSock(), (struct sockaddr*)&addr, sizeof(addr)) < 0)
128 {
129 r = -errno;
130 goto finish;
131 }
132
Gunnar Mills57d9c502018-09-14 14:42:34 -0500133 r = sd_event_add_io(eventPtr.get(), nullptr, smartSock(), EPOLLIN,
134 eventHandler, nullptr);
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530135 if (r < 0)
136 {
137 goto finish;
138 }
139
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530140finish:
141
142 if (r < 0)
143 {
Gunnar Millsd75f0492017-10-25 20:33:32 -0500144 log<level::ERR>("Failure Occurred in starting of server:",
Gunnar Millsfc7df192017-10-19 16:11:05 -0500145 entry("ERRNO=%d", errno));
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530146 elog<InternalFailure>();
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530147 }
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530148}
149
Gunnar Mills57d9c502018-09-14 14:42:34 -0500150} // namespace rtnetlink
151} // namespace network
152} // namespace phosphor