blob: eb63f74d5d163377d00085f1fa7cc2fa18e634bb [file] [log] [blame]
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05301#include "rtnetlink_server.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
Ratan Guptaa54d8f82017-09-08 17:05:46 +05303#include "timer.hpp"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05304#include "types.hpp"
5#include "util.hpp"
6
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05307#include <linux/netlink.h>
8#include <linux/rtnetlink.h>
9#include <net/if.h>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <netinet/in.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053011#include <sys/types.h>
12#include <systemd/sd-daemon.h>
13#include <unistd.h>
14
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053015#include <memory>
Patrick Venture189d44e2018-07-09 12:30:59 -070016#include <phosphor-logging/elog-errors.hpp>
17#include <phosphor-logging/log.hpp>
18#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053019
20namespace phosphor
21{
22namespace network
23{
Ratan Guptaa54d8f82017-09-08 17:05:46 +053024
Ratan Gupta16f12882017-09-22 18:26:11 +053025extern std::unique_ptr<phosphor::network::Timer> refreshObjectTimer;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053026
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053027namespace rtnetlink
28{
29
30/* Call Back for the sd event loop */
31static int eventHandler(sd_event_source* es, int fd, uint32_t revents,
32 void* userdata)
33{
34 char buffer[phosphor::network::rtnetlink::BUFSIZE] {};
35 int len {};
36
37 auto netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer);
38 while ((len = recv(fd, netLinkHeader,
39 phosphor::network::rtnetlink::BUFSIZE, 0)) > 0)
40 {
41 for (; (NLMSG_OK(netLinkHeader, len)) &&
42 (netLinkHeader->nlmsg_type != NLMSG_DONE);
43 netLinkHeader = NLMSG_NEXT(netLinkHeader, len))
44 {
45 if (netLinkHeader->nlmsg_type == RTM_NEWADDR ||
46 netLinkHeader->nlmsg_type == RTM_DELADDR)
47 {
Ratan Guptaa54d8f82017-09-08 17:05:46 +053048 // starting the timer here to make sure that we don't want
49 // create the child objects multiple times.
Ratan Gupta16f12882017-09-22 18:26:11 +053050 if (refreshObjectTimer->isExpired())
Ratan Guptaa54d8f82017-09-08 17:05:46 +053051 {
52 using namespace std::chrono;
Ratan Gupta16f12882017-09-22 18:26:11 +053053 auto time = duration_cast<microseconds>(refreshTimeout);
Ratan Guptaa54d8f82017-09-08 17:05:46 +053054 // if start timer throws exception then let the application
55 // crash
Ratan Gupta16f12882017-09-22 18:26:11 +053056 refreshObjectTimer->startTimer(time);
Ratan Guptaa54d8f82017-09-08 17:05:46 +053057 } // end if
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053058 } // end if
59
60 } // end for
61
62 } // end while
63
64 return 0;
65}
66
Ratan Guptaf6657382017-11-10 17:58:17 +053067Server::Server(EventPtr& eventPtr, const phosphor::Descriptor& smartSock)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053068{
69 using namespace phosphor::logging;
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +053070 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
71 Error::InternalFailure;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053072 struct sockaddr_nl addr {};
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053073 int r {};
74
75 sigset_t ss {};
Ratan Guptaf6657382017-11-10 17:58:17 +053076 // check that the given socket is valid or not.
77 if(smartSock() < 0)
78 {
79 r = -EBADF;
80 goto finish;
81 }
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053082
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
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530111 memset(&addr, 0, sizeof(addr));
112 addr.nl_family = AF_NETLINK;
Ratan Gupta16f12882017-09-22 18:26:11 +0530113 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530114
115 if (bind(smartSock(), (struct sockaddr*)&addr, sizeof(addr)) < 0)
116 {
117 r = -errno;
118 goto finish;
119 }
120
121 r = sd_event_add_io(eventPtr.get(), nullptr,
122 smartSock(), EPOLLIN, eventHandler, nullptr);
123 if (r < 0)
124 {
125 goto finish;
126 }
127
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530128finish:
129
130 if (r < 0)
131 {
Gunnar Millsd75f0492017-10-25 20:33:32 -0500132 log<level::ERR>("Failure Occurred in starting of server:",
Gunnar Millsfc7df192017-10-19 16:11:05 -0500133 entry("ERRNO=%d", errno));
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530134 elog<InternalFailure>();
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530135 }
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530136}
137
138
139} //rtnetlink
140} //network
141} //phosphor