1 #pragma once
2 
3 #include "types.hpp"
4 #include "util.hpp"
5 #include "xyz/openbmc_project/Network/IP/Create/server.hpp"
6 #include "xyz/openbmc_project/Network/Neighbor/CreateStatic/server.hpp"
7 
8 #include <filesystem>
9 #include <sdbusplus/bus.hpp>
10 #include <sdbusplus/server/object.hpp>
11 #include <string>
12 #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
13 #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
14 #include <xyz/openbmc_project/Network/MACAddress/server.hpp>
15 
16 #ifndef SDBUSPP_NEW_CAMELCASE
17 #define dhcpEnabled dHCPEnabled
18 #define ip iP
19 #define ipAddress iPAddress
20 #define ipv6AcceptRA iPv6AcceptRA
21 #define macAddress mACAddress
22 #define nicEnabled nICEnabled
23 #define ntpServers nTPServers
24 #endif
25 
26 namespace phosphor
27 {
28 namespace network
29 {
30 
31 using Ifaces = sdbusplus::server::object::object<
32     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
33     sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
34     sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
35     sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
36     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
37 
38 using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
39 
40 using EthernetInterfaceIntf =
41     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
42 using MacAddressIntf =
43     sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
44 
45 using ServerList = std::vector<std::string>;
46 using ObjectPath = sdbusplus::message::object_path;
47 
48 namespace fs = std::filesystem;
49 
50 class Manager; // forward declaration of network manager.
51 
52 class TestEthernetInterface;
53 
54 class VlanInterface;
55 
56 class IPAddress;
57 
58 class Neighbor;
59 
60 using LinkSpeed = uint16_t;
61 using DuplexMode = uint8_t;
62 using Autoneg = uint8_t;
63 using LinkUp = bool;
64 using NICEnabled = bool;
65 using VlanId = uint32_t;
66 using InterfaceName = std::string;
67 using InterfaceInfo =
68     std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp, NICEnabled>;
69 using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
70 using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>;
71 using VlanInterfaceMap =
72     std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
73 
74 /** @class EthernetInterface
75  *  @brief OpenBMC Ethernet Interface implementation.
76  *  @details A concrete implementation for the
77  *  xyz.openbmc_project.Network.EthernetInterface DBus API.
78  */
79 class EthernetInterface : public Ifaces
80 {
81   public:
82     EthernetInterface() = delete;
83     EthernetInterface(const EthernetInterface&) = delete;
84     EthernetInterface& operator=(const EthernetInterface&) = delete;
85     EthernetInterface(EthernetInterface&&) = delete;
86     EthernetInterface& operator=(EthernetInterface&&) = delete;
87     virtual ~EthernetInterface() = default;
88 
89     /** @brief Constructor to put object onto bus at a dbus path.
90      *  @param[in] bus - Bus to attach to.
91      *  @param[in] objPath - Path to attach at.
92      *  @param[in] dhcpEnabled - is dhcp enabled(true/false).
93      *  @param[in] parent - parent object.
94      *  @param[in] emitSignal - true if the object added signal needs to be
95      *                          send.
96      */
97     EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath,
98                       DHCPConf dhcpEnabled, Manager& parent,
99                       bool emitSignal = true);
100 
101     /** @brief Function used to load the nameservers.
102      */
103     virtual void loadNameServers();
104 
105     /** @brief Function to create ipAddress dbus object.
106      *  @param[in] addressType - Type of ip address.
107      *  @param[in] ipAddress- IP address.
108      *  @param[in] prefixLength - Length of prefix.
109      *  @param[in] gateway - Gateway ip address.
110      */
111 
112     ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
113                   uint8_t prefixLength, std::string gateway) override;
114 
115     /** @brief Function to create static neighbor dbus object.
116      *  @param[in] ipAddress - IP address.
117      *  @param[in] macAddress - Low level MAC address.
118      */
119     ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
120 
121     /* @brief delete the dbus object of the given ipAddress.
122      * @param[in] ipAddress - IP address.
123      */
124     void deleteObject(const std::string& ipAddress);
125 
126     /* @brief delete the dbus object of the given ipAddress.
127      * @param[in] ipAddress - IP address.
128      */
129     void deleteStaticNeighborObject(const std::string& ipAddress);
130 
131     /* @brief delete the vlan dbus object of the given interface.
132      *        Also deletes the device file and the network file.
133      * @param[in] interface - VLAN Interface.
134      */
135     void deleteVLANObject(const std::string& interface);
136 
137     /* @brief creates the dbus object(IPaddres) given in the address list.
138      * @param[in] addrs - address list for which dbus objects needs
139      *                    to create.
140      */
141     void createIPAddressObjects();
142 
143     /* @brief creates the dbus object(Neighbor) given in the neighbor list.
144      */
145     void createStaticNeighborObjects();
146 
147     /* @brief Gets the index of the interface on the system
148      */
149     unsigned ifIndex() const;
150 
151     /* @brief Gets all the ip addresses.
152      * @returns the list of ipAddress.
153      */
154     const AddressMap& getAddresses() const
155     {
156         return addrs;
157     }
158 
159     /* @brief Gets all the static neighbor entries.
160      * @returns Static neighbor map.
161      */
162     const NeighborMap& getStaticNeighbors() const
163     {
164         return staticNeighbors;
165     }
166 
167     /** Set value of DHCPEnabled */
168     DHCPConf dhcpEnabled(DHCPConf value) override;
169 
170     /** @brief Selectively disables DHCP
171      *  @param[in] protocol - The IPv4 or IPv6 protocol to return to static
172      *                        addressing mode
173      */
174     void disableDHCP(IP::Protocol protocol);
175 
176     /** Retrieve Link State */
177     bool linkUp() const override;
178 
179     /** Retrieve NIC State */
180     bool nicEnabled() const override;
181 
182     /** Set value of NICEnabled */
183     bool nicEnabled(bool value) override;
184 
185     /** @brief sets the MAC address.
186      *  @param[in] value - MAC address which needs to be set on the system.
187      *  @returns macAddress of the interface or throws an error.
188      */
189     std::string macAddress(std::string value) override;
190 
191     /** @brief get the IPv6AcceptRA flag from the network configuration file
192      *
193      */
194     bool getIPv6AcceptRAFromConf();
195 
196     /** @brief check conf file for Router Advertisements
197      *
198      */
199     bool ipv6AcceptRA(bool value) override;
200 
201     /** @brief sets the NTP servers.
202      *  @param[in] value - vector of NTP servers.
203      */
204     ServerList ntpServers(ServerList value) override;
205 
206     /** @brief sets the DNS/nameservers.
207      *  @param[in] value - vector of DNS servers.
208      */
209     ServerList nameservers(ServerList value) override;
210 
211     /** @brief sets the Static DNS/nameservers.
212      *  @param[in] value - vector of DNS servers.
213      */
214 
215     ServerList staticNameServers(ServerList value) override;
216 
217     /** @brief create Vlan interface.
218      *  @param[in] id- VLAN identifier.
219      */
220     ObjectPath createVLAN(VlanId id);
221 
222     /** @brief load the vlan info from the system
223      *         and creates the ip address dbus objects.
224      *  @param[in] vlanID- VLAN identifier.
225      */
226     void loadVLAN(VlanId vlanID);
227 
228     /** @brief write the network conf file with the in-memory objects.
229      */
230     void writeConfigurationFile();
231 
232     /** @brief delete all dbus objects.
233      */
234     void deleteAll();
235 
236     /** @brief set the default v4 gateway of the interface.
237      *  @param[in] gateway - default v4 gateway of the interface.
238      */
239     std::string defaultGateway(std::string gateway) override;
240 
241     /** @brief set the default v6 gateway of the interface.
242      *  @param[in] gateway - default v6 gateway of the interface.
243      */
244     std::string defaultGateway6(std::string gateway) override;
245 
246     using EthernetInterfaceIntf::dhcpEnabled;
247     using EthernetInterfaceIntf::interfaceName;
248     using EthernetInterfaceIntf::linkUp;
249     using EthernetInterfaceIntf::nicEnabled;
250     using MacAddressIntf::macAddress;
251 
252     using EthernetInterfaceIntf::defaultGateway;
253     using EthernetInterfaceIntf::defaultGateway6;
254     /** @brief Absolute path of the resolv conf file */
255     static constexpr auto resolvConfFile = "/etc/resolv.conf";
256 
257   protected:
258     /** @brief get the info of the ethernet interface.
259      *  @return tuple having the link speed,autonegotiation,duplexmode .
260      */
261     InterfaceInfo getInterfaceInfo() const;
262 
263     /* @brief delete the vlan interface from system.
264      * @param[in] interface - vlan Interface.
265      */
266     void deleteVLANFromSystem(const std::string& interface);
267 
268     /** @brief get the mac address of the interface.
269      *  @param[in] interfaceName - Network interface name.
270      *  @return macaddress on success
271      */
272 
273     std::string getMACAddress(const std::string& interfaceName) const;
274 
275     /** @brief construct the ip address dbus object path.
276      *  @param[in] addressType - Type of ip address.
277      *  @param[in] ipAddress - IP address.
278      *  @param[in] prefixLength - Length of prefix.
279      *  @param[in] gateway - Gateway address.
280 
281      *  @return path of the address object.
282      */
283 
284     std::string generateObjectPath(IP::Protocol addressType,
285                                    const std::string& ipAddress,
286                                    uint8_t prefixLength,
287                                    const std::string& gateway) const;
288 
289     std::string
290         generateStaticNeighborObjectPath(const std::string& ipAddress,
291                                          const std::string& macAddress) const;
292 
293     /** @brief generates the id by doing hash of ipAddress,
294      *         prefixlength and the gateway.
295      *  @param[in] ipAddress - IP address.
296      *  @param[in] prefixLength - Length of prefix.
297      *  @param[in] gateway - Gateway address.
298      *  @return hash string.
299      */
300 
301     static std::string generateId(const std::string& ipAddress,
302                                   uint8_t prefixLength,
303                                   const std::string& gateway);
304 
305     /** @brief generates the id by doing hash of ipAddress
306      *         and the mac address.
307      *  @param[in] ipAddress  - IP address.
308      *  @param[in] macAddress - Gateway address.
309      *  @return hash string.
310      */
311     static std::string generateNeighborId(const std::string& ipAddress,
312                                           const std::string& macAddress);
313 
314     /** @brief write the dhcp section **/
315     void writeDHCPSection(std::fstream& stream);
316 
317     /** @brief get the NTP server list from the network conf
318      *
319      */
320     ServerList getNTPServersFromConf();
321 
322     /** @brief get the name server details from the network conf
323      *
324      */
325     virtual ServerList getNameServerFromResolvd();
326     ServerList getstaticNameServerFromConf();
327 
328     /** @brief Persistent sdbusplus DBus bus connection. */
329     sdbusplus::bus::bus& bus;
330 
331     /** @brief Network Manager object. */
332     Manager& manager;
333 
334     /** @brief Persistent map of IPAddress dbus objects and their names */
335     AddressMap addrs;
336 
337     /** @brief Persistent map of Neighbor dbus objects and their names */
338     NeighborMap staticNeighbors;
339 
340     /** @brief Persistent map of VLAN interface dbus objects and their names */
341     VlanInterfaceMap vlanInterfaces;
342 
343     /** @brief Dbus object path */
344     std::string objPath;
345 
346     friend class TestEthernetInterface;
347 
348   private:
349     /** @brief Determines if DHCP is active for the IP::Protocol supplied.
350      *  @param[in] protocol - Either IPv4 or IPv6
351      *  @param[in] ignoreProtocol - Allows IPv4 and IPv6 to be checked using a
352      *                              single call.
353      *  @returns true/false value if DHCP is active for the input protocol
354      */
355     bool dhcpIsEnabled(IP::Protocol protocol, bool ignoreProtocol = false);
356 
357     /** @brief Determines if DHCP will be active following next reconfig
358      *  @param[in] protocol - Either IPv4 or IPv6
359      *  @param[in] nextDHCPState - The new DHCP mode to take affect
360      *  @returns true/false value if DHCP is active for the input protocol
361      */
362     bool dhcpToBeEnabled(IP::Protocol family, const std::string& nextDHCPState);
363 
364     /** @brief Determines if the address is manually assigned
365      *  @param[in] origin - The origin entry of the IP::Address
366      *  @returns true/false value if the address is static
367      */
368     bool originIsManuallyAssigned(IP::AddressOrigin origin);
369 };
370 
371 } // namespace network
372 } // namespace phosphor
373