blob: 20535e5750c3ec9d305fbd40a6b24605c05c7500 [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"
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -07004#include "types.hpp"
Ratan Gupta233524c2017-05-27 11:47:31 +05305#include "util.hpp"
Ratan Gupta233524c2017-05-27 11:47:31 +05306
Patrick Venture189d44e2018-07-09 12:30:59 -07007#include <net/if.h>
Ratan Gupta233524c2017-05-27 11:47:31 +05308
William A. Kennington IIId81a0982019-02-01 21:10:27 -08009#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/log.hpp>
Ratan Gupta233524c2017-05-27 11:47:31 +053012#include <stdexcept>
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070013#include <stdplus/raw.hpp>
William A. Kennington IIId81a0982019-02-01 21:10:27 -080014#include <string_view>
Patrick Venture189d44e2018-07-09 12:30:59 -070015#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta233524c2017-05-27 11:47:31 +053016
17namespace phosphor
18{
19namespace network
20{
21namespace route
22{
23
24using namespace phosphor::logging;
25using namespace sdbusplus::xyz::openbmc_project::Common::Error;
26
William A. Kennington IIIe0564842021-10-23 16:02:22 -070027void Table::refresh()
Ratan Gupta233524c2017-05-27 11:47:31 +053028{
Jiaqing Zhaoc2e061f2022-04-07 21:55:48 +080029 defaultGateway.clear();
30 defaultGateway6.clear();
Ratan Gupta233524c2017-05-27 11:47:31 +053031 try
32 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070033 rtmsg msg{};
34 netlink::performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, msg,
35 [&](const nlmsghdr& hdr, std::string_view msg) {
36 this->parseRoutes(hdr, msg);
37 });
Ratan Gupta233524c2017-05-27 11:47:31 +053038 }
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070039 catch (const std::exception& e)
Ratan Gupta233524c2017-05-27 11:47:31 +053040 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070041 log<level::ERR>("Reading routes failed", entry("ERROR=%s", e.what()));
Ratan Gupta233524c2017-05-27 11:47:31 +053042 commit<InternalFailure>();
43 }
Ratan Gupta233524c2017-05-27 11:47:31 +053044}
45
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070046void Table::parseRoutes(const nlmsghdr& hdr, std::string_view msg)
Ratan Gupta233524c2017-05-27 11:47:31 +053047{
William A. Kennington IIId81a0982019-02-01 21:10:27 -080048 std::optional<InAddrAny> dstAddr;
49 std::optional<InAddrAny> gateWayAddr;
Ratan Gupta233524c2017-05-27 11:47:31 +053050 char ifName[IF_NAMESIZE] = {};
51
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070052 if (hdr.nlmsg_type != RTM_NEWROUTE)
53 {
54 throw std::runtime_error("Not a route msg");
55 }
56 auto rtm = stdplus::raw::extract<rtmsg>(msg);
Ratan Gupta233524c2017-05-27 11:47:31 +053057
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070058 if ((rtm.rtm_family != AF_INET && rtm.rtm_family != AF_INET6) ||
59 rtm.rtm_table != RT_TABLE_MAIN)
Ratan Gupta233524c2017-05-27 11:47:31 +053060 {
61 return;
62 }
63
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070064 while (!msg.empty())
Ratan Gupta233524c2017-05-27 11:47:31 +053065 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070066 auto [hdr, data] = netlink::extractRtAttr(msg);
67 switch (hdr.rta_type)
Ratan Gupta233524c2017-05-27 11:47:31 +053068 {
69 case RTA_OIF:
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070070 if_indextoname(stdplus::raw::copyFrom<int>(data), ifName);
Ratan Gupta233524c2017-05-27 11:47:31 +053071 break;
72 case RTA_GATEWAY:
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070073 gateWayAddr = addrFromBuf(rtm.rtm_family, data);
Ratan Gupta233524c2017-05-27 11:47:31 +053074 break;
75 case RTA_DST:
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070076 dstAddr = addrFromBuf(rtm.rtm_family, data);
Ratan Gupta233524c2017-05-27 11:47:31 +053077 break;
78 }
79 }
80
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070081 if (rtm.rtm_dst_len == 0 && gateWayAddr)
William A. Kennington IIId81a0982019-02-01 21:10:27 -080082 {
Ravi Tejaa5a09442020-07-17 00:57:33 -050083 std::string ifNameStr(ifName);
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070084 if (rtm.rtm_family == AF_INET)
William A. Kennington IIId81a0982019-02-01 21:10:27 -080085 {
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070086 defaultGateway.emplace(ifNameStr, std::to_string(*gateWayAddr));
William A. Kennington IIId81a0982019-02-01 21:10:27 -080087 }
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070088 else if (rtm.rtm_family == AF_INET6)
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080089 {
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070090 defaultGateway6.emplace(ifNameStr, std::to_string(*gateWayAddr));
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080091 }
William A. Kennington IIId81a0982019-02-01 21:10:27 -080092 }
Ratan Gupta233524c2017-05-27 11:47:31 +053093}
94
Gunnar Mills57d9c502018-09-14 14:42:34 -050095} // namespace route
96} // namespace network
97} // namespace phosphor