blob: 43c231fe288e9510259d4920dcbd43d37023a466 [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 III7b9e8bd2019-04-23 19:31:31 -070012#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -070013#include <sdbusplus/bus.hpp>
William A. Kennington IIId27410f2019-01-30 17:15:43 -080014#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080015#include <string_view>
Johnathan Mantey817012a2020-01-30 15:07:39 -080016#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053017
18namespace phosphor
19{
20namespace network
21{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050022
Johnathan Mantey817012a2020-01-30 15:07:39 -080023using EthernetInterfaceIntf =
24 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
25
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050026constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
27constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
John Wang4d94ecd2021-04-07 17:37:03 +080028constexpr auto IPV6_MAX_PREFIX_LENGTH = 128;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050029
Ratan Guptabd303b12017-08-18 17:10:07 +053030namespace mac_address
31{
32
Ratan Guptabd303b12017-08-18 17:10:07 +053033/** @brief gets the MAC address from the Inventory.
34 * @param[in] bus - DBUS Bus Object.
Alvin Wang38a63c32019-08-29 22:56:46 +080035 * @param[in] intfName - Interface name
Ratan Guptabd303b12017-08-18 17:10:07 +053036 */
Patrick Williamsc38b0712022-07-22 19:26:54 -050037ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName);
William A. Kennington III1137a972019-04-20 20:49:58 -070038
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 III1c776022022-01-05 14:12:16 -080044ether_addr fromString(const char* str);
45inline ether_addr fromString(const std::string& str)
46{
47 return fromString(str.c_str());
48}
Ratan Guptabd303b12017-08-18 17:10:07 +053049
William A. Kennington IIId27410f2019-01-30 17:15:43 -080050/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070051 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080052 * @returns A valid mac address string
53 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070054std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080055
William A. Kennington III1137a972019-04-20 20:49:58 -070056/** @brief Determines if the mac address is empty
57 * @param[in] mac - The mac address
58 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053059 */
William A. Kennington III1137a972019-04-20 20:49:58 -070060bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053061
William A. Kennington III1137a972019-04-20 20:49:58 -070062/** @brief Determines if the mac address is a multicast address
63 * @param[in] mac - The mac address
64 * @return True if multicast bit is set
65 */
66bool isMulticast(const ether_addr& mac);
67
68/** @brief Determines if the mac address is a unicast address
69 * @param[in] mac - The mac address
70 * @return True if not multicast or empty
71 */
72bool isUnicast(const ether_addr& mac);
73
Gunnar Mills57d9c502018-09-14 14:42:34 -050074} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053075
Ratan Gupta497c0c92017-08-22 19:15:59 +053076constexpr auto networkdService = "systemd-networkd.service";
77constexpr auto timeSynchdService = "systemd-timesyncd.service";
78
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080079/* @brief converts a sockaddr for the specified address family into
80 * a type_safe InAddrAny.
81 * @param[in] addressFamily - The address family of the buf
82 * @param[in] buf - The network byte order address
83 */
84InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
85
William A. Kennington III5058f572019-01-30 17:18:14 -080086/* @brief converts the ip bytes into a string representation
87 * @param[in] addr - input ip address to convert.
88 * @returns String representation of the ip.
89 */
90std::string toString(const InAddrAny& addr);
Alexander Filippov983da552021-02-08 15:26:54 +030091std::string toString(const struct in_addr& addr);
92std::string toString(const struct in6_addr& addr);
William A. Kennington III5058f572019-01-30 17:18:14 -080093
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050094/* @brief checks that the given ip address valid or not.
95 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
96 * @param[in] address - IP address.
97 * @returns true if it is valid otherwise false.
98 */
99bool isValidIP(int addressFamily, const std::string& address);
100
101/* @brief checks that the given prefix is valid or not.
102 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
103 * @param[in] prefix - prefix length.
104 * @returns true if it is valid otherwise false.
105 */
106bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530107
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530108/** @brief Get all the interfaces from the system.
109 * @returns list of interface names.
110 */
111InterfaceList getInterfaces();
112
Ratan Guptabc886292017-07-25 18:29:57 +0530113/** @brief Delete the given interface.
114 * @param[in] intf - interface name.
115 */
116void deleteInterface(const std::string& intf);
117
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700118/** @brief Converts the interface name into a u-boot environment
119 * variable that would hold its ethernet address.
120 *
121 * @param[in] intf - interface name
122 * @return The name of th environment key
123 */
124std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
125
Ratan Gupta56187e72017-08-13 09:40:14 +0530126/** @brief read the DHCP value from the configuration file
127 * @param[in] confDir - Network configuration directory.
128 * @param[in] intf - Interface name.
129 */
Johnathan Mantey817012a2020-01-30 15:07:39 -0800130EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir,
131 const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530132
Ratan Guptabd303b12017-08-18 17:10:07 +0530133namespace internal
134{
135
136/* @brief runs the given command in child process.
137 * @param[in] path - path of the binary file which needs to be execeuted.
138 * @param[in] args - arguments of the command.
139 */
140void executeCommandinChildProcess(const char* path, char** args);
141
Lei YU307554e2021-03-18 14:56:50 +0800142/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700143std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800144
145/** @brief Parse the comma separated interface names */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700146std::set<std::string_view> parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800147
148/** @brief Get the ignored interfaces */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700149const std::set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800150
Ratan Guptabd303b12017-08-18 17:10:07 +0530151} // namespace internal
152
153/* @brief runs the given command in child process.
154 * @param[in] path -path of the binary file which needs to be execeuted.
155 * @param[in] tArgs - arguments of the command.
156 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530158void execute(const char* path, ArgTypes&&... tArgs)
159{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700160 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530161
Gunnar Mills57d9c502018-09-14 14:42:34 -0500162 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530163
164 internal::executeCommandinChildProcess(path, args);
165}
166
Gunnar Mills57d9c502018-09-14 14:42:34 -0500167} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530168
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169} // namespace phosphor