blob: cc7ab030df89a6ca6b49a101e919ea4ed8ffa263 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
2
Ratan Gupta068a8cf2017-07-11 19:18:29 +05303#include "config.h"
Gunnar Mills57d9c502018-09-14 14:42:34 -05004
Alvin Wang38a63c32019-08-29 22:56:46 +08005#include "ethernet_interface.hpp"
Ratan Gupta3681a502017-06-17 19:20:04 +05306#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07007
William A. Kennington III4966e962019-04-08 01:58:10 -07008#include <netinet/ether.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07009#include <unistd.h>
10
William A. Kennington III4966e962019-04-08 01:58:10 -070011#include <cstring>
William A. Kennington IIIa520a392022-08-08 12:17:34 -070012#include <filesystem>
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070013#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <sdbusplus/bus.hpp>
William A. Kennington IIId27410f2019-01-30 17:15:43 -080015#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080016#include <string_view>
Johnathan Mantey817012a2020-01-30 15:07:39 -080017#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053018
19namespace phosphor
20{
21namespace network
22{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050023
Johnathan Mantey817012a2020-01-30 15:07:39 -080024using EthernetInterfaceIntf =
25 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
26
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050027constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
28constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
John Wang4d94ecd2021-04-07 17:37:03 +080029constexpr auto IPV6_MAX_PREFIX_LENGTH = 128;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050030
Ratan Guptabd303b12017-08-18 17:10:07 +053031namespace mac_address
32{
33
Ratan Guptabd303b12017-08-18 17:10:07 +053034/** @brief gets the MAC address from the Inventory.
35 * @param[in] bus - DBUS Bus Object.
Alvin Wang38a63c32019-08-29 22:56:46 +080036 * @param[in] intfName - Interface name
Ratan Guptabd303b12017-08-18 17:10:07 +053037 */
Patrick Williamsc38b0712022-07-22 19:26:54 -050038ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName);
William A. Kennington III1137a972019-04-20 20:49:58 -070039
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 III1c776022022-01-05 14:12:16 -080045ether_addr fromString(const char* str);
46inline ether_addr fromString(const std::string& str)
47{
48 return fromString(str.c_str());
49}
Ratan Guptabd303b12017-08-18 17:10:07 +053050
William A. Kennington IIId27410f2019-01-30 17:15:43 -080051/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070052 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080053 * @returns A valid mac address string
54 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070055std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080056
William A. Kennington III1137a972019-04-20 20:49:58 -070057/** @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 Guptabd303b12017-08-18 17:10:07 +053060 */
William A. Kennington III1137a972019-04-20 20:49:58 -070061bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053062
William A. Kennington III1137a972019-04-20 20:49:58 -070063/** @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 */
67bool 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 */
73bool isUnicast(const ether_addr& mac);
74
Gunnar Mills57d9c502018-09-14 14:42:34 -050075} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053076
Ratan Gupta497c0c92017-08-22 19:15:59 +053077constexpr auto networkdService = "systemd-networkd.service";
78constexpr auto timeSynchdService = "systemd-timesyncd.service";
79
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080080/* @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 */
85InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
86
William A. Kennington III5058f572019-01-30 17:18:14 -080087/* @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 */
91std::string toString(const InAddrAny& addr);
Alexander Filippov983da552021-02-08 15:26:54 +030092std::string toString(const struct in_addr& addr);
93std::string toString(const struct in6_addr& addr);
William A. Kennington III5058f572019-01-30 17:18:14 -080094
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050095/* @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 */
100bool 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 */
107bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530108
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530109/** @brief Get all the interfaces from the system.
110 * @returns list of interface names.
111 */
112InterfaceList getInterfaces();
113
Ratan Guptabc886292017-07-25 18:29:57 +0530114/** @brief Delete the given interface.
115 * @param[in] intf - interface name.
116 */
117void deleteInterface(const std::string& intf);
118
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700119/** @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 */
125std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
126
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700127/** @brief read the IPv6AcceptRA value from the configuration file
128 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +0530129 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700130bool 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 III8060c0d2022-08-18 19:19:34 -0700135struct DHCPVal
136{
137 bool v4, v6;
138};
139DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +0530140
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700141/** @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 */
145bool getDHCPProp(const config::Parser& config, std::string_view key);
146
Ratan Guptabd303b12017-08-18 17:10:07 +0530147namespace 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 */
154void executeCommandinChildProcess(const char* path, char** args);
155
Lei YU307554e2021-03-18 14:56:50 +0800156/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700157std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800158
159/** @brief Parse the comma separated interface names */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700160std::set<std::string_view> parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800161
162/** @brief Get the ignored interfaces */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700163const std::set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800164
Ratan Guptabd303b12017-08-18 17:10:07 +0530165} // 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 Mills57d9c502018-09-14 14:42:34 -0500171template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530172void execute(const char* path, ArgTypes&&... tArgs)
173{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700174 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530175
Gunnar Mills57d9c502018-09-14 14:42:34 -0500176 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530177
178 internal::executeCommandinChildProcess(path, args);
179}
180
Gunnar Mills57d9c502018-09-14 14:42:34 -0500181} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530182
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183} // namespace phosphor