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