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