blob: 5705edaf48e46ef2431e36b0549ed45659fab6a2 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
William A. Kennington III95530ec2022-08-19 01:44:39 -07002#include "config_parser.hpp"
Ratan Gupta3681a502017-06-17 19:20:04 +05303#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
William A. Kennington III4966e962019-04-08 01:58:10 -07005#include <netinet/ether.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <unistd.h>
7
William A. Kennington III4966e962019-04-08 01:58:10 -07008#include <cstring>
William A. Kennington IIIa520a392022-08-08 12:17:34 -07009#include <filesystem>
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070010#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <sdbusplus/bus.hpp>
William A. Kennington IIId27410f2019-01-30 17:15:43 -080012#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080013#include <string_view>
William A. Kennington III95530ec2022-08-19 01:44:39 -070014#include <unordered_set>
Johnathan Mantey817012a2020-01-30 15:07:39 -080015#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053016
17namespace phosphor
18{
19namespace network
20{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050021
Johnathan Mantey817012a2020-01-30 15:07:39 -080022using EthernetInterfaceIntf =
23 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
24
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050025constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
26constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
John Wang4d94ecd2021-04-07 17:37:03 +080027constexpr auto IPV6_MAX_PREFIX_LENGTH = 128;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050028
Ratan Guptabd303b12017-08-18 17:10:07 +053029namespace mac_address
30{
31
Ratan Guptabd303b12017-08-18 17:10:07 +053032/** @brief gets the MAC address from the Inventory.
33 * @param[in] bus - DBUS Bus Object.
Alvin Wang38a63c32019-08-29 22:56:46 +080034 * @param[in] intfName - Interface name
Ratan Guptabd303b12017-08-18 17:10:07 +053035 */
Patrick Williamsc38b0712022-07-22 19:26:54 -050036ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName);
William A. Kennington III1137a972019-04-20 20:49:58 -070037
38/** @brief Converts the given mac address into byte form
39 * @param[in] str - The mac address in human readable form
40 * @returns A mac address in network byte order
41 * @throws std::runtime_error for bad mac
42 */
William A. Kennington III1c776022022-01-05 14:12:16 -080043ether_addr fromString(const char* str);
44inline ether_addr fromString(const std::string& str)
45{
46 return fromString(str.c_str());
47}
Ratan Guptabd303b12017-08-18 17:10:07 +053048
William A. Kennington IIId27410f2019-01-30 17:15:43 -080049/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070050 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080051 * @returns A valid mac address string
52 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070053std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080054
William A. Kennington III1137a972019-04-20 20:49:58 -070055/** @brief Determines if the mac address is empty
56 * @param[in] mac - The mac address
57 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053058 */
William A. Kennington III1137a972019-04-20 20:49:58 -070059bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053060
William A. Kennington III1137a972019-04-20 20:49:58 -070061/** @brief Determines if the mac address is a multicast address
62 * @param[in] mac - The mac address
63 * @return True if multicast bit is set
64 */
65bool isMulticast(const ether_addr& mac);
66
67/** @brief Determines if the mac address is a unicast address
68 * @param[in] mac - The mac address
69 * @return True if not multicast or empty
70 */
71bool isUnicast(const ether_addr& mac);
72
Gunnar Mills57d9c502018-09-14 14:42:34 -050073} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053074
Ratan Gupta497c0c92017-08-22 19:15:59 +053075constexpr auto networkdService = "systemd-networkd.service";
76constexpr auto timeSynchdService = "systemd-timesyncd.service";
77
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080078/* @brief converts a sockaddr for the specified address family into
79 * a type_safe InAddrAny.
80 * @param[in] addressFamily - The address family of the buf
81 * @param[in] buf - The network byte order address
82 */
83InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
84
William A. Kennington III5058f572019-01-30 17:18:14 -080085/* @brief converts the ip bytes into a string representation
86 * @param[in] addr - input ip address to convert.
87 * @returns String representation of the ip.
88 */
89std::string toString(const InAddrAny& addr);
Alexander Filippov983da552021-02-08 15:26:54 +030090std::string toString(const struct in_addr& addr);
91std::string toString(const struct in6_addr& addr);
William A. Kennington III5058f572019-01-30 17:18:14 -080092
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050093/* @brief checks that the given ip address valid or not.
94 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
95 * @param[in] address - IP address.
96 * @returns true if it is valid otherwise false.
97 */
98bool isValidIP(int addressFamily, const std::string& address);
99
100/* @brief checks that the given prefix is valid or not.
101 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
102 * @param[in] prefix - prefix length.
103 * @returns true if it is valid otherwise false.
104 */
105bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530106
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530107/** @brief Get all the interfaces from the system.
108 * @returns list of interface names.
109 */
110InterfaceList getInterfaces();
111
Ratan Guptabc886292017-07-25 18:29:57 +0530112/** @brief Delete the given interface.
113 * @param[in] intf - interface name.
114 */
115void deleteInterface(const std::string& intf);
116
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700117/** @brief Converts the interface name into a u-boot environment
118 * variable that would hold its ethernet address.
119 *
120 * @param[in] intf - interface name
121 * @return The name of th environment key
122 */
123std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
124
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700125/** @brief read the IPv6AcceptRA value from the configuration file
126 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +0530127 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700128bool getIPv6AcceptRA(const config::Parser& config);
129
130/** @brief read the DHCP value from the configuration file
131 * @param[in] config - The parsed configuration.
132 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700133struct DHCPVal
134{
135 bool v4, v6;
136};
137DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +0530138
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700139/** @brief Read a boolean DHCP property from a conf file
140 * @param[in] config - The parsed configuration.
141 * @param[in] key - The property name.
142 */
143bool getDHCPProp(const config::Parser& config, std::string_view key);
144
Ratan Guptabd303b12017-08-18 17:10:07 +0530145namespace internal
146{
147
148/* @brief runs the given command in child process.
149 * @param[in] path - path of the binary file which needs to be execeuted.
150 * @param[in] args - arguments of the command.
151 */
152void executeCommandinChildProcess(const char* path, char** args);
153
Lei YU307554e2021-03-18 14:56:50 +0800154/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700155std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800156
157/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700158std::unordered_set<std::string_view>
159 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800160
161/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700162const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800163
Ratan Guptabd303b12017-08-18 17:10:07 +0530164} // namespace internal
165
166/* @brief runs the given command in child process.
167 * @param[in] path -path of the binary file which needs to be execeuted.
168 * @param[in] tArgs - arguments of the command.
169 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500170template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530171void execute(const char* path, ArgTypes&&... tArgs)
172{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700173 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530174
Gunnar Mills57d9c502018-09-14 14:42:34 -0500175 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530176
177 internal::executeCommandinChildProcess(path, args);
178}
179
Gunnar Mills57d9c502018-09-14 14:42:34 -0500180} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530181
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182} // namespace phosphor