Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 3 | #include "config.h" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 5 | #include "types.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 7 | #include <netinet/ether.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 10 | #include <cstring> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 12 | #include <string> |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 13 | #include <string_view> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 19 | |
| 20 | constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; |
| 21 | constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; |
| 22 | constexpr auto IPV6_MAX_PREFIX_LENGTH = 64; |
| 23 | constexpr auto IPV4_PREFIX = "169.254"; |
| 24 | constexpr auto IPV6_PREFIX = "fe80"; |
| 25 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 26 | namespace mac_address |
| 27 | { |
| 28 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 29 | constexpr auto localAdminMask = 0x020000000000; |
| 30 | constexpr auto broadcastMac = 0xFFFFFFFFFFFF; |
| 31 | |
Nagaraju Goruganti | 5993763 | 2017-12-05 00:06:07 -0600 | [diff] [blame] | 32 | constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 33 | constexpr size_t size = 18; |
| 34 | |
| 35 | /** @brief validate the mac address |
| 36 | * @param[in] value - MAC address. |
| 37 | * @returns true if validate otherwise false. |
| 38 | */ |
| 39 | inline bool validate(const std::string& value) |
| 40 | { |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 41 | struct ether_addr* addr = ether_aton(value.c_str()); |
| 42 | if (addr == nullptr) |
| 43 | { |
| 44 | return false; |
| 45 | } |
| 46 | ether_addr zero{}; |
| 47 | return std::memcmp(addr, &zero, sizeof(zero)) != 0; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** @brief gets the MAC address from the Inventory. |
| 51 | * @param[in] bus - DBUS Bus Object. |
| 52 | */ |
| 53 | std::string getfromInventory(sdbusplus::bus::bus& bus); |
| 54 | |
William A. Kennington III | a14879e | 2019-02-01 21:43:11 -0800 | [diff] [blame] | 55 | /* @brief Marshalls the bytes for a mac address into a MacAddr. |
| 56 | * @param[in] buf - The network byte order address |
| 57 | */ |
| 58 | MacAddr fromBuf(std::string_view buf); |
| 59 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 60 | /** @brief Converts the given mac address bytes into a string |
| 61 | * @param[in] bytes - The mac address |
| 62 | * @returns A valid mac address string |
| 63 | */ |
| 64 | std::string toString(const MacAddr& mac); |
| 65 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 66 | namespace internal |
| 67 | { |
| 68 | /** @brief Converts the given mac address into unsigned 64 bit integer |
| 69 | * @param[in] value - MAC address. |
| 70 | * @returns converted unsigned 64 bit number. |
| 71 | */ |
| 72 | inline uint64_t convertToInt(const std::string& value) |
| 73 | { |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 74 | struct ether_addr* addr = ether_aton(value.c_str()); |
| 75 | if (addr == nullptr) |
| 76 | { |
| 77 | return 0; |
| 78 | } |
| 79 | uint64_t ret = 0; |
| 80 | static_assert(sizeof(ret) >= sizeof(*addr)); |
| 81 | std::memcpy(&ret, addr, sizeof(*addr)); |
| 82 | return ret; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 83 | } |
| 84 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 85 | /** @brief Converts the lower nibble of a byte value to a hex digit |
| 86 | */ |
| 87 | inline char toHex(std::byte byte) |
| 88 | { |
| 89 | uint8_t val = std::to_integer<uint8_t>(byte) & 0xf; |
| 90 | return val < 10 ? '0' + val : 'A' + (val - 10); |
| 91 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 92 | } // namespace internal |
| 93 | } // namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 94 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 95 | constexpr auto networkdService = "systemd-networkd.service"; |
| 96 | constexpr auto timeSynchdService = "systemd-timesyncd.service"; |
| 97 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 98 | /* @brief converts the given subnet into prefix notation. |
| 99 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 100 | * @param[in] mask - Subnet Mask. |
| 101 | * @returns prefix. |
| 102 | */ |
| 103 | uint8_t toCidr(int addressFamily, const std::string& mask); |
| 104 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 105 | /* @brief converts a sockaddr for the specified address family into |
| 106 | * a type_safe InAddrAny. |
| 107 | * @param[in] addressFamily - The address family of the buf |
| 108 | * @param[in] buf - The network byte order address |
| 109 | */ |
| 110 | InAddrAny addrFromBuf(int addressFamily, std::string_view buf); |
| 111 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 112 | /* @brief converts the ip bytes into a string representation |
| 113 | * @param[in] addr - input ip address to convert. |
| 114 | * @returns String representation of the ip. |
| 115 | */ |
| 116 | std::string toString(const InAddrAny& addr); |
| 117 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 118 | /* @brief converts the prefix into subnetmask. |
| 119 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 120 | * @param[in] prefix - prefix length. |
| 121 | * @returns subnet mask. |
| 122 | */ |
| 123 | std::string toMask(int addressFamily, uint8_t prefix); |
| 124 | |
| 125 | /* @brief checks that the given ip address is link local or not. |
| 126 | * @param[in] address - IP address. |
| 127 | * @returns true if it is linklocal otherwise false. |
| 128 | */ |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 129 | bool isLinkLocalIP(const std::string& address); |
| 130 | |
| 131 | /* @brief checks that the given ip address valid or not. |
| 132 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 133 | * @param[in] address - IP address. |
| 134 | * @returns true if it is valid otherwise false. |
| 135 | */ |
| 136 | bool isValidIP(int addressFamily, const std::string& address); |
| 137 | |
| 138 | /* @brief checks that the given prefix is valid or not. |
| 139 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 140 | * @param[in] prefix - prefix length. |
| 141 | * @returns true if it is valid otherwise false. |
| 142 | */ |
| 143 | bool isValidPrefix(int addressFamily, uint8_t prefixLength); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 144 | |
Gunnar Mills | d75f049 | 2017-10-25 20:33:32 -0500 | [diff] [blame] | 145 | /* @brief gets the network section of the ip address. |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 146 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 147 | * @param[in] ipaddress - IP address. |
| 148 | * @param[in] prefix - prefix length. |
| 149 | * @returns network section of the ipaddress. |
| 150 | */ |
| 151 | std::string getNetworkID(int addressFamily, const std::string& ipaddress, |
| 152 | uint8_t prefix); |
| 153 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 154 | /** @brief Gets the map of interface and the associated |
| 155 | * address. |
| 156 | * @returns map of interface and the address. |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 157 | */ |
| 158 | IntfAddrMap getInterfaceAddrs(); |
| 159 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 160 | /** @brief Get all the interfaces from the system. |
| 161 | * @returns list of interface names. |
| 162 | */ |
| 163 | InterfaceList getInterfaces(); |
| 164 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 165 | /** @brief Delete the given interface. |
| 166 | * @param[in] intf - interface name. |
| 167 | */ |
| 168 | void deleteInterface(const std::string& intf); |
| 169 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 170 | /** @brief read the DHCP value from the configuration file |
| 171 | * @param[in] confDir - Network configuration directory. |
| 172 | * @param[in] intf - Interface name. |
| 173 | */ |
| 174 | bool getDHCPValue(const std::string& confDir, const std::string& intf); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 175 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 176 | namespace internal |
| 177 | { |
| 178 | |
| 179 | /* @brief runs the given command in child process. |
| 180 | * @param[in] path - path of the binary file which needs to be execeuted. |
| 181 | * @param[in] args - arguments of the command. |
| 182 | */ |
| 183 | void executeCommandinChildProcess(const char* path, char** args); |
| 184 | |
| 185 | } // namespace internal |
| 186 | |
| 187 | /* @brief runs the given command in child process. |
| 188 | * @param[in] path -path of the binary file which needs to be execeuted. |
| 189 | * @param[in] tArgs - arguments of the command. |
| 190 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 191 | template <typename... ArgTypes> |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 192 | void execute(const char* path, ArgTypes&&... tArgs) |
| 193 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 194 | using expandType = char* []; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 195 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 196 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 197 | |
| 198 | internal::executeCommandinChildProcess(path, args); |
| 199 | } |
| 200 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 201 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 202 | |
| 203 | class Descriptor |
| 204 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 205 | private: |
| 206 | /** default value */ |
| 207 | int fd = -1; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 208 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 209 | public: |
| 210 | Descriptor() = default; |
| 211 | Descriptor(const Descriptor&) = delete; |
| 212 | Descriptor& operator=(const Descriptor&) = delete; |
| 213 | Descriptor(Descriptor&&) = delete; |
| 214 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 215 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 216 | explicit Descriptor(int fd) : fd(fd) |
| 217 | { |
| 218 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 219 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 220 | /* @brief sets the internal file descriptor with the given descriptor |
| 221 | * and closes the old descriptor. |
| 222 | * @param[in] descriptor - File/Socket descriptor. |
| 223 | */ |
| 224 | void set(int descriptor) |
| 225 | { |
| 226 | // same descriptor given |
| 227 | if (fd == descriptor) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 228 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 229 | return; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 230 | } |
| 231 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 232 | // close the old descriptor |
| 233 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 234 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 235 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 236 | } |
| 237 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 238 | fd = descriptor; |
| 239 | } |
| 240 | |
| 241 | ~Descriptor() |
| 242 | { |
| 243 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 244 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 245 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 246 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | int operator()() const |
| 250 | { |
| 251 | return fd; |
| 252 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 253 | }; |
| 254 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 255 | } // namespace phosphor |