blob: 020674378eab73708429fd5d597c6dc6b762d1c9 [file] [log] [blame]
Ratan Gupta233524c2017-05-27 11:47:31 +05301#include "routing_table.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -07003#include "netlink.hpp"
Ratan Gupta233524c2017-05-27 11:47:31 +05304#include "util.hpp"
Ratan Gupta233524c2017-05-27 11:47:31 +05305
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <net/if.h>
Ratan Gupta233524c2017-05-27 11:47:31 +05307
William A. Kennington IIId81a0982019-02-01 21:10:27 -08008#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -07009#include <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/log.hpp>
Ratan Gupta233524c2017-05-27 11:47:31 +053011#include <stdexcept>
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070012#include <stdplus/raw.hpp>
William A. Kennington IIId81a0982019-02-01 21:10:27 -080013#include <string_view>
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta233524c2017-05-27 11:47:31 +053015
16namespace phosphor
17{
18namespace network
19{
20namespace route
21{
22
23using namespace phosphor::logging;
24using namespace sdbusplus::xyz::openbmc_project::Common::Error;
25
William A. Kennington IIIe0564842021-10-23 16:02:22 -070026void Table::refresh()
Ratan Gupta233524c2017-05-27 11:47:31 +053027{
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +080028 defaultGateway.clear();
29 defaultGateway6.clear();
Ratan Gupta233524c2017-05-27 11:47:31 +053030 try
31 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070032 rtmsg msg{};
33 netlink::performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, msg,
34 [&](const nlmsghdr& hdr, std::string_view msg) {
35 this->parseRoutes(hdr, msg);
36 });
Ratan Gupta233524c2017-05-27 11:47:31 +053037 }
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070038 catch (const std::exception& e)
Ratan Gupta233524c2017-05-27 11:47:31 +053039 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070040 log<level::ERR>("Reading routes failed", entry("ERROR=%s", e.what()));
Ratan Gupta233524c2017-05-27 11:47:31 +053041 commit<InternalFailure>();
42 }
Ratan Gupta233524c2017-05-27 11:47:31 +053043}
44
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070045void Table::parseRoutes(const nlmsghdr& hdr, std::string_view msg)
Ratan Gupta233524c2017-05-27 11:47:31 +053046{
William A. Kennington IIId81a0982019-02-01 21:10:27 -080047 std::optional<InAddrAny> dstAddr;
48 std::optional<InAddrAny> gateWayAddr;
Ratan Gupta233524c2017-05-27 11:47:31 +053049 char ifName[IF_NAMESIZE] = {};
50
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070051 if (hdr.nlmsg_type != RTM_NEWROUTE)
52 {
53 throw std::runtime_error("Not a route msg");
54 }
55 auto rtm = stdplus::raw::extract<rtmsg>(msg);
Ratan Gupta233524c2017-05-27 11:47:31 +053056
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070057 if ((rtm.rtm_family != AF_INET && rtm.rtm_family != AF_INET6) ||
58 rtm.rtm_table != RT_TABLE_MAIN)
Ratan Gupta233524c2017-05-27 11:47:31 +053059 {
60 return;
61 }
62
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070063 while (!msg.empty())
Ratan Gupta233524c2017-05-27 11:47:31 +053064 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070065 auto [hdr, data] = netlink::extractRtAttr(msg);
66 switch (hdr.rta_type)
Ratan Gupta233524c2017-05-27 11:47:31 +053067 {
68 case RTA_OIF:
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070069 if_indextoname(stdplus::raw::copyFrom<int>(data), ifName);
Ratan Gupta233524c2017-05-27 11:47:31 +053070 break;
71 case RTA_GATEWAY:
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070072 gateWayAddr = addrFromBuf(rtm.rtm_family, data);
Ratan Gupta233524c2017-05-27 11:47:31 +053073 break;
74 case RTA_DST:
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070075 dstAddr = addrFromBuf(rtm.rtm_family, data);
Ratan Gupta233524c2017-05-27 11:47:31 +053076 break;
77 }
78 }
79
80 std::string dstStr;
William A. Kennington IIId81a0982019-02-01 21:10:27 -080081 if (dstAddr)
Ratan Gupta233524c2017-05-27 11:47:31 +053082 {
William A. Kennington IIId81a0982019-02-01 21:10:27 -080083 dstStr = toString(*dstAddr);
Ratan Gupta233524c2017-05-27 11:47:31 +053084 }
William A. Kennington IIId81a0982019-02-01 21:10:27 -080085 std::string gatewayStr;
86 if (gateWayAddr)
87 {
88 gatewayStr = toString(*gateWayAddr);
89 }
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070090 if (rtm.rtm_dst_len == 0 && gateWayAddr)
William A. Kennington IIId81a0982019-02-01 21:10:27 -080091 {
Ravi Tejaa5a09442020-07-17 00:57:33 -050092 std::string ifNameStr(ifName);
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070093 if (rtm.rtm_family == AF_INET)
William A. Kennington IIId81a0982019-02-01 21:10:27 -080094 {
Ravi Tejaa5a09442020-07-17 00:57:33 -050095 defaultGateway[ifNameStr] = gatewayStr;
William A. Kennington IIId81a0982019-02-01 21:10:27 -080096 }
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070097 else if (rtm.rtm_family == AF_INET6)
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080098 {
Ravi Tejaa5a09442020-07-17 00:57:33 -050099 defaultGateway6[ifNameStr] = gatewayStr;
William A. Kennington IIId3c249c2019-02-01 21:12:02 -0800100 }
William A. Kennington IIId81a0982019-02-01 21:10:27 -0800101 }
Ratan Gupta233524c2017-05-27 11:47:31 +0530102 Entry route(dstStr, gatewayStr, ifName);
Ratan Gupta4a5f08a2018-05-04 17:23:16 +0530103 // if there is already existing route for this network
104 // then ignore the next one as it would not be used by the
105 // routing policy
106 // So don't update the route entry for the network for which
107 // there is already a route exist.
108 if (routeList.find(dstStr) == routeList.end())
109 {
110 routeList.emplace(std::make_pair(dstStr, std::move(route)));
111 }
Ratan Gupta233524c2017-05-27 11:47:31 +0530112}
113
Gunnar Mills57d9c502018-09-14 14:42:34 -0500114} // namespace route
115} // namespace network
116} // namespace phosphor