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 | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 55 | /** @brief Converts the given mac address bytes into a string |
| 56 | * @param[in] bytes - The mac address |
| 57 | * @returns A valid mac address string |
| 58 | */ |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame^] | 59 | std::string toString(const ether_addr& mac); |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 60 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 61 | namespace internal |
| 62 | { |
| 63 | /** @brief Converts the given mac address into unsigned 64 bit integer |
| 64 | * @param[in] value - MAC address. |
| 65 | * @returns converted unsigned 64 bit number. |
| 66 | */ |
| 67 | inline uint64_t convertToInt(const std::string& value) |
| 68 | { |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 69 | struct ether_addr* addr = ether_aton(value.c_str()); |
| 70 | if (addr == nullptr) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | uint64_t ret = 0; |
| 75 | static_assert(sizeof(ret) >= sizeof(*addr)); |
| 76 | std::memcpy(&ret, addr, sizeof(*addr)); |
| 77 | return ret; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 78 | } |
| 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | } // namespace internal |
| 81 | } // namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 82 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 83 | constexpr auto networkdService = "systemd-networkd.service"; |
| 84 | constexpr auto timeSynchdService = "systemd-timesyncd.service"; |
| 85 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 86 | /* @brief converts the given subnet into prefix notation. |
| 87 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 88 | * @param[in] mask - Subnet Mask. |
| 89 | * @returns prefix. |
| 90 | */ |
| 91 | uint8_t toCidr(int addressFamily, const std::string& mask); |
| 92 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 93 | /* @brief converts a sockaddr for the specified address family into |
| 94 | * a type_safe InAddrAny. |
| 95 | * @param[in] addressFamily - The address family of the buf |
| 96 | * @param[in] buf - The network byte order address |
| 97 | */ |
| 98 | InAddrAny addrFromBuf(int addressFamily, std::string_view buf); |
| 99 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 100 | /* @brief converts the ip bytes into a string representation |
| 101 | * @param[in] addr - input ip address to convert. |
| 102 | * @returns String representation of the ip. |
| 103 | */ |
| 104 | std::string toString(const InAddrAny& addr); |
| 105 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 106 | /* @brief converts the prefix into subnetmask. |
| 107 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 108 | * @param[in] prefix - prefix length. |
| 109 | * @returns subnet mask. |
| 110 | */ |
| 111 | std::string toMask(int addressFamily, uint8_t prefix); |
| 112 | |
| 113 | /* @brief checks that the given ip address is link local or not. |
| 114 | * @param[in] address - IP address. |
| 115 | * @returns true if it is linklocal otherwise false. |
| 116 | */ |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 117 | bool isLinkLocalIP(const std::string& address); |
| 118 | |
| 119 | /* @brief checks that the given ip address valid or not. |
| 120 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 121 | * @param[in] address - IP address. |
| 122 | * @returns true if it is valid otherwise false. |
| 123 | */ |
| 124 | bool isValidIP(int addressFamily, const std::string& address); |
| 125 | |
| 126 | /* @brief checks that the given prefix is valid or not. |
| 127 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 128 | * @param[in] prefix - prefix length. |
| 129 | * @returns true if it is valid otherwise false. |
| 130 | */ |
| 131 | bool isValidPrefix(int addressFamily, uint8_t prefixLength); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 132 | |
Gunnar Mills | d75f049 | 2017-10-25 20:33:32 -0500 | [diff] [blame] | 133 | /* @brief gets the network section of the ip address. |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 134 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 135 | * @param[in] ipaddress - IP address. |
| 136 | * @param[in] prefix - prefix length. |
| 137 | * @returns network section of the ipaddress. |
| 138 | */ |
| 139 | std::string getNetworkID(int addressFamily, const std::string& ipaddress, |
| 140 | uint8_t prefix); |
| 141 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 142 | /** @brief Gets the map of interface and the associated |
| 143 | * address. |
| 144 | * @returns map of interface and the address. |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 145 | */ |
| 146 | IntfAddrMap getInterfaceAddrs(); |
| 147 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 148 | /** @brief Get all the interfaces from the system. |
| 149 | * @returns list of interface names. |
| 150 | */ |
| 151 | InterfaceList getInterfaces(); |
| 152 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 153 | /** @brief Delete the given interface. |
| 154 | * @param[in] intf - interface name. |
| 155 | */ |
| 156 | void deleteInterface(const std::string& intf); |
| 157 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 158 | /** @brief read the DHCP value from the configuration file |
| 159 | * @param[in] confDir - Network configuration directory. |
| 160 | * @param[in] intf - Interface name. |
| 161 | */ |
| 162 | bool getDHCPValue(const std::string& confDir, const std::string& intf); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 163 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 164 | namespace internal |
| 165 | { |
| 166 | |
| 167 | /* @brief runs the given command in child process. |
| 168 | * @param[in] path - path of the binary file which needs to be execeuted. |
| 169 | * @param[in] args - arguments of the command. |
| 170 | */ |
| 171 | void executeCommandinChildProcess(const char* path, char** args); |
| 172 | |
| 173 | } // namespace internal |
| 174 | |
| 175 | /* @brief runs the given command in child process. |
| 176 | * @param[in] path -path of the binary file which needs to be execeuted. |
| 177 | * @param[in] tArgs - arguments of the command. |
| 178 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 179 | template <typename... ArgTypes> |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 180 | void execute(const char* path, ArgTypes&&... tArgs) |
| 181 | { |
William A. Kennington III | 0420c6a | 2019-06-27 14:38:17 -0700 | [diff] [blame] | 182 | using expandType = char*[]; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 183 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 184 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 185 | |
| 186 | internal::executeCommandinChildProcess(path, args); |
| 187 | } |
| 188 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 189 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 190 | |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 191 | /** @brief Copies data from a buffer into a copyable type |
| 192 | * |
| 193 | * @param[in] data - The data buffer being extracted from |
| 194 | * @param[in] emsg - The message to print if extraction fails |
| 195 | * @return The copyable type with data populated |
| 196 | */ |
| 197 | template <typename T> |
| 198 | T copyFrom(std::string_view data, const char* emsg = "Extract Failed") |
| 199 | { |
| 200 | static_assert(std::is_trivially_copyable_v<T>); |
| 201 | T ret; |
| 202 | if (data.size() < sizeof(ret)) |
| 203 | { |
| 204 | throw std::runtime_error(emsg); |
| 205 | } |
| 206 | std::memcpy(&ret, data.data(), sizeof(ret)); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | /** @brief Extracts data from a buffer into a copyable type |
| 211 | * Updates the data buffer to show that data was removed |
| 212 | * |
| 213 | * @param[in,out] data - The data buffer being extracted from |
| 214 | * @param[in] emsg - The message to print if extraction fails |
| 215 | * @return The copyable type with data populated |
| 216 | */ |
| 217 | template <typename T> |
| 218 | T extract(std::string_view& data, const char* emsg = "Extract Failed") |
| 219 | { |
| 220 | T ret = copyFrom<T>(data, emsg); |
| 221 | data.remove_prefix(sizeof(ret)); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | /** @brief Compares two of the same trivially copyable types |
| 226 | * |
| 227 | * @param[in] a - The data buffer being extracted from |
| 228 | * @param[in] b - The message to print if extraction fails |
| 229 | * @return True if the parameters are bitwise identical |
| 230 | */ |
| 231 | template <typename T> |
| 232 | bool equal(const T& a, const T& b) |
| 233 | { |
| 234 | static_assert(std::is_trivially_copyable_v<T>); |
| 235 | return memcmp(&a, &b, sizeof(T)) == 0; |
| 236 | } |
| 237 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 238 | class Descriptor |
| 239 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 240 | private: |
| 241 | /** default value */ |
| 242 | int fd = -1; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 243 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 244 | public: |
| 245 | Descriptor() = default; |
| 246 | Descriptor(const Descriptor&) = delete; |
| 247 | Descriptor& operator=(const Descriptor&) = delete; |
| 248 | Descriptor(Descriptor&&) = delete; |
| 249 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 250 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 251 | explicit Descriptor(int fd) : fd(fd) |
| 252 | { |
| 253 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 254 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 255 | /* @brief sets the internal file descriptor with the given descriptor |
| 256 | * and closes the old descriptor. |
| 257 | * @param[in] descriptor - File/Socket descriptor. |
| 258 | */ |
| 259 | void set(int descriptor) |
| 260 | { |
| 261 | // same descriptor given |
| 262 | if (fd == descriptor) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 263 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 264 | return; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 267 | // close the old descriptor |
| 268 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 269 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 270 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 271 | } |
| 272 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 273 | fd = descriptor; |
| 274 | } |
| 275 | |
| 276 | ~Descriptor() |
| 277 | { |
| 278 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 279 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 280 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 281 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | int operator()() const |
| 285 | { |
| 286 | return fd; |
| 287 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 288 | }; |
| 289 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 290 | } // namespace phosphor |