blob: 3a5e0bce15660bea29b750cdeb42e3104f7af377 [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;
28constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
29constexpr 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 */
47ether_addr fromString(const char* str);
48inline ether_addr fromString(const std::string& str)
49{
50 return fromString(str.c_str());
51}
Ratan Guptabd303b12017-08-18 17:10:07 +053052
William A. Kennington IIId27410f2019-01-30 17:15:43 -080053/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070054 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080055 * @returns A valid mac address string
56 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070057std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080058
William A. Kennington III1137a972019-04-20 20:49:58 -070059/** @brief Determines if the mac address is empty
60 * @param[in] mac - The mac address
61 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053062 */
William A. Kennington III1137a972019-04-20 20:49:58 -070063bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053064
William A. Kennington III1137a972019-04-20 20:49:58 -070065/** @brief Determines if the mac address is a multicast address
66 * @param[in] mac - The mac address
67 * @return True if multicast bit is set
68 */
69bool isMulticast(const ether_addr& mac);
70
71/** @brief Determines if the mac address is a unicast address
72 * @param[in] mac - The mac address
73 * @return True if not multicast or empty
74 */
75bool isUnicast(const ether_addr& mac);
76
77/** @brief Determines if the mac address is locally managed
78 * @param[in] mac - The mac address
79 * @return True if local admin bit is set
80 */
81bool isLocalAdmin(const ether_addr& mac);
82
Gunnar Mills57d9c502018-09-14 14:42:34 -050083} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053084
Ratan Gupta497c0c92017-08-22 19:15:59 +053085constexpr auto networkdService = "systemd-networkd.service";
86constexpr auto timeSynchdService = "systemd-timesyncd.service";
87
Ratan Gupta8804feb2017-05-25 10:49:57 +053088/* @brief converts the given subnet into prefix notation.
89 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
90 * @param[in] mask - Subnet Mask.
91 * @returns prefix.
92 */
93uint8_t toCidr(int addressFamily, const std::string& mask);
94
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080095/* @brief converts a sockaddr for the specified address family into
96 * a type_safe InAddrAny.
97 * @param[in] addressFamily - The address family of the buf
98 * @param[in] buf - The network byte order address
99 */
100InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
101
William A. Kennington III5058f572019-01-30 17:18:14 -0800102/* @brief converts the ip bytes into a string representation
103 * @param[in] addr - input ip address to convert.
104 * @returns String representation of the ip.
105 */
106std::string toString(const InAddrAny& addr);
107
Ratan Gupta8804feb2017-05-25 10:49:57 +0530108/* @brief converts the prefix into subnetmask.
109 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
110 * @param[in] prefix - prefix length.
111 * @returns subnet mask.
112 */
113std::string toMask(int addressFamily, uint8_t prefix);
114
115/* @brief checks that the given ip address is link local or not.
116 * @param[in] address - IP address.
117 * @returns true if it is linklocal otherwise false.
118 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500119bool isLinkLocalIP(const std::string& address);
120
121/* @brief checks that the given ip address valid or not.
122 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
123 * @param[in] address - IP address.
124 * @returns true if it is valid otherwise false.
125 */
126bool isValidIP(int addressFamily, const std::string& address);
127
128/* @brief checks that the given prefix is valid or not.
129 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
130 * @param[in] prefix - prefix length.
131 * @returns true if it is valid otherwise false.
132 */
133bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530134
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530135/** @brief Gets the map of interface and the associated
136 * address.
137 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530138 */
139IntfAddrMap getInterfaceAddrs();
140
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530141/** @brief Get all the interfaces from the system.
142 * @returns list of interface names.
143 */
144InterfaceList getInterfaces();
145
Ratan Guptabc886292017-07-25 18:29:57 +0530146/** @brief Delete the given interface.
147 * @param[in] intf - interface name.
148 */
149void deleteInterface(const std::string& intf);
150
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700151/** @brief Converts the interface name into a u-boot environment
152 * variable that would hold its ethernet address.
153 *
154 * @param[in] intf - interface name
155 * @return The name of th environment key
156 */
157std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
158
Ratan Gupta56187e72017-08-13 09:40:14 +0530159/** @brief read the DHCP value from the configuration file
160 * @param[in] confDir - Network configuration directory.
161 * @param[in] intf - Interface name.
162 */
Johnathan Mantey817012a2020-01-30 15:07:39 -0800163EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir,
164 const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530165
Ratan Guptabd303b12017-08-18 17:10:07 +0530166namespace internal
167{
168
169/* @brief runs the given command in child process.
170 * @param[in] path - path of the binary file which needs to be execeuted.
171 * @param[in] args - arguments of the command.
172 */
173void executeCommandinChildProcess(const char* path, char** args);
174
175} // namespace internal
176
177/* @brief runs the given command in child process.
178 * @param[in] path -path of the binary file which needs to be execeuted.
179 * @param[in] tArgs - arguments of the command.
180 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500181template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530182void execute(const char* path, ArgTypes&&... tArgs)
183{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700184 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530185
Gunnar Mills57d9c502018-09-14 14:42:34 -0500186 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530187
188 internal::executeCommandinChildProcess(path, args);
189}
190
Gunnar Mills57d9c502018-09-14 14:42:34 -0500191} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530192
193class Descriptor
194{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500195 private:
196 /** default value */
197 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530198
Gunnar Mills57d9c502018-09-14 14:42:34 -0500199 public:
200 Descriptor() = default;
201 Descriptor(const Descriptor&) = delete;
202 Descriptor& operator=(const Descriptor&) = delete;
203 Descriptor(Descriptor&&) = delete;
204 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206 explicit Descriptor(int fd) : fd(fd)
207 {
208 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 /* @brief sets the internal file descriptor with the given descriptor
211 * and closes the old descriptor.
212 * @param[in] descriptor - File/Socket descriptor.
213 */
214 void set(int descriptor)
215 {
216 // same descriptor given
217 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530218 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500219 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530220 }
221
Gunnar Mills57d9c502018-09-14 14:42:34 -0500222 // close the old descriptor
223 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530224 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500225 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530226 }
227
Gunnar Mills57d9c502018-09-14 14:42:34 -0500228 fd = descriptor;
229 }
230
231 ~Descriptor()
232 {
233 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530234 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500235 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530236 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500237 }
238
239 int operator()() const
240 {
241 return fd;
242 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530243};
244
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245} // namespace phosphor