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