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