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 | |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 3 | #include "util.hpp" |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 4 | |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 5 | #include <arpa/inet.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include <linux/rtnetlink.h> |
| 7 | #include <net/if.h> |
| 8 | #include <netinet/in.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 9 | #include <sys/ioctl.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <unistd.h> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 13 | |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 14 | #include <optional> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 15 | #include <phosphor-logging/elog-errors.hpp> |
| 16 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 17 | #include <stdexcept> |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 18 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 19 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace network |
| 24 | { |
| 25 | namespace route |
| 26 | { |
| 27 | |
| 28 | using namespace phosphor::logging; |
| 29 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 30 | |
| 31 | Table::Table() |
| 32 | { |
| 33 | try |
| 34 | { |
| 35 | getRoutes(); |
| 36 | } |
| 37 | catch (InternalFailure& e) |
| 38 | { |
| 39 | commit<InternalFailure>(); |
| 40 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 41 | } |
| 42 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | int Table::readNetLinkSock(int sockFd, std::array<char, BUFSIZE>& buf) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 44 | { |
| 45 | struct nlmsghdr* nlHdr = nullptr; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | int readLen{}; |
| 47 | int msgLen{}; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 48 | uint8_t seqNum = 1; |
| 49 | uint8_t pID = getpid(); |
| 50 | char* bufPtr = buf.data(); |
| 51 | |
| 52 | do |
| 53 | { |
Gunnar Mills | d75f049 | 2017-10-25 20:33:32 -0500 | [diff] [blame] | 54 | // Receive response from the kernel |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 55 | if ((readLen = recv(sockFd, bufPtr, BUFSIZE - msgLen, 0)) < 0) |
| 56 | { |
| 57 | auto error = errno; |
| 58 | log<level::ERR>("Socket recv failed:", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 59 | entry("ERROR=%s", strerror(error))); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 60 | elog<InternalFailure>(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | nlHdr = reinterpret_cast<nlmsghdr*>(bufPtr); |
| 64 | |
| 65 | // Check if the header is valid |
| 66 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 67 | if ((NLMSG_OK(nlHdr, readLen) == 0) || |
| 68 | (nlHdr->nlmsg_type == NLMSG_ERROR)) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 69 | { |
| 70 | |
| 71 | auto error = errno; |
| 72 | log<level::ERR>("Error validating header", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 73 | entry("NLMSGTYPE=%d", nlHdr->nlmsg_type), |
| 74 | entry("ERROR=%s", strerror(error))); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 75 | elog<InternalFailure>(); |
| 76 | } |
| 77 | |
| 78 | // Check if the its the last message |
| 79 | if (nlHdr->nlmsg_type == NLMSG_DONE) |
| 80 | { |
| 81 | break; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | // Else move the pointer to buffer appropriately |
| 86 | bufPtr += readLen; |
| 87 | msgLen += readLen; |
| 88 | } |
| 89 | |
| 90 | // Check if its a multi part message |
| 91 | if ((nlHdr->nlmsg_flags & NLM_F_MULTI) == 0) |
| 92 | { |
| 93 | break; |
| 94 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 95 | } while ((nlHdr->nlmsg_seq != seqNum) || (nlHdr->nlmsg_pid != pID)); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 96 | return msgLen; |
| 97 | } |
| 98 | |
| 99 | void Table::parseRoutes(const nlmsghdr* nlHdr) |
| 100 | { |
| 101 | rtmsg* rtMsg = nullptr; |
| 102 | rtattr* rtAttr = nullptr; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 103 | int rtLen{}; |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 104 | std::optional<InAddrAny> dstAddr; |
| 105 | std::optional<InAddrAny> gateWayAddr; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 106 | char ifName[IF_NAMESIZE] = {}; |
| 107 | |
| 108 | rtMsg = reinterpret_cast<rtmsg*>(NLMSG_DATA(nlHdr)); |
| 109 | |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 110 | // If the route is not for AF_INET{,6} or does not belong to main routing |
| 111 | // table then return. |
| 112 | if ((rtMsg->rtm_family != AF_INET && rtMsg->rtm_family != AF_INET6) || |
| 113 | rtMsg->rtm_table != RT_TABLE_MAIN) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 114 | { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // get the rtattr field |
| 119 | rtAttr = reinterpret_cast<rtattr*>(RTM_RTA(rtMsg)); |
| 120 | |
| 121 | rtLen = RTM_PAYLOAD(nlHdr); |
| 122 | |
| 123 | for (; RTA_OK(rtAttr, rtLen); rtAttr = RTA_NEXT(rtAttr, rtLen)) |
| 124 | { |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 125 | std::string_view attrData(reinterpret_cast<char*>(RTA_DATA(rtAttr)), |
| 126 | RTA_PAYLOAD(rtAttr)); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 127 | switch (rtAttr->rta_type) |
| 128 | { |
| 129 | case RTA_OIF: |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 130 | if_indextoname(*reinterpret_cast<int*>(RTA_DATA(rtAttr)), |
| 131 | ifName); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 132 | break; |
| 133 | case RTA_GATEWAY: |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 134 | gateWayAddr = addrFromBuf(rtMsg->rtm_family, attrData); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 135 | break; |
| 136 | case RTA_DST: |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 137 | dstAddr = addrFromBuf(rtMsg->rtm_family, attrData); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | std::string dstStr; |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 143 | if (dstAddr) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 144 | { |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 145 | dstStr = toString(*dstAddr); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 146 | } |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 147 | std::string gatewayStr; |
| 148 | if (gateWayAddr) |
| 149 | { |
| 150 | gatewayStr = toString(*gateWayAddr); |
| 151 | } |
| 152 | if (!dstAddr && gateWayAddr) |
| 153 | { |
| 154 | if (rtMsg->rtm_family == AF_INET) |
| 155 | { |
| 156 | defaultGateway = gatewayStr; |
| 157 | } |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 158 | else if (rtMsg->rtm_family == AF_INET6) |
| 159 | { |
| 160 | defaultGateway6 = gatewayStr; |
| 161 | } |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 162 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 163 | |
| 164 | Entry route(dstStr, gatewayStr, ifName); |
Ratan Gupta | 4a5f08a | 2018-05-04 17:23:16 +0530 | [diff] [blame] | 165 | // if there is already existing route for this network |
| 166 | // then ignore the next one as it would not be used by the |
| 167 | // routing policy |
| 168 | // So don't update the route entry for the network for which |
| 169 | // there is already a route exist. |
| 170 | if (routeList.find(dstStr) == routeList.end()) |
| 171 | { |
| 172 | routeList.emplace(std::make_pair(dstStr, std::move(route))); |
| 173 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | Map Table::getRoutes() |
| 177 | { |
| 178 | nlmsghdr* nlMsg = nullptr; |
| 179 | std::array<char, BUFSIZE> msgBuf = {0}; |
| 180 | |
| 181 | int sock = -1; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 182 | int len{0}; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 183 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 184 | uint8_t msgSeq{0}; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 185 | |
| 186 | // Create Socket |
| 187 | if ((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) |
| 188 | { |
| 189 | auto error = errno; |
Gunnar Mills | d75f049 | 2017-10-25 20:33:32 -0500 | [diff] [blame] | 190 | log<level::ERR>("Error occurred during socket creation", |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 191 | entry("ERRNO=%s", strerror(error))); |
| 192 | elog<InternalFailure>(); |
| 193 | } |
| 194 | |
| 195 | phosphor::Descriptor smartSock(sock); |
| 196 | sock = -1; |
| 197 | |
| 198 | // point the header and the msg structure pointers into the buffer. |
| 199 | nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data()); |
| 200 | // Length of message |
| 201 | nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg)); |
| 202 | // Get the routes from kernel routing table |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 203 | nlMsg->nlmsg_type = RTM_GETROUTE; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 204 | // The message is a request for dump |
| 205 | nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; |
| 206 | |
| 207 | nlMsg->nlmsg_seq = msgSeq; |
| 208 | nlMsg->nlmsg_pid = getpid(); |
| 209 | |
| 210 | // Send the request |
| 211 | if (send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0) < 0) |
| 212 | { |
| 213 | auto error = errno; |
| 214 | log<level::ERR>("Error occurred during send on netlink socket", |
| 215 | entry("ERRNO=%s", strerror(error))); |
| 216 | elog<InternalFailure>(); |
| 217 | } |
| 218 | |
| 219 | // Read the response |
| 220 | len = readNetLinkSock(smartSock(), msgBuf); |
| 221 | |
| 222 | // Parse and print the response |
| 223 | for (; NLMSG_OK(nlMsg, len); nlMsg = NLMSG_NEXT(nlMsg, len)) |
| 224 | { |
| 225 | parseRoutes(nlMsg); |
| 226 | } |
| 227 | return routeList; |
| 228 | } |
| 229 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 230 | std::string Table::getGateway(int addressFamily, const std::string& ipaddress, |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 231 | uint8_t prefix) const |
| 232 | { |
| 233 | std::string gateway; |
| 234 | std::string network = getNetworkID(addressFamily, ipaddress, prefix); |
| 235 | auto it = routeList.find(network); |
| 236 | if (it != routeList.end()) |
| 237 | { |
| 238 | gateway = it->second.gateway; |
| 239 | } |
| 240 | |
| 241 | return gateway; |
| 242 | } |
| 243 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 244 | } // namespace route |
| 245 | } // namespace network |
| 246 | } // namespace phosphor |