blob: ddc5454f30a914c757f128fbeb125a7697a7fed7 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
Ratan Gupta3681a502017-06-17 19:20:04 +05302#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07003
William A. Kennington III4966e962019-04-08 01:58:10 -07004#include <netinet/ether.h>
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -07005#include <netinet/in.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>
William A. Kennington IIId27410f2019-01-30 17:15:43 -080013#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080014#include <string_view>
William A. Kennington III95530ec2022-08-19 01:44:39 -070015#include <unordered_set>
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{
William A. Kennington III20efa792022-10-04 17:19:08 -070022namespace config
23{
24class Parser;
25}
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050026
Johnathan Mantey817012a2020-01-30 15:07:39 -080027using EthernetInterfaceIntf =
28 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
29
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 IIIfeb7aab2022-10-03 17:21:44 -070044ether_addr fromString(std::string_view str);
Ratan Guptabd303b12017-08-18 17:10:07 +053045
William A. Kennington IIId27410f2019-01-30 17:15:43 -080046/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070047 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080048 * @returns A valid mac address string
49 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070050std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080051
William A. Kennington III1137a972019-04-20 20:49:58 -070052/** @brief Determines if the mac address is empty
53 * @param[in] mac - The mac address
54 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053055 */
William A. Kennington III1137a972019-04-20 20:49:58 -070056bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053057
William A. Kennington III1137a972019-04-20 20:49:58 -070058/** @brief Determines if the mac address is a multicast address
59 * @param[in] mac - The mac address
60 * @return True if multicast bit is set
61 */
62bool isMulticast(const ether_addr& mac);
63
64/** @brief Determines if the mac address is a unicast address
65 * @param[in] mac - The mac address
66 * @return True if not multicast or empty
67 */
68bool isUnicast(const ether_addr& mac);
69
Gunnar Mills57d9c502018-09-14 14:42:34 -050070} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053071
Ratan Gupta497c0c92017-08-22 19:15:59 +053072constexpr auto networkdService = "systemd-networkd.service";
73constexpr auto timeSynchdService = "systemd-timesyncd.service";
74
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -070075template <int family>
76struct FamilyTraits
77{
78};
79
80template <>
81struct FamilyTraits<AF_INET>
82{
83 using addr = in_addr;
William A. Kennington IIId07c0862022-10-07 14:17:05 -070084 static constexpr std::size_t strlen = INET_ADDRSTRLEN;
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -070085};
86
87template <>
88struct FamilyTraits<AF_INET6>
89{
90 using addr = in6_addr;
William A. Kennington IIId07c0862022-10-07 14:17:05 -070091 static constexpr std::size_t strlen = INET6_ADDRSTRLEN;
92};
93
94template <typename Addr>
95struct AddrToFamily
96{
97};
98
99template <>
100struct AddrToFamily<in_addr>
101{
102 static constexpr int value = AF_INET;
103};
104
105template <>
106struct AddrToFamily<in6_addr>
107{
108 static constexpr int value = AF_INET6;
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700109};
110
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800111/* @brief converts a sockaddr for the specified address family into
112 * a type_safe InAddrAny.
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700113 * @param[in] family - The address family of the buf
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800114 * @param[in] buf - The network byte order address
115 */
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700116template <int family>
117typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf);
118InAddrAny addrFromBuf(int family, std::string_view buf);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800119
William A. Kennington III5058f572019-01-30 17:18:14 -0800120/* @brief converts the ip bytes into a string representation
121 * @param[in] addr - input ip address to convert.
122 * @returns String representation of the ip.
123 */
William A. Kennington IIId07c0862022-10-07 14:17:05 -0700124template <typename Addr>
125std::string toString(const Addr& addr);
William A. Kennington III5058f572019-01-30 17:18:14 -0800126std::string toString(const InAddrAny& addr);
127
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500128/* @brief checks that the given ip address valid or not.
William A. Kennington III9f228302022-10-07 14:02:49 -0700129 * @param[in] family - IP address family(AF_INET/AF_INET6).
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500130 * @param[in] address - IP address.
131 * @returns true if it is valid otherwise false.
132 */
William A. Kennington III9f228302022-10-07 14:02:49 -0700133bool isValidIP(int family, stdplus::const_zstring address) noexcept;
William A. Kennington IIIff12acb2022-10-07 19:06:56 -0700134bool isValidIP(stdplus::const_zstring address) noexcept;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500135
136/* @brief checks that the given prefix is valid or not.
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700137 * @param[in] family - IP address family(AF_INET/AF_INET6).
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500138 * @param[in] prefix - prefix length.
139 * @returns true if it is valid otherwise false.
140 */
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700141template <int family>
142constexpr bool isValidPrefix(uint8_t prefix) noexcept
143{
144 return prefix <= sizeof(typename FamilyTraits<family>::addr) * 8;
145}
146bool isValidPrefix(int family, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530147
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530148/** @brief Get all the interfaces from the system.
149 * @returns list of interface names.
150 */
William A. Kennington III96444792022-10-05 15:16:22 -0700151string_uset getSystemInterfaces();
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530152
Ratan Guptabc886292017-07-25 18:29:57 +0530153/** @brief Delete the given interface.
154 * @param[in] intf - interface name.
155 */
William A. Kennington III69f45542022-09-24 23:28:14 -0700156void deleteInterface(stdplus::const_zstring intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530157
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700158/** @brief Converts the interface name into a u-boot environment
159 * variable that would hold its ethernet address.
160 *
161 * @param[in] intf - interface name
162 * @return The name of th environment key
163 */
William A. Kennington III69f45542022-09-24 23:28:14 -0700164std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700165
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700166/** @brief read the IPv6AcceptRA value from the configuration file
167 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +0530168 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700169bool getIPv6AcceptRA(const config::Parser& config);
170
171/** @brief read the DHCP value from the configuration file
172 * @param[in] config - The parsed configuration.
173 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700174struct DHCPVal
175{
176 bool v4, v6;
177};
178DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +0530179
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700180/** @brief Read a boolean DHCP property from a conf file
181 * @param[in] config - The parsed configuration.
182 * @param[in] key - The property name.
183 */
184bool getDHCPProp(const config::Parser& config, std::string_view key);
185
Ratan Guptabd303b12017-08-18 17:10:07 +0530186namespace internal
187{
188
189/* @brief runs the given command in child process.
190 * @param[in] path - path of the binary file which needs to be execeuted.
191 * @param[in] args - arguments of the command.
192 */
William A. Kennington III69f45542022-09-24 23:28:14 -0700193void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +0530194
Lei YU307554e2021-03-18 14:56:50 +0800195/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700196std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800197
198/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700199std::unordered_set<std::string_view>
200 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800201
202/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700203const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800204
Ratan Guptabd303b12017-08-18 17:10:07 +0530205} // namespace internal
206
207/* @brief runs the given command in child process.
208 * @param[in] path -path of the binary file which needs to be execeuted.
209 * @param[in] tArgs - arguments of the command.
210 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500211template <typename... ArgTypes>
William A. Kennington III69f45542022-09-24 23:28:14 -0700212void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +0530213{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700214 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530215
Gunnar Mills57d9c502018-09-14 14:42:34 -0500216 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530217
218 internal::executeCommandinChildProcess(path, args);
219}
220
Gunnar Mills57d9c502018-09-14 14:42:34 -0500221} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530222
Gunnar Mills57d9c502018-09-14 14:42:34 -0500223} // namespace phosphor