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> |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace network |
| 21 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 22 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 23 | using EthernetInterfaceIntf = |
| 24 | sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; |
| 25 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 26 | constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; |
| 27 | constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; |
John Wang | 4d94ecd | 2021-04-07 17:37:03 +0800 | [diff] [blame] | 28 | constexpr auto IPV6_MAX_PREFIX_LENGTH = 128; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 29 | constexpr auto IPV4_PREFIX = "169.254"; |
| 30 | constexpr auto IPV6_PREFIX = "fe80"; |
| 31 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 32 | namespace mac_address |
| 33 | { |
| 34 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 35 | /** @brief gets the MAC address from the Inventory. |
| 36 | * @param[in] bus - DBUS Bus Object. |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 37 | * @param[in] intfName - Interface name |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 38 | */ |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 39 | ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 40 | |
| 41 | /** @brief Converts the given mac address into byte form |
| 42 | * @param[in] str - The mac address in human readable form |
| 43 | * @returns A mac address in network byte order |
| 44 | * @throws std::runtime_error for bad mac |
| 45 | */ |
William A. Kennington III | 1c77602 | 2022-01-05 14:12:16 -0800 | [diff] [blame] | 46 | ether_addr fromString(const char* str); |
| 47 | inline ether_addr fromString(const std::string& str) |
| 48 | { |
| 49 | return fromString(str.c_str()); |
| 50 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 51 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 52 | /** @brief Converts the given mac address bytes into a string |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 53 | * @param[in] mac - The mac address |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 54 | * @returns A valid mac address string |
| 55 | */ |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame] | 56 | std::string toString(const ether_addr& mac); |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 57 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 58 | /** @brief Determines if the mac address is empty |
| 59 | * @param[in] mac - The mac address |
| 60 | * @return True if 00:00:00:00:00:00 |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 61 | */ |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 62 | bool isEmpty(const ether_addr& mac); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 63 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 64 | /** @brief Determines if the mac address is a multicast address |
| 65 | * @param[in] mac - The mac address |
| 66 | * @return True if multicast bit is set |
| 67 | */ |
| 68 | bool isMulticast(const ether_addr& mac); |
| 69 | |
| 70 | /** @brief Determines if the mac address is a unicast address |
| 71 | * @param[in] mac - The mac address |
| 72 | * @return True if not multicast or empty |
| 73 | */ |
| 74 | bool isUnicast(const ether_addr& mac); |
| 75 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 76 | } // namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 77 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 78 | constexpr auto networkdService = "systemd-networkd.service"; |
| 79 | constexpr auto timeSynchdService = "systemd-timesyncd.service"; |
| 80 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 81 | /* @brief converts the given subnet into prefix notation. |
| 82 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 83 | * @param[in] mask - Subnet Mask. |
| 84 | * @returns prefix. |
| 85 | */ |
| 86 | uint8_t toCidr(int addressFamily, const std::string& mask); |
| 87 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 88 | /* @brief converts a sockaddr for the specified address family into |
| 89 | * a type_safe InAddrAny. |
| 90 | * @param[in] addressFamily - The address family of the buf |
| 91 | * @param[in] buf - The network byte order address |
| 92 | */ |
| 93 | InAddrAny addrFromBuf(int addressFamily, std::string_view buf); |
| 94 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 95 | /* @brief converts the ip bytes into a string representation |
| 96 | * @param[in] addr - input ip address to convert. |
| 97 | * @returns String representation of the ip. |
| 98 | */ |
| 99 | std::string toString(const InAddrAny& addr); |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 100 | std::string toString(const struct in_addr& addr); |
| 101 | std::string toString(const struct in6_addr& addr); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 102 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 103 | /* @brief converts the prefix into subnetmask. |
| 104 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 105 | * @param[in] prefix - prefix length. |
| 106 | * @returns subnet mask. |
| 107 | */ |
| 108 | std::string toMask(int addressFamily, uint8_t prefix); |
| 109 | |
| 110 | /* @brief checks that the given ip address is link local or not. |
| 111 | * @param[in] address - IP address. |
| 112 | * @returns true if it is linklocal otherwise false. |
| 113 | */ |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 114 | bool isLinkLocalIP(const std::string& address); |
| 115 | |
| 116 | /* @brief checks that the given ip address valid or not. |
| 117 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 118 | * @param[in] address - IP address. |
| 119 | * @returns true if it is valid otherwise false. |
| 120 | */ |
| 121 | bool isValidIP(int addressFamily, const std::string& address); |
| 122 | |
| 123 | /* @brief checks that the given prefix is valid or not. |
| 124 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 125 | * @param[in] prefix - prefix length. |
| 126 | * @returns true if it is valid otherwise false. |
| 127 | */ |
| 128 | bool isValidPrefix(int addressFamily, uint8_t prefixLength); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 129 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 130 | /** @brief Gets the map of interface and the associated |
| 131 | * address. |
| 132 | * @returns map of interface and the address. |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 133 | */ |
| 134 | IntfAddrMap getInterfaceAddrs(); |
| 135 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 136 | /** @brief Get all the interfaces from the system. |
| 137 | * @returns list of interface names. |
| 138 | */ |
| 139 | InterfaceList getInterfaces(); |
| 140 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 141 | /** @brief Delete the given interface. |
| 142 | * @param[in] intf - interface name. |
| 143 | */ |
| 144 | void deleteInterface(const std::string& intf); |
| 145 | |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 146 | /** @brief Converts the interface name into a u-boot environment |
| 147 | * variable that would hold its ethernet address. |
| 148 | * |
| 149 | * @param[in] intf - interface name |
| 150 | * @return The name of th environment key |
| 151 | */ |
| 152 | std::optional<std::string> interfaceToUbootEthAddr(const char* intf); |
| 153 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 154 | /** @brief read the DHCP value from the configuration file |
| 155 | * @param[in] confDir - Network configuration directory. |
| 156 | * @param[in] intf - Interface name. |
| 157 | */ |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 158 | EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir, |
| 159 | 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 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 170 | /** @brief Get ignored interfaces from environment */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 171 | std::string_view getIgnoredInterfacesEnv(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 172 | |
| 173 | /** @brief Parse the comma separated interface names */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 174 | std::set<std::string_view> parseInterfaces(std::string_view interfaces); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 175 | |
| 176 | /** @brief Get the ignored interfaces */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 177 | const std::set<std::string_view>& getIgnoredInterfaces(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 178 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 179 | } // namespace internal |
| 180 | |
| 181 | /* @brief runs the given command in child process. |
| 182 | * @param[in] path -path of the binary file which needs to be execeuted. |
| 183 | * @param[in] tArgs - arguments of the command. |
| 184 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 185 | template <typename... ArgTypes> |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 186 | void execute(const char* path, ArgTypes&&... tArgs) |
| 187 | { |
William A. Kennington III | 0420c6a | 2019-06-27 14:38:17 -0700 | [diff] [blame] | 188 | using expandType = char*[]; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 189 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 190 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 191 | |
| 192 | internal::executeCommandinChildProcess(path, args); |
| 193 | } |
| 194 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 195 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 196 | |
| 197 | class Descriptor |
| 198 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 199 | private: |
| 200 | /** default value */ |
| 201 | int fd = -1; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 202 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 203 | public: |
| 204 | Descriptor() = default; |
| 205 | Descriptor(const Descriptor&) = delete; |
| 206 | Descriptor& operator=(const Descriptor&) = delete; |
| 207 | Descriptor(Descriptor&&) = delete; |
| 208 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 209 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 210 | explicit Descriptor(int fd) : fd(fd) |
| 211 | { |
| 212 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 213 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 214 | /* @brief sets the internal file descriptor with the given descriptor |
| 215 | * and closes the old descriptor. |
| 216 | * @param[in] descriptor - File/Socket descriptor. |
| 217 | */ |
| 218 | void set(int descriptor) |
| 219 | { |
| 220 | // same descriptor given |
| 221 | if (fd == descriptor) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 222 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 223 | return; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 224 | } |
| 225 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 226 | // close the old descriptor |
| 227 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 228 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 229 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 230 | } |
| 231 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 232 | fd = descriptor; |
| 233 | } |
| 234 | |
| 235 | ~Descriptor() |
| 236 | { |
| 237 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 238 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 239 | close(fd); |
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 | } |
| 242 | |
| 243 | int operator()() const |
| 244 | { |
| 245 | return fd; |
| 246 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 247 | }; |
| 248 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 249 | } // namespace phosphor |