blob: c463ce078d62a16bbacc17b5d49e73ac2c72755e [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 -050029constexpr auto IPV4_PREFIX = "169.254";
30constexpr auto IPV6_PREFIX = "fe80";
31
Ratan Guptabd303b12017-08-18 17:10:07 +053032namespace mac_address
33{
34
Ratan Guptabd303b12017-08-18 17:10:07 +053035/** @brief gets the MAC address from the Inventory.
36 * @param[in] bus - DBUS Bus Object.
Alvin Wang38a63c32019-08-29 22:56:46 +080037 * @param[in] intfName - Interface name
Ratan Guptabd303b12017-08-18 17:10:07 +053038 */
Patrick Williamsc38b0712022-07-22 19:26:54 -050039ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName);
William A. Kennington III1137a972019-04-20 20:49:58 -070040
41/** @brief Converts the given mac address into byte form
42 * @param[in] str - The mac address in human readable form
43 * @returns A mac address in network byte order
44 * @throws std::runtime_error for bad mac
45 */
William A. Kennington III1c776022022-01-05 14:12:16 -080046ether_addr fromString(const char* str);
47inline ether_addr fromString(const std::string& str)
48{
49 return fromString(str.c_str());
50}
Ratan Guptabd303b12017-08-18 17:10:07 +053051
William A. Kennington IIId27410f2019-01-30 17:15:43 -080052/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070053 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080054 * @returns A valid mac address string
55 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070056std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080057
William A. Kennington III1137a972019-04-20 20:49:58 -070058/** @brief Determines if the mac address is empty
59 * @param[in] mac - The mac address
60 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053061 */
William A. Kennington III1137a972019-04-20 20:49:58 -070062bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053063
William A. Kennington III1137a972019-04-20 20:49:58 -070064/** @brief Determines if the mac address is a multicast address
65 * @param[in] mac - The mac address
66 * @return True if multicast bit is set
67 */
68bool isMulticast(const ether_addr& mac);
69
70/** @brief Determines if the mac address is a unicast address
71 * @param[in] mac - The mac address
72 * @return True if not multicast or empty
73 */
74bool isUnicast(const ether_addr& mac);
75
Gunnar Mills57d9c502018-09-14 14:42:34 -050076} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053077
Ratan Gupta497c0c92017-08-22 19:15:59 +053078constexpr auto networkdService = "systemd-networkd.service";
79constexpr auto timeSynchdService = "systemd-timesyncd.service";
80
Ratan Gupta8804feb2017-05-25 10:49:57 +053081/* @brief converts the given subnet into prefix notation.
82 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
83 * @param[in] mask - Subnet Mask.
84 * @returns prefix.
85 */
86uint8_t toCidr(int addressFamily, const std::string& mask);
87
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080088/* @brief converts a sockaddr for the specified address family into
89 * a type_safe InAddrAny.
90 * @param[in] addressFamily - The address family of the buf
91 * @param[in] buf - The network byte order address
92 */
93InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
94
William A. Kennington III5058f572019-01-30 17:18:14 -080095/* @brief converts the ip bytes into a string representation
96 * @param[in] addr - input ip address to convert.
97 * @returns String representation of the ip.
98 */
99std::string toString(const InAddrAny& addr);
Alexander Filippov983da552021-02-08 15:26:54 +0300100std::string toString(const struct in_addr& addr);
101std::string toString(const struct in6_addr& addr);
William A. Kennington III5058f572019-01-30 17:18:14 -0800102
Ratan Gupta8804feb2017-05-25 10:49:57 +0530103/* @brief converts the prefix into subnetmask.
104 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
105 * @param[in] prefix - prefix length.
106 * @returns subnet mask.
107 */
108std::string toMask(int addressFamily, uint8_t prefix);
109
110/* @brief checks that the given ip address is link local or not.
111 * @param[in] address - IP address.
112 * @returns true if it is linklocal otherwise false.
113 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500114bool isLinkLocalIP(const std::string& address);
115
116/* @brief checks that the given ip address valid or not.
117 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
118 * @param[in] address - IP address.
119 * @returns true if it is valid otherwise false.
120 */
121bool isValidIP(int addressFamily, const std::string& address);
122
123/* @brief checks that the given prefix is valid or not.
124 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
125 * @param[in] prefix - prefix length.
126 * @returns true if it is valid otherwise false.
127 */
128bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530129
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530130/** @brief Gets the map of interface and the associated
131 * address.
132 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530133 */
134IntfAddrMap getInterfaceAddrs();
135
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530136/** @brief Get all the interfaces from the system.
137 * @returns list of interface names.
138 */
139InterfaceList getInterfaces();
140
Ratan Guptabc886292017-07-25 18:29:57 +0530141/** @brief Delete the given interface.
142 * @param[in] intf - interface name.
143 */
144void deleteInterface(const std::string& intf);
145
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700146/** @brief Converts the interface name into a u-boot environment
147 * variable that would hold its ethernet address.
148 *
149 * @param[in] intf - interface name
150 * @return The name of th environment key
151 */
152std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
153
Ratan Gupta56187e72017-08-13 09:40:14 +0530154/** @brief read the DHCP value from the configuration file
155 * @param[in] confDir - Network configuration directory.
156 * @param[in] intf - Interface name.
157 */
Johnathan Mantey817012a2020-01-30 15:07:39 -0800158EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir,
159 const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530160
Ratan Guptabd303b12017-08-18 17:10:07 +0530161namespace internal
162{
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] args - arguments of the command.
167 */
168void executeCommandinChildProcess(const char* path, char** args);
169
Lei YU307554e2021-03-18 14:56:50 +0800170/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700171std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800172
173/** @brief Parse the comma separated interface names */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700174std::set<std::string_view> parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800175
176/** @brief Get the ignored interfaces */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700177const std::set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800178
Ratan Guptabd303b12017-08-18 17:10:07 +0530179} // namespace internal
180
181/* @brief runs the given command in child process.
182 * @param[in] path -path of the binary file which needs to be execeuted.
183 * @param[in] tArgs - arguments of the command.
184 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500185template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530186void execute(const char* path, ArgTypes&&... tArgs)
187{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700188 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530189
Gunnar Mills57d9c502018-09-14 14:42:34 -0500190 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530191
192 internal::executeCommandinChildProcess(path, args);
193}
194
Gunnar Mills57d9c502018-09-14 14:42:34 -0500195} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530196
197class Descriptor
198{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500199 private:
200 /** default value */
201 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530202
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203 public:
204 Descriptor() = default;
205 Descriptor(const Descriptor&) = delete;
206 Descriptor& operator=(const Descriptor&) = delete;
207 Descriptor(Descriptor&&) = delete;
208 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 explicit Descriptor(int fd) : fd(fd)
211 {
212 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530213
Gunnar Mills57d9c502018-09-14 14:42:34 -0500214 /* @brief sets the internal file descriptor with the given descriptor
215 * and closes the old descriptor.
216 * @param[in] descriptor - File/Socket descriptor.
217 */
218 void set(int descriptor)
219 {
220 // same descriptor given
221 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530222 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500223 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530224 }
225
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 // close the old descriptor
227 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530228 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530230 }
231
Gunnar Mills57d9c502018-09-14 14:42:34 -0500232 fd = descriptor;
233 }
234
235 ~Descriptor()
236 {
237 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530238 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500239 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530240 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500241 }
242
243 int operator()() const
244 {
245 return fd;
246 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530247};
248
Gunnar Mills57d9c502018-09-14 14:42:34 -0500249} // namespace phosphor