Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 1 | #pragma once |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 2 | #include "types.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 4 | #include <netinet/ether.h> |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 5 | #include <netinet/in.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 8 | #include <cstring> |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 9 | #include <filesystem> |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 10 | #include <optional> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 12 | #include <stdplus/zstring.hpp> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 13 | #include <string> |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 14 | #include <string_view> |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 15 | #include <unordered_set> |
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 | { |
William A. Kennington III | 20efa79 | 2022-10-04 17:19:08 -0700 | [diff] [blame] | 22 | namespace config |
| 23 | { |
| 24 | class Parser; |
| 25 | } |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 26 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 27 | using EthernetInterfaceIntf = |
| 28 | sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; |
| 29 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 30 | namespace mac_address |
| 31 | { |
| 32 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 33 | /** @brief gets the MAC address from the Inventory. |
| 34 | * @param[in] bus - DBUS Bus Object. |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 35 | * @param[in] intfName - Interface name |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 36 | */ |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 37 | 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] | 38 | |
| 39 | /** @brief Converts the given mac address into byte form |
| 40 | * @param[in] str - The mac address in human readable form |
| 41 | * @returns A mac address in network byte order |
| 42 | * @throws std::runtime_error for bad mac |
| 43 | */ |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 44 | ether_addr fromString(std::string_view str); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 45 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 46 | /** @brief Converts the given mac address bytes into a string |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 47 | * @param[in] mac - The mac address |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 48 | * @returns A valid mac address string |
| 49 | */ |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame] | 50 | std::string toString(const ether_addr& mac); |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 51 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 52 | /** @brief Determines if the mac address is empty |
| 53 | * @param[in] mac - The mac address |
| 54 | * @return True if 00:00:00:00:00:00 |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 55 | */ |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 56 | bool isEmpty(const ether_addr& mac); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [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 a multicast address |
| 59 | * @param[in] mac - The mac address |
| 60 | * @return True if multicast bit is set |
| 61 | */ |
| 62 | bool isMulticast(const ether_addr& mac); |
| 63 | |
| 64 | /** @brief Determines if the mac address is a unicast address |
| 65 | * @param[in] mac - The mac address |
| 66 | * @return True if not multicast or empty |
| 67 | */ |
| 68 | bool isUnicast(const ether_addr& mac); |
| 69 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 70 | } // namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 71 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 72 | constexpr auto networkdService = "systemd-networkd.service"; |
| 73 | constexpr auto timeSynchdService = "systemd-timesyncd.service"; |
| 74 | |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 75 | template <int family> |
| 76 | struct FamilyTraits |
| 77 | { |
| 78 | }; |
| 79 | |
| 80 | template <> |
| 81 | struct FamilyTraits<AF_INET> |
| 82 | { |
| 83 | using addr = in_addr; |
William A. Kennington III | d07c086 | 2022-10-07 14:17:05 -0700 | [diff] [blame] | 84 | static constexpr std::size_t strlen = INET_ADDRSTRLEN; |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | template <> |
| 88 | struct FamilyTraits<AF_INET6> |
| 89 | { |
| 90 | using addr = in6_addr; |
William A. Kennington III | d07c086 | 2022-10-07 14:17:05 -0700 | [diff] [blame] | 91 | static constexpr std::size_t strlen = INET6_ADDRSTRLEN; |
| 92 | }; |
| 93 | |
| 94 | template <typename Addr> |
| 95 | struct AddrToFamily |
| 96 | { |
| 97 | }; |
| 98 | |
| 99 | template <> |
| 100 | struct AddrToFamily<in_addr> |
| 101 | { |
| 102 | static constexpr int value = AF_INET; |
| 103 | }; |
| 104 | |
| 105 | template <> |
| 106 | struct AddrToFamily<in6_addr> |
| 107 | { |
| 108 | static constexpr int value = AF_INET6; |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 111 | /* @brief converts a sockaddr for the specified address family into |
| 112 | * a type_safe InAddrAny. |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 113 | * @param[in] family - The address family of the buf |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 114 | * @param[in] buf - The network byte order address |
| 115 | */ |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 116 | template <int family> |
| 117 | typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf); |
| 118 | InAddrAny addrFromBuf(int family, std::string_view buf); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 119 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 120 | /* @brief converts the ip bytes into a string representation |
| 121 | * @param[in] addr - input ip address to convert. |
| 122 | * @returns String representation of the ip. |
| 123 | */ |
William A. Kennington III | d07c086 | 2022-10-07 14:17:05 -0700 | [diff] [blame] | 124 | template <typename Addr> |
| 125 | std::string toString(const Addr& addr); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 126 | std::string toString(const InAddrAny& addr); |
| 127 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 128 | /* @brief checks that the given ip address valid or not. |
William A. Kennington III | 9f22830 | 2022-10-07 14:02:49 -0700 | [diff] [blame] | 129 | * @param[in] family - IP address family(AF_INET/AF_INET6). |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 130 | * @param[in] address - IP address. |
| 131 | * @returns true if it is valid otherwise false. |
| 132 | */ |
William A. Kennington III | 9f22830 | 2022-10-07 14:02:49 -0700 | [diff] [blame] | 133 | bool isValidIP(int family, stdplus::const_zstring address) noexcept; |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 134 | bool isValidIP(stdplus::const_zstring address) noexcept; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 135 | |
| 136 | /* @brief checks that the given prefix is valid or not. |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 137 | * @param[in] family - IP address family(AF_INET/AF_INET6). |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 138 | * @param[in] prefix - prefix length. |
| 139 | * @returns true if it is valid otherwise false. |
| 140 | */ |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 141 | template <int family> |
| 142 | constexpr bool isValidPrefix(uint8_t prefix) noexcept |
| 143 | { |
| 144 | return prefix <= sizeof(typename FamilyTraits<family>::addr) * 8; |
| 145 | } |
| 146 | bool isValidPrefix(int family, uint8_t prefixLength); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 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 | */ |
William A. Kennington III | 9644479 | 2022-10-05 15:16:22 -0700 | [diff] [blame^] | 151 | string_uset getSystemInterfaces(); |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 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 | */ |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 156 | void deleteInterface(stdplus::const_zstring intf); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 157 | |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 158 | /** @brief Converts the interface name into a u-boot environment |
| 159 | * variable that would hold its ethernet address. |
| 160 | * |
| 161 | * @param[in] intf - interface name |
| 162 | * @return The name of th environment key |
| 163 | */ |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 164 | std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf); |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 165 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 166 | /** @brief read the IPv6AcceptRA value from the configuration file |
| 167 | * @param[in] config - The parsed configuration. |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 168 | */ |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 169 | bool getIPv6AcceptRA(const config::Parser& config); |
| 170 | |
| 171 | /** @brief read the DHCP value from the configuration file |
| 172 | * @param[in] config - The parsed configuration. |
| 173 | */ |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 174 | struct DHCPVal |
| 175 | { |
| 176 | bool v4, v6; |
| 177 | }; |
| 178 | DHCPVal getDHCPValue(const config::Parser& config); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 179 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 180 | /** @brief Read a boolean DHCP property from a conf file |
| 181 | * @param[in] config - The parsed configuration. |
| 182 | * @param[in] key - The property name. |
| 183 | */ |
| 184 | bool getDHCPProp(const config::Parser& config, std::string_view key); |
| 185 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 186 | namespace internal |
| 187 | { |
| 188 | |
| 189 | /* @brief runs the given command in child process. |
| 190 | * @param[in] path - path of the binary file which needs to be execeuted. |
| 191 | * @param[in] args - arguments of the command. |
| 192 | */ |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 193 | void executeCommandinChildProcess(stdplus::const_zstring path, char** args); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 194 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 195 | /** @brief Get ignored interfaces from environment */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 196 | std::string_view getIgnoredInterfacesEnv(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 197 | |
| 198 | /** @brief Parse the comma separated interface names */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 199 | std::unordered_set<std::string_view> |
| 200 | parseInterfaces(std::string_view interfaces); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 201 | |
| 202 | /** @brief Get the ignored interfaces */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 203 | const std::unordered_set<std::string_view>& getIgnoredInterfaces(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 204 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 205 | } // namespace internal |
| 206 | |
| 207 | /* @brief runs the given command in child process. |
| 208 | * @param[in] path -path of the binary file which needs to be execeuted. |
| 209 | * @param[in] tArgs - arguments of the command. |
| 210 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 211 | template <typename... ArgTypes> |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 212 | void execute(stdplus::const_zstring path, ArgTypes&&... tArgs) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 213 | { |
William A. Kennington III | 0420c6a | 2019-06-27 14:38:17 -0700 | [diff] [blame] | 214 | using expandType = char*[]; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 215 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 216 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 217 | |
| 218 | internal::executeCommandinChildProcess(path, args); |
| 219 | } |
| 220 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 221 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 222 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 223 | } // namespace phosphor |