blob: ded3d2773c96f9d28dbd4a2ed2576469cf9b5767 [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 III69f45542022-09-24 23:28:14 -070012#include <stdplus/zstring.hpp>
13#include <stdplus/zstring_view.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>
William A. Kennington III95530ec2022-08-19 01:44:39 -070016#include <unordered_set>
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 III69f45542022-09-24 23:28:14 -070045ether_addr fromString(stdplus::zstring_view str);
Ratan Guptabd303b12017-08-18 17:10:07 +053046
William A. Kennington IIId27410f2019-01-30 17:15:43 -080047/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070048 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080049 * @returns A valid mac address string
50 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070051std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080052
William A. Kennington III1137a972019-04-20 20:49:58 -070053/** @brief Determines if the mac address is empty
54 * @param[in] mac - The mac address
55 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053056 */
William A. Kennington III1137a972019-04-20 20:49:58 -070057bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053058
William A. Kennington III1137a972019-04-20 20:49:58 -070059/** @brief Determines if the mac address is a multicast address
60 * @param[in] mac - The mac address
61 * @return True if multicast bit is set
62 */
63bool isMulticast(const ether_addr& mac);
64
65/** @brief Determines if the mac address is a unicast address
66 * @param[in] mac - The mac address
67 * @return True if not multicast or empty
68 */
69bool isUnicast(const ether_addr& mac);
70
Gunnar Mills57d9c502018-09-14 14:42:34 -050071} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053072
Ratan Gupta497c0c92017-08-22 19:15:59 +053073constexpr auto networkdService = "systemd-networkd.service";
74constexpr auto timeSynchdService = "systemd-timesyncd.service";
75
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080076/* @brief converts a sockaddr for the specified address family into
77 * a type_safe InAddrAny.
78 * @param[in] addressFamily - The address family of the buf
79 * @param[in] buf - The network byte order address
80 */
81InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
82
William A. Kennington III5058f572019-01-30 17:18:14 -080083/* @brief converts the ip bytes into a string representation
84 * @param[in] addr - input ip address to convert.
85 * @returns String representation of the ip.
86 */
87std::string toString(const InAddrAny& addr);
Alexander Filippov983da552021-02-08 15:26:54 +030088std::string toString(const struct in_addr& addr);
89std::string toString(const struct in6_addr& addr);
William A. Kennington III5058f572019-01-30 17:18:14 -080090
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050091/* @brief checks that the given ip address valid or not.
92 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
93 * @param[in] address - IP address.
94 * @returns true if it is valid otherwise false.
95 */
William A. Kennington III69f45542022-09-24 23:28:14 -070096bool isValidIP(int addressFamily, stdplus::const_zstring address);
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050097
98/* @brief checks that the given prefix is valid or not.
99 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
100 * @param[in] prefix - prefix length.
101 * @returns true if it is valid otherwise false.
102 */
103bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530104
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530105/** @brief Get all the interfaces from the system.
106 * @returns list of interface names.
107 */
108InterfaceList getInterfaces();
109
Ratan Guptabc886292017-07-25 18:29:57 +0530110/** @brief Delete the given interface.
111 * @param[in] intf - interface name.
112 */
William A. Kennington III69f45542022-09-24 23:28:14 -0700113void deleteInterface(stdplus::const_zstring intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530114
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700115/** @brief Converts the interface name into a u-boot environment
116 * variable that would hold its ethernet address.
117 *
118 * @param[in] intf - interface name
119 * @return The name of th environment key
120 */
William A. Kennington III69f45542022-09-24 23:28:14 -0700121std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700122
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700123/** @brief read the IPv6AcceptRA value from the configuration file
124 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +0530125 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700126bool getIPv6AcceptRA(const config::Parser& config);
127
128/** @brief read the DHCP value from the configuration file
129 * @param[in] config - The parsed configuration.
130 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700131struct DHCPVal
132{
133 bool v4, v6;
134};
135DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +0530136
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700137/** @brief Read a boolean DHCP property from a conf file
138 * @param[in] config - The parsed configuration.
139 * @param[in] key - The property name.
140 */
141bool getDHCPProp(const config::Parser& config, std::string_view key);
142
Ratan Guptabd303b12017-08-18 17:10:07 +0530143namespace internal
144{
145
146/* @brief runs the given command in child process.
147 * @param[in] path - path of the binary file which needs to be execeuted.
148 * @param[in] args - arguments of the command.
149 */
William A. Kennington III69f45542022-09-24 23:28:14 -0700150void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +0530151
Lei YU307554e2021-03-18 14:56:50 +0800152/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700153std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800154
155/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700156std::unordered_set<std::string_view>
157 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800158
159/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700160const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800161
Ratan Guptabd303b12017-08-18 17:10:07 +0530162} // namespace internal
163
164/* @brief runs the given command in child process.
165 * @param[in] path -path of the binary file which needs to be execeuted.
166 * @param[in] tArgs - arguments of the command.
167 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500168template <typename... ArgTypes>
William A. Kennington III69f45542022-09-24 23:28:14 -0700169void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +0530170{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700171 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530172
Gunnar Mills57d9c502018-09-14 14:42:34 -0500173 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530174
175 internal::executeCommandinChildProcess(path, args);
176}
177
Gunnar Mills57d9c502018-09-14 14:42:34 -0500178} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530179
Gunnar Mills57d9c502018-09-14 14:42:34 -0500180} // namespace phosphor