Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 1 | #include "routing_table.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 3 | #include "netlink.hpp" |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 4 | #include "types.hpp" |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 5 | #include "util.hpp" |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 6 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 7 | #include <net/if.h> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 8 | |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 9 | #include <optional> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
| 11 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 12 | #include <stdexcept> |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 13 | #include <stdplus/raw.hpp> |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 14 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace network |
| 20 | { |
| 21 | namespace route |
| 22 | { |
| 23 | |
| 24 | using namespace phosphor::logging; |
| 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 26 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 27 | void Table::refresh() |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 28 | { |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 29 | defaultGateway.clear(); |
| 30 | defaultGateway6.clear(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 31 | try |
| 32 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 33 | 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 Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 38 | } |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 39 | catch (const std::exception& e) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 40 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 41 | log<level::ERR>("Reading routes failed", entry("ERROR=%s", e.what())); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 42 | commit<InternalFailure>(); |
| 43 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 44 | } |
| 45 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 46 | void Table::parseRoutes(const nlmsghdr& hdr, std::string_view msg) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 47 | { |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 48 | std::optional<InAddrAny> dstAddr; |
| 49 | std::optional<InAddrAny> gateWayAddr; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 50 | char ifName[IF_NAMESIZE] = {}; |
| 51 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 52 | 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 Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 57 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 58 | if ((rtm.rtm_family != AF_INET && rtm.rtm_family != AF_INET6) || |
| 59 | rtm.rtm_table != RT_TABLE_MAIN) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 60 | { |
| 61 | return; |
| 62 | } |
| 63 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 64 | while (!msg.empty()) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 65 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 66 | auto [hdr, data] = netlink::extractRtAttr(msg); |
| 67 | switch (hdr.rta_type) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 68 | { |
| 69 | case RTA_OIF: |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 70 | if_indextoname(stdplus::raw::copyFrom<int>(data), ifName); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 71 | break; |
| 72 | case RTA_GATEWAY: |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 73 | gateWayAddr = addrFromBuf(rtm.rtm_family, data); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 74 | break; |
| 75 | case RTA_DST: |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 76 | dstAddr = addrFromBuf(rtm.rtm_family, data); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 81 | if (rtm.rtm_dst_len == 0 && gateWayAddr) |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 82 | { |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 83 | std::string ifNameStr(ifName); |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 84 | if (rtm.rtm_family == AF_INET) |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 85 | { |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 86 | defaultGateway.emplace(ifNameStr, std::to_string(*gateWayAddr)); |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 87 | } |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 88 | else if (rtm.rtm_family == AF_INET6) |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 89 | { |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 90 | defaultGateway6.emplace(ifNameStr, std::to_string(*gateWayAddr)); |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 91 | } |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 92 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 93 | } |
| 94 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 95 | } // namespace route |
| 96 | } // namespace network |
| 97 | } // namespace phosphor |