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" |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 4 | #include "util.hpp" |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 5 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include <net/if.h> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 7 | |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 8 | #include <optional> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 11 | #include <stdexcept> |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 12 | #include <stdplus/raw.hpp> |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 13 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
| 20 | namespace route |
| 21 | { |
| 22 | |
| 23 | using namespace phosphor::logging; |
| 24 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 25 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 26 | void Table::refresh() |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 27 | { |
| 28 | try |
| 29 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 30 | rtmsg msg{}; |
| 31 | netlink::performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, msg, |
| 32 | [&](const nlmsghdr& hdr, std::string_view msg) { |
| 33 | this->parseRoutes(hdr, msg); |
| 34 | }); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 35 | } |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 36 | catch (const std::exception& e) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 37 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 38 | log<level::ERR>("Reading routes failed", entry("ERROR=%s", e.what())); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 39 | commit<InternalFailure>(); |
| 40 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 41 | } |
| 42 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 43 | void Table::parseRoutes(const nlmsghdr& hdr, std::string_view msg) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 44 | { |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 45 | std::optional<InAddrAny> dstAddr; |
| 46 | std::optional<InAddrAny> gateWayAddr; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 47 | char ifName[IF_NAMESIZE] = {}; |
| 48 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 49 | if (hdr.nlmsg_type != RTM_NEWROUTE) |
| 50 | { |
| 51 | throw std::runtime_error("Not a route msg"); |
| 52 | } |
| 53 | auto rtm = stdplus::raw::extract<rtmsg>(msg); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 54 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 55 | if ((rtm.rtm_family != AF_INET && rtm.rtm_family != AF_INET6) || |
| 56 | rtm.rtm_table != RT_TABLE_MAIN) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 57 | { |
| 58 | return; |
| 59 | } |
| 60 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 61 | while (!msg.empty()) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 62 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 63 | auto [hdr, data] = netlink::extractRtAttr(msg); |
| 64 | switch (hdr.rta_type) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 65 | { |
| 66 | case RTA_OIF: |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 67 | if_indextoname(stdplus::raw::copyFrom<int>(data), ifName); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 68 | break; |
| 69 | case RTA_GATEWAY: |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 70 | gateWayAddr = addrFromBuf(rtm.rtm_family, data); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 71 | break; |
| 72 | case RTA_DST: |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 73 | dstAddr = addrFromBuf(rtm.rtm_family, data); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | std::string dstStr; |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 79 | if (dstAddr) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 80 | { |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 81 | dstStr = toString(*dstAddr); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 82 | } |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 83 | std::string gatewayStr; |
| 84 | if (gateWayAddr) |
| 85 | { |
| 86 | gatewayStr = toString(*gateWayAddr); |
| 87 | } |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 88 | if (rtm.rtm_dst_len == 0 && gateWayAddr) |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 89 | { |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 90 | std::string ifNameStr(ifName); |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 91 | if (rtm.rtm_family == AF_INET) |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 92 | { |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 93 | defaultGateway[ifNameStr] = gatewayStr; |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 94 | } |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 95 | else if (rtm.rtm_family == AF_INET6) |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 96 | { |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 97 | defaultGateway6[ifNameStr] = gatewayStr; |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 98 | } |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 99 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 100 | Entry route(dstStr, gatewayStr, ifName); |
Ratan Gupta | 4a5f08a | 2018-05-04 17:23:16 +0530 | [diff] [blame] | 101 | // if there is already existing route for this network |
| 102 | // then ignore the next one as it would not be used by the |
| 103 | // routing policy |
| 104 | // So don't update the route entry for the network for which |
| 105 | // there is already a route exist. |
| 106 | if (routeList.find(dstStr) == routeList.end()) |
| 107 | { |
| 108 | routeList.emplace(std::make_pair(dstStr, std::move(route))); |
| 109 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 110 | } |
| 111 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 112 | } // namespace route |
| 113 | } // namespace network |
| 114 | } // namespace phosphor |