blob: b1d27c4b0a9f8552c1331c0d2fe9d91d708ea0ed [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 */
Alvin Wang38a63c32019-08-29 22:56:46 +080039ether_addr getfromInventory(sdbusplus::bus::bus& bus,
40 const std::string& intfName);
William A. Kennington III1137a972019-04-20 20:49:58 -070041
42/** @brief Converts the given mac address into byte form
43 * @param[in] str - The mac address in human readable form
44 * @returns A mac address in network byte order
45 * @throws std::runtime_error for bad mac
46 */
Potin Laida0b1d42021-12-26 20:08:20 +080047ether_addr fromString(std::string_view str);
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
Ratan Gupta8804feb2017-05-25 10:49:57 +053078/* @brief converts the given subnet into prefix notation.
79 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
80 * @param[in] mask - Subnet Mask.
81 * @returns prefix.
82 */
83uint8_t toCidr(int addressFamily, const std::string& mask);
84
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080085/* @brief converts a sockaddr for the specified address family into
86 * a type_safe InAddrAny.
87 * @param[in] addressFamily - The address family of the buf
88 * @param[in] buf - The network byte order address
89 */
90InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
91
William A. Kennington III5058f572019-01-30 17:18:14 -080092/* @brief converts the ip bytes into a string representation
93 * @param[in] addr - input ip address to convert.
94 * @returns String representation of the ip.
95 */
96std::string toString(const InAddrAny& addr);
Alexander Filippov983da552021-02-08 15:26:54 +030097std::string toString(const struct in_addr& addr);
98std::string toString(const struct in6_addr& addr);
William A. Kennington III5058f572019-01-30 17:18:14 -080099
Ratan Gupta8804feb2017-05-25 10:49:57 +0530100/* @brief converts the prefix into subnetmask.
101 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
102 * @param[in] prefix - prefix length.
103 * @returns subnet mask.
104 */
105std::string toMask(int addressFamily, uint8_t prefix);
106
107/* @brief checks that the given ip address is link local or not.
108 * @param[in] address - IP address.
109 * @returns true if it is linklocal otherwise false.
110 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500111bool isLinkLocalIP(const std::string& address);
112
113/* @brief checks that the given ip address valid or not.
114 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
115 * @param[in] address - IP address.
116 * @returns true if it is valid otherwise false.
117 */
118bool isValidIP(int addressFamily, const std::string& address);
119
120/* @brief checks that the given prefix is valid or not.
121 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
122 * @param[in] prefix - prefix length.
123 * @returns true if it is valid otherwise false.
124 */
125bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530126
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530127/** @brief Gets the map of interface and the associated
128 * address.
129 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530130 */
131IntfAddrMap getInterfaceAddrs();
132
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530133/** @brief Get all the interfaces from the system.
134 * @returns list of interface names.
135 */
136InterfaceList getInterfaces();
137
Ratan Guptabc886292017-07-25 18:29:57 +0530138/** @brief Delete the given interface.
139 * @param[in] intf - interface name.
140 */
141void deleteInterface(const std::string& intf);
142
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700143/** @brief Converts the interface name into a u-boot environment
144 * variable that would hold its ethernet address.
145 *
146 * @param[in] intf - interface name
147 * @return The name of th environment key
148 */
149std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
150
Ratan Gupta56187e72017-08-13 09:40:14 +0530151/** @brief read the DHCP value from the configuration file
152 * @param[in] confDir - Network configuration directory.
153 * @param[in] intf - Interface name.
154 */
Johnathan Mantey817012a2020-01-30 15:07:39 -0800155EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir,
156 const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530157
Ratan Guptabd303b12017-08-18 17:10:07 +0530158namespace internal
159{
160
161/* @brief runs the given command in child process.
162 * @param[in] path - path of the binary file which needs to be execeuted.
163 * @param[in] args - arguments of the command.
164 */
165void executeCommandinChildProcess(const char* path, char** args);
166
Lei YU307554e2021-03-18 14:56:50 +0800167/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700168std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800169
170/** @brief Parse the comma separated interface names */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700171std::set<std::string_view> parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800172
173/** @brief Get the ignored interfaces */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700174const std::set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800175
Ratan Guptabd303b12017-08-18 17:10:07 +0530176} // namespace internal
177
178/* @brief runs the given command in child process.
179 * @param[in] path -path of the binary file which needs to be execeuted.
180 * @param[in] tArgs - arguments of the command.
181 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530183void execute(const char* path, ArgTypes&&... tArgs)
184{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700185 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530186
Gunnar Mills57d9c502018-09-14 14:42:34 -0500187 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530188
189 internal::executeCommandinChildProcess(path, args);
190}
191
Gunnar Mills57d9c502018-09-14 14:42:34 -0500192} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530193
194class Descriptor
195{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196 private:
197 /** default value */
198 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530199
Gunnar Mills57d9c502018-09-14 14:42:34 -0500200 public:
201 Descriptor() = default;
202 Descriptor(const Descriptor&) = delete;
203 Descriptor& operator=(const Descriptor&) = delete;
204 Descriptor(Descriptor&&) = delete;
205 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530206
Gunnar Mills57d9c502018-09-14 14:42:34 -0500207 explicit Descriptor(int fd) : fd(fd)
208 {
209 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530210
Gunnar Mills57d9c502018-09-14 14:42:34 -0500211 /* @brief sets the internal file descriptor with the given descriptor
212 * and closes the old descriptor.
213 * @param[in] descriptor - File/Socket descriptor.
214 */
215 void set(int descriptor)
216 {
217 // same descriptor given
218 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530219 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500220 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530221 }
222
Gunnar Mills57d9c502018-09-14 14:42:34 -0500223 // close the old descriptor
224 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530225 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530227 }
228
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 fd = descriptor;
230 }
231
232 ~Descriptor()
233 {
234 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530235 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500236 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530237 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500238 }
239
240 int operator()() const
241 {
242 return fd;
243 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530244};
245
Gunnar Mills57d9c502018-09-14 14:42:34 -0500246} // namespace phosphor