Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame^] | 1 | #include "config_parser.hpp" |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 2 | #include "util.hpp" |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame^] | 3 | #include "types.hpp" |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
| 5 | |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 8 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 9 | #include <arpa/inet.h> |
| 10 | #include <dirent.h> |
| 11 | #include <net/if.h> |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 12 | #include <sys/wait.h> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 13 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 14 | #include <iostream> |
| 15 | #include <list> |
| 16 | #include <string> |
| 17 | #include <algorithm> |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame^] | 18 | #include <experimental/filesystem> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace network |
| 23 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 24 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 25 | namespace |
| 26 | { |
| 27 | |
| 28 | using namespace phosphor::logging; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 29 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame^] | 30 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 31 | |
| 32 | uint8_t toV6Cidr(const std::string& subnetMask) |
| 33 | { |
| 34 | uint8_t pos = 0; |
| 35 | uint8_t prevPos = 0; |
| 36 | uint8_t cidr = 0; |
| 37 | uint16_t buff {}; |
| 38 | do |
| 39 | { |
| 40 | //subnet mask look like ffff:ffff:: |
| 41 | // or ffff:c000:: |
| 42 | pos = subnetMask.find(":", prevPos); |
| 43 | if (pos == std::string::npos) |
| 44 | { |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | auto str = subnetMask.substr(prevPos, (pos - prevPos)); |
| 49 | prevPos = pos + 1; |
| 50 | |
| 51 | // String length is 0 |
| 52 | if (!str.length()) |
| 53 | { |
| 54 | return cidr; |
| 55 | } |
| 56 | //converts it into number. |
| 57 | if (sscanf(str.c_str(), "%hx", &buff) <= 0) |
| 58 | { |
| 59 | log<level::ERR>("Invalid Mask", |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 60 | entry("SUBNETMASK=%s", subnetMask)); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | // convert the number into bitset |
| 66 | // and check for how many ones are there. |
| 67 | // if we don't have all the ones then make |
| 68 | // sure that all the ones should be left justify. |
| 69 | |
| 70 | if (__builtin_popcount(buff) != 16) |
| 71 | { |
| 72 | if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) != __builtin_popcount(buff)) |
| 73 | { |
| 74 | log<level::ERR>("Invalid Mask", |
| 75 | entry("SUBNETMASK=%s", subnetMask)); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | cidr += __builtin_popcount(buff); |
| 80 | return cidr; |
| 81 | } |
| 82 | |
| 83 | cidr += 16; |
| 84 | } |
| 85 | while (1); |
| 86 | |
| 87 | return cidr; |
| 88 | } |
| 89 | }// anonymous namespace |
| 90 | |
| 91 | uint8_t toCidr(int addressFamily, const std::string& subnetMask) |
| 92 | { |
| 93 | if (addressFamily == AF_INET6) |
| 94 | { |
| 95 | return toV6Cidr(subnetMask); |
| 96 | } |
| 97 | |
| 98 | uint32_t buff; |
| 99 | |
| 100 | auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff); |
| 101 | if (rc <= 0) |
| 102 | { |
| 103 | log<level::ERR>("inet_pton failed:", |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 104 | entry("SUBNETMASK=%s", subnetMask)); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | buff = be32toh(buff); |
| 109 | // total no of bits - total no of leading zero == total no of ones |
| 110 | if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) == __builtin_popcount(buff)) |
| 111 | { |
| 112 | return __builtin_popcount(buff); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | log<level::ERR>("Invalid Mask", |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 117 | entry("SUBNETMASK=%s", subnetMask)); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | std::string toMask(int addressFamily, uint8_t prefix) |
| 123 | { |
| 124 | if (addressFamily == AF_INET6) |
| 125 | { |
| 126 | //TODO:- conversion for v6 |
| 127 | return ""; |
| 128 | } |
| 129 | |
| 130 | if (prefix < 1 || prefix > 30) |
| 131 | { |
| 132 | log<level::ERR>("Invalid Prefix", |
| 133 | entry("PREFIX=%d", prefix)); |
| 134 | return ""; |
| 135 | } |
| 136 | /* Create the netmask from the number of bits */ |
| 137 | unsigned long mask = 0; |
| 138 | for (auto i = 0 ; i < prefix ; i++) |
| 139 | { |
| 140 | mask |= 1 << (31 - i); |
| 141 | } |
| 142 | struct in_addr netmask; |
| 143 | netmask.s_addr = htonl(mask); |
| 144 | return inet_ntoa(netmask); |
| 145 | } |
| 146 | |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 147 | std::string getNetworkID(int addressFamily, const std::string& ipaddress, |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 148 | uint8_t prefix) |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 149 | { |
| 150 | unsigned char* pntMask = nullptr; |
| 151 | unsigned char* pntNetwork = nullptr; |
| 152 | int bit {}; |
| 153 | int offset {}; |
| 154 | struct in6_addr netmask {}; |
| 155 | const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, |
| 156 | 0xf8, 0xfc, 0xfe, 0xff |
| 157 | }; |
| 158 | |
| 159 | pntMask = reinterpret_cast<unsigned char*>(&netmask); |
| 160 | |
| 161 | offset = prefix / 8; |
| 162 | bit = prefix % 8; |
| 163 | |
| 164 | while (offset--) |
| 165 | { |
| 166 | *pntMask++ = 0xff; |
| 167 | } |
| 168 | |
| 169 | if (bit) |
| 170 | { |
| 171 | *pntMask = maskbit[bit]; |
| 172 | } |
| 173 | |
| 174 | // convert ipaddres string into network address |
| 175 | struct in6_addr ipaddressNetwork; |
| 176 | if (inet_pton(addressFamily, ipaddress.c_str(), &ipaddressNetwork) <= 0) |
| 177 | { |
| 178 | log<level::ERR>("inet_pton failure", |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 179 | entry("IPADDRESS=%s", ipaddress.c_str())); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 180 | report<InternalFailure>(); |
| 181 | |
| 182 | return ""; |
| 183 | } |
| 184 | |
| 185 | // Now bit wise and gets you the network address |
| 186 | pntMask = reinterpret_cast<unsigned char*>(&netmask); |
| 187 | pntNetwork = reinterpret_cast<unsigned char*>(&ipaddressNetwork); |
| 188 | |
| 189 | for (int i = 0; i < 16 ; i++) |
| 190 | { |
| 191 | pntNetwork[i] = pntNetwork[i] & pntMask[i]; |
| 192 | } |
| 193 | |
| 194 | //convert the network address into string fomat. |
| 195 | char networkString[INET6_ADDRSTRLEN] = { 0 }; |
| 196 | if (inet_ntop(addressFamily, &ipaddressNetwork, networkString, |
| 197 | INET6_ADDRSTRLEN) == NULL) |
| 198 | { |
| 199 | log<level::ERR>("inet_ntop failure"); |
| 200 | report<InternalFailure>(); |
| 201 | } |
| 202 | return networkString; |
| 203 | } |
| 204 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 205 | bool isLinkLocal(const std::string& address) |
| 206 | { |
| 207 | std::string linklocal = "fe80"; |
| 208 | return std::mismatch(linklocal.begin(), linklocal.end(), |
| 209 | address.begin()).first == linklocal.end() ? |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 210 | true : false; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 211 | } |
| 212 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 213 | IntfAddrMap getInterfaceAddrs() |
| 214 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 215 | IntfAddrMap intfMap {}; |
| 216 | AddrList addrList {}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 217 | struct ifaddrs* ifaddr = nullptr; |
| 218 | |
| 219 | // attempt to fill struct with ifaddrs |
| 220 | if (getifaddrs(&ifaddr) == -1) |
| 221 | { |
| 222 | auto error = errno; |
| 223 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 224 | entry("ERRNO=%s", strerror(error))); |
| 225 | elog<InternalFailure>(); |
| 226 | } |
| 227 | |
| 228 | AddrPtr ifaddrPtr(ifaddr); |
| 229 | ifaddr = nullptr; |
| 230 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 231 | std::string intfName {}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 232 | |
| 233 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 234 | { |
| 235 | // walk interfaces |
| 236 | if (ifa->ifa_addr == nullptr) |
| 237 | { |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | // get only INET interfaces not ipv6 |
| 242 | if (ifa->ifa_addr->sa_family == AF_INET || |
| 243 | ifa->ifa_addr->sa_family == AF_INET6) |
| 244 | { |
| 245 | // if loopback, or not running ignore |
| 246 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 247 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 248 | { |
| 249 | continue; |
| 250 | } |
| 251 | // if the interface name is not same as the previous |
| 252 | // iteration then add the addr list into |
| 253 | // the map. |
| 254 | if (intfName != "" && intfName != std::string(ifa->ifa_name)) |
| 255 | { |
| 256 | intfMap.emplace(intfName, addrList); |
| 257 | addrList.clear(); |
| 258 | } |
| 259 | intfName = ifa->ifa_name; |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 260 | AddrInfo info {}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 261 | char ip[INET6_ADDRSTRLEN] = { 0 }; |
| 262 | char subnetMask[INET6_ADDRSTRLEN] = { 0 }; |
| 263 | |
| 264 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 265 | { |
| 266 | |
| 267 | inet_ntop(ifa->ifa_addr->sa_family, |
| 268 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), |
| 269 | ip, |
| 270 | sizeof(ip)); |
| 271 | |
| 272 | inet_ntop(ifa->ifa_addr->sa_family, |
| 273 | &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr), |
| 274 | subnetMask, |
| 275 | sizeof(subnetMask)); |
| 276 | |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | inet_ntop(ifa->ifa_addr->sa_family, |
| 281 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
| 282 | ip, |
| 283 | sizeof(ip)); |
| 284 | |
| 285 | inet_ntop(ifa->ifa_addr->sa_family, |
| 286 | &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), |
| 287 | subnetMask, |
| 288 | sizeof(subnetMask)); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | info.addrType = ifa->ifa_addr->sa_family; |
| 293 | info.ipaddress = ip; |
| 294 | info.prefix = toCidr(info.addrType, std::string(subnetMask)); |
| 295 | addrList.emplace_back(info); |
| 296 | } |
| 297 | } |
| 298 | intfMap.emplace(intfName, addrList); |
| 299 | return intfMap; |
| 300 | } |
| 301 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 302 | void deleteInterface(const std::string& intf) |
| 303 | { |
| 304 | pid_t pid = fork(); |
| 305 | int status {}; |
| 306 | |
| 307 | if (pid == 0) |
| 308 | { |
| 309 | |
| 310 | execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr); |
| 311 | auto error = errno; |
| 312 | log<level::ERR>("Couldn't delete the device", |
| 313 | entry("ERRNO=%d", error), |
| 314 | entry("INTF=%s", intf.c_str())); |
| 315 | elog<InternalFailure>(); |
| 316 | } |
| 317 | else if (pid < 0) |
| 318 | { |
| 319 | auto error = errno; |
| 320 | log<level::ERR>("Error occurred during fork", |
| 321 | entry("ERRNO=%d", error)); |
| 322 | elog<InternalFailure>(); |
| 323 | } |
| 324 | else if (pid > 0) |
| 325 | { |
| 326 | while (waitpid(pid, &status, 0) == -1) |
| 327 | { |
| 328 | if (errno != EINTR) |
| 329 | { /* Error other than EINTR */ |
| 330 | status = -1; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if(status < 0) |
| 336 | { |
| 337 | log<level::ERR>("Unable to delete the interface", |
| 338 | entry("INTF=%s", intf.c_str(), |
| 339 | entry("STATUS=%d", status))); |
| 340 | elog<InternalFailure>(); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame^] | 345 | bool getDHCPValue(const std::string& confDir, const std::string& intf) |
| 346 | { |
| 347 | bool dhcp = false; |
| 348 | // Get the interface mode value from systemd conf |
| 349 | //using namespace std::string_literals; |
| 350 | fs::path confPath = confDir; |
| 351 | std::string fileName = systemd::config::networkFilePrefix + intf + |
| 352 | systemd::config::networkFileSuffix; |
| 353 | confPath /= fileName; |
| 354 | |
| 355 | try |
| 356 | { |
| 357 | config::Parser parser(confPath.string()); |
| 358 | auto values = parser.getValues("Network", "DHCP"); |
| 359 | // There will be only single value for DHCP key. |
| 360 | if (values[0] == "true") |
| 361 | { |
| 362 | dhcp = true; |
| 363 | } |
| 364 | } |
| 365 | catch (InternalFailure& e) |
| 366 | { |
| 367 | log<level::INFO>("Exception occured during getting of DHCP value"); |
| 368 | } |
| 369 | return dhcp; |
| 370 | } |
| 371 | |
| 372 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 373 | }//namespace network |
| 374 | }//namespace phosphor |