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 | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 12 | #include <filesystem> |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 13 | #include <optional> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 14 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 15 | #include <string> |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 16 | #include <string_view> |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 17 | #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 18 | |
| 19 | namespace phosphor |
| 20 | { |
| 21 | namespace network |
| 22 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 23 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 24 | using EthernetInterfaceIntf = |
| 25 | sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; |
| 26 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 27 | constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; |
| 28 | constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; |
John Wang | 4d94ecd | 2021-04-07 17:37:03 +0800 | [diff] [blame] | 29 | constexpr auto IPV6_MAX_PREFIX_LENGTH = 128; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 30 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 31 | namespace mac_address |
| 32 | { |
| 33 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 34 | /** @brief gets the MAC address from the Inventory. |
| 35 | * @param[in] bus - DBUS Bus Object. |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 36 | * @param[in] intfName - Interface name |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 37 | */ |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 38 | 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] | 39 | |
| 40 | /** @brief Converts the given mac address into byte form |
| 41 | * @param[in] str - The mac address in human readable form |
| 42 | * @returns A mac address in network byte order |
| 43 | * @throws std::runtime_error for bad mac |
| 44 | */ |
William A. Kennington III | 1c77602 | 2022-01-05 14:12:16 -0800 | [diff] [blame] | 45 | ether_addr fromString(const char* str); |
| 46 | inline ether_addr fromString(const std::string& str) |
| 47 | { |
| 48 | return fromString(str.c_str()); |
| 49 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 50 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 51 | /** @brief Converts the given mac address bytes into a string |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 52 | * @param[in] mac - The mac address |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 53 | * @returns A valid mac address string |
| 54 | */ |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame] | 55 | std::string toString(const ether_addr& mac); |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 56 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 57 | /** @brief Determines if the mac address is empty |
| 58 | * @param[in] mac - The mac address |
| 59 | * @return True if 00:00:00:00:00:00 |
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 | bool isEmpty(const ether_addr& mac); |
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 | /** @brief Determines if the mac address is a multicast address |
| 64 | * @param[in] mac - The mac address |
| 65 | * @return True if multicast bit is set |
| 66 | */ |
| 67 | bool isMulticast(const ether_addr& mac); |
| 68 | |
| 69 | /** @brief Determines if the mac address is a unicast address |
| 70 | * @param[in] mac - The mac address |
| 71 | * @return True if not multicast or empty |
| 72 | */ |
| 73 | bool isUnicast(const ether_addr& mac); |
| 74 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 75 | } // namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 76 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 77 | constexpr auto networkdService = "systemd-networkd.service"; |
| 78 | constexpr auto timeSynchdService = "systemd-timesyncd.service"; |
| 79 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 80 | /* @brief converts a sockaddr for the specified address family into |
| 81 | * a type_safe InAddrAny. |
| 82 | * @param[in] addressFamily - The address family of the buf |
| 83 | * @param[in] buf - The network byte order address |
| 84 | */ |
| 85 | InAddrAny addrFromBuf(int addressFamily, std::string_view buf); |
| 86 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 87 | /* @brief converts the ip bytes into a string representation |
| 88 | * @param[in] addr - input ip address to convert. |
| 89 | * @returns String representation of the ip. |
| 90 | */ |
| 91 | std::string toString(const InAddrAny& addr); |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 92 | std::string toString(const struct in_addr& addr); |
| 93 | std::string toString(const struct in6_addr& addr); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 94 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 95 | /* @brief checks that the given ip address valid or not. |
| 96 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 97 | * @param[in] address - IP address. |
| 98 | * @returns true if it is valid otherwise false. |
| 99 | */ |
| 100 | bool isValidIP(int addressFamily, const std::string& address); |
| 101 | |
| 102 | /* @brief checks that the given prefix is valid or not. |
| 103 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 104 | * @param[in] prefix - prefix length. |
| 105 | * @returns true if it is valid otherwise false. |
| 106 | */ |
| 107 | bool isValidPrefix(int addressFamily, uint8_t prefixLength); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 108 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 109 | /** @brief Get all the interfaces from the system. |
| 110 | * @returns list of interface names. |
| 111 | */ |
| 112 | InterfaceList getInterfaces(); |
| 113 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 114 | /** @brief Delete the given interface. |
| 115 | * @param[in] intf - interface name. |
| 116 | */ |
| 117 | void deleteInterface(const std::string& intf); |
| 118 | |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 119 | /** @brief Converts the interface name into a u-boot environment |
| 120 | * variable that would hold its ethernet address. |
| 121 | * |
| 122 | * @param[in] intf - interface name |
| 123 | * @return The name of th environment key |
| 124 | */ |
| 125 | std::optional<std::string> interfaceToUbootEthAddr(const char* intf); |
| 126 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 127 | /** @brief read the IPv6AcceptRA value from the configuration file |
| 128 | * @param[in] config - The parsed configuration. |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 129 | */ |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 130 | bool getIPv6AcceptRA(const config::Parser& config); |
| 131 | |
| 132 | /** @brief read the DHCP value from the configuration file |
| 133 | * @param[in] config - The parsed configuration. |
| 134 | */ |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 135 | struct DHCPVal |
| 136 | { |
| 137 | bool v4, v6; |
| 138 | }; |
| 139 | DHCPVal getDHCPValue(const config::Parser& config); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 140 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame^] | 141 | /** @brief Read a boolean DHCP property from a conf file |
| 142 | * @param[in] config - The parsed configuration. |
| 143 | * @param[in] key - The property name. |
| 144 | */ |
| 145 | bool getDHCPProp(const config::Parser& config, std::string_view key); |
| 146 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 147 | namespace internal |
| 148 | { |
| 149 | |
| 150 | /* @brief runs the given command in child process. |
| 151 | * @param[in] path - path of the binary file which needs to be execeuted. |
| 152 | * @param[in] args - arguments of the command. |
| 153 | */ |
| 154 | void executeCommandinChildProcess(const char* path, char** args); |
| 155 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 156 | /** @brief Get ignored interfaces from environment */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 157 | std::string_view getIgnoredInterfacesEnv(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 158 | |
| 159 | /** @brief Parse the comma separated interface names */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 160 | std::set<std::string_view> parseInterfaces(std::string_view interfaces); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 161 | |
| 162 | /** @brief Get the ignored interfaces */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 163 | const std::set<std::string_view>& getIgnoredInterfaces(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 164 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 165 | } // namespace internal |
| 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] tArgs - arguments of the command. |
| 170 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 171 | template <typename... ArgTypes> |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 172 | void execute(const char* path, ArgTypes&&... tArgs) |
| 173 | { |
William A. Kennington III | 0420c6a | 2019-06-27 14:38:17 -0700 | [diff] [blame] | 174 | using expandType = char*[]; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 175 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 176 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 177 | |
| 178 | internal::executeCommandinChildProcess(path, args); |
| 179 | } |
| 180 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 181 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 182 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 183 | } // namespace phosphor |