xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 253f11b84347de6bff7c6b624bef270fefae5f5a)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 
18 #include "app.hpp"
19 #include "dbus_singleton.hpp"
20 #include "dbus_utility.hpp"
21 #include "error_messages.hpp"
22 #include "human_sort.hpp"
23 #include "query.hpp"
24 #include "registries/privilege_registry.hpp"
25 #include "utils/ip_utils.hpp"
26 #include "utils/json_utils.hpp"
27 
28 #include <boost/system/error_code.hpp>
29 #include <boost/url/format.hpp>
30 
31 #include <array>
32 #include <cstddef>
33 #include <memory>
34 #include <optional>
35 #include <ranges>
36 #include <regex>
37 #include <string_view>
38 #include <variant>
39 #include <vector>
40 
41 namespace redfish
42 {
43 
44 enum class LinkType
45 {
46     Local,
47     Global
48 };
49 
50 enum class IpVersion
51 {
52     IpV4,
53     IpV6
54 };
55 
56 /**
57  * Structure for keeping IPv4 data required by Redfish
58  */
59 struct IPv4AddressData
60 {
61     std::string id;
62     std::string address;
63     std::string domain;
64     std::string gateway;
65     std::string netmask;
66     std::string origin;
67     LinkType linktype{};
68     bool isActive{};
69 };
70 
71 /**
72  * Structure for keeping IPv6 data required by Redfish
73  */
74 struct IPv6AddressData
75 {
76     std::string id;
77     std::string address;
78     std::string origin;
79     uint8_t prefixLength = 0;
80 };
81 
82 /**
83  * Structure for keeping static route data required by Redfish
84  */
85 struct StaticGatewayData
86 {
87     std::string id;
88     std::string gateway;
89     size_t prefixLength = 0;
90     std::string protocol;
91 };
92 
93 /**
94  * Structure for keeping basic single Ethernet Interface information
95  * available from DBus
96  */
97 struct EthernetInterfaceData
98 {
99     uint32_t speed;
100     size_t mtuSize;
101     bool autoNeg;
102     bool dnsv4Enabled;
103     bool dnsv6Enabled;
104     bool domainv4Enabled;
105     bool domainv6Enabled;
106     bool ntpv4Enabled;
107     bool ntpv6Enabled;
108     bool hostNamev4Enabled;
109     bool hostNamev6Enabled;
110     bool linkUp;
111     bool nicEnabled;
112     bool ipv6AcceptRa;
113     std::string dhcpEnabled;
114     std::string operatingMode;
115     std::string hostName;
116     std::string defaultGateway;
117     std::string ipv6DefaultGateway;
118     std::string ipv6StaticDefaultGateway;
119     std::string macAddress;
120     std::optional<uint32_t> vlanId;
121     std::vector<std::string> nameServers;
122     std::vector<std::string> staticNameServers;
123     std::vector<std::string> domainnames;
124 };
125 
126 struct DHCPParameters
127 {
128     std::optional<bool> dhcpv4Enabled;
129     std::optional<bool> useDnsServers;
130     std::optional<bool> useNtpServers;
131     std::optional<bool> useDomainName;
132     std::optional<std::string> dhcpv6OperatingMode;
133 };
134 
135 // Helper function that changes bits netmask notation (i.e. /24)
136 // into full dot notation
137 inline std::string getNetmask(unsigned int bits)
138 {
139     uint32_t value = 0xffffffff << (32 - bits);
140     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
141                           std::to_string((value >> 16) & 0xff) + "." +
142                           std::to_string((value >> 8) & 0xff) + "." +
143                           std::to_string(value & 0xff);
144     return netmask;
145 }
146 
147 inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
148                                        bool isIPv4)
149 {
150     if (isIPv4)
151     {
152         return (
153             (inputDHCP ==
154              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
155             (inputDHCP ==
156              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
157     }
158     return ((inputDHCP ==
159              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
160             (inputDHCP ==
161              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
162 }
163 
164 inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
165 {
166     if (isIPv4 && isIPv6)
167     {
168         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
169     }
170     if (isIPv4)
171     {
172         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
173     }
174     if (isIPv6)
175     {
176         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
177     }
178     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
179 }
180 
181 inline std::string
182     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
183                                         bool isIPv4)
184 {
185     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
186     {
187         return "Static";
188     }
189     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
190     {
191         if (isIPv4)
192         {
193             return "IPv4LinkLocal";
194         }
195         return "LinkLocal";
196     }
197     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
198     {
199         if (isIPv4)
200         {
201             return "DHCP";
202         }
203         return "DHCPv6";
204     }
205     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
206     {
207         return "SLAAC";
208     }
209     return "";
210 }
211 
212 inline bool extractEthernetInterfaceData(
213     const std::string& ethifaceId,
214     const dbus::utility::ManagedObjectType& dbusData,
215     EthernetInterfaceData& ethData)
216 {
217     bool idFound = false;
218     for (const auto& objpath : dbusData)
219     {
220         for (const auto& ifacePair : objpath.second)
221         {
222             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
223             {
224                 idFound = true;
225                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
226                 {
227                     for (const auto& propertyPair : ifacePair.second)
228                     {
229                         if (propertyPair.first == "MACAddress")
230                         {
231                             const std::string* mac =
232                                 std::get_if<std::string>(&propertyPair.second);
233                             if (mac != nullptr)
234                             {
235                                 ethData.macAddress = *mac;
236                             }
237                         }
238                     }
239                 }
240                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
241                 {
242                     for (const auto& propertyPair : ifacePair.second)
243                     {
244                         if (propertyPair.first == "Id")
245                         {
246                             const uint32_t* id =
247                                 std::get_if<uint32_t>(&propertyPair.second);
248                             if (id != nullptr)
249                             {
250                                 ethData.vlanId = *id;
251                             }
252                         }
253                     }
254                 }
255                 else if (ifacePair.first ==
256                          "xyz.openbmc_project.Network.EthernetInterface")
257                 {
258                     for (const auto& propertyPair : ifacePair.second)
259                     {
260                         if (propertyPair.first == "AutoNeg")
261                         {
262                             const bool* autoNeg =
263                                 std::get_if<bool>(&propertyPair.second);
264                             if (autoNeg != nullptr)
265                             {
266                                 ethData.autoNeg = *autoNeg;
267                             }
268                         }
269                         else if (propertyPair.first == "Speed")
270                         {
271                             const uint32_t* speed =
272                                 std::get_if<uint32_t>(&propertyPair.second);
273                             if (speed != nullptr)
274                             {
275                                 ethData.speed = *speed;
276                             }
277                         }
278                         else if (propertyPair.first == "MTU")
279                         {
280                             const size_t* mtuSize =
281                                 std::get_if<size_t>(&propertyPair.second);
282                             if (mtuSize != nullptr)
283                             {
284                                 ethData.mtuSize = *mtuSize;
285                             }
286                         }
287                         else if (propertyPair.first == "LinkUp")
288                         {
289                             const bool* linkUp =
290                                 std::get_if<bool>(&propertyPair.second);
291                             if (linkUp != nullptr)
292                             {
293                                 ethData.linkUp = *linkUp;
294                             }
295                         }
296                         else if (propertyPair.first == "NICEnabled")
297                         {
298                             const bool* nicEnabled =
299                                 std::get_if<bool>(&propertyPair.second);
300                             if (nicEnabled != nullptr)
301                             {
302                                 ethData.nicEnabled = *nicEnabled;
303                             }
304                         }
305                         else if (propertyPair.first == "IPv6AcceptRA")
306                         {
307                             const bool* ipv6AcceptRa =
308                                 std::get_if<bool>(&propertyPair.second);
309                             if (ipv6AcceptRa != nullptr)
310                             {
311                                 ethData.ipv6AcceptRa = *ipv6AcceptRa;
312                             }
313                         }
314                         else if (propertyPair.first == "Nameservers")
315                         {
316                             const std::vector<std::string>* nameservers =
317                                 std::get_if<std::vector<std::string>>(
318                                     &propertyPair.second);
319                             if (nameservers != nullptr)
320                             {
321                                 ethData.nameServers = *nameservers;
322                             }
323                         }
324                         else if (propertyPair.first == "StaticNameServers")
325                         {
326                             const std::vector<std::string>* staticNameServers =
327                                 std::get_if<std::vector<std::string>>(
328                                     &propertyPair.second);
329                             if (staticNameServers != nullptr)
330                             {
331                                 ethData.staticNameServers = *staticNameServers;
332                             }
333                         }
334                         else if (propertyPair.first == "DHCPEnabled")
335                         {
336                             const std::string* dhcpEnabled =
337                                 std::get_if<std::string>(&propertyPair.second);
338                             if (dhcpEnabled != nullptr)
339                             {
340                                 ethData.dhcpEnabled = *dhcpEnabled;
341                             }
342                         }
343                         else if (propertyPair.first == "DomainName")
344                         {
345                             const std::vector<std::string>* domainNames =
346                                 std::get_if<std::vector<std::string>>(
347                                     &propertyPair.second);
348                             if (domainNames != nullptr)
349                             {
350                                 ethData.domainnames = *domainNames;
351                             }
352                         }
353                         else if (propertyPair.first == "DefaultGateway")
354                         {
355                             const std::string* defaultGateway =
356                                 std::get_if<std::string>(&propertyPair.second);
357                             if (defaultGateway != nullptr)
358                             {
359                                 std::string defaultGatewayStr = *defaultGateway;
360                                 if (defaultGatewayStr.empty())
361                                 {
362                                     ethData.defaultGateway = "0.0.0.0";
363                                 }
364                                 else
365                                 {
366                                     ethData.defaultGateway = defaultGatewayStr;
367                                 }
368                             }
369                         }
370                         else if (propertyPair.first == "DefaultGateway6")
371                         {
372                             const std::string* defaultGateway6 =
373                                 std::get_if<std::string>(&propertyPair.second);
374                             if (defaultGateway6 != nullptr)
375                             {
376                                 std::string defaultGateway6Str =
377                                     *defaultGateway6;
378                                 if (defaultGateway6Str.empty())
379                                 {
380                                     ethData.ipv6DefaultGateway =
381                                         "0:0:0:0:0:0:0:0";
382                                 }
383                                 else
384                                 {
385                                     ethData.ipv6DefaultGateway =
386                                         defaultGateway6Str;
387                                 }
388                             }
389                         }
390                     }
391                 }
392             }
393 
394             sdbusplus::message::object_path path(
395                 "/xyz/openbmc_project/network");
396             sdbusplus::message::object_path dhcp4Path = path / ethifaceId /
397                                                         "dhcp4";
398 
399             if (sdbusplus::message::object_path(objpath.first) == dhcp4Path)
400             {
401                 if (ifacePair.first ==
402                     "xyz.openbmc_project.Network.DHCPConfiguration")
403                 {
404                     for (const auto& propertyPair : ifacePair.second)
405                     {
406                         if (propertyPair.first == "DNSEnabled")
407                         {
408                             const bool* dnsEnabled =
409                                 std::get_if<bool>(&propertyPair.second);
410                             if (dnsEnabled != nullptr)
411                             {
412                                 ethData.dnsv4Enabled = *dnsEnabled;
413                             }
414                         }
415                         else if (propertyPair.first == "DomainEnabled")
416                         {
417                             const bool* domainEnabled =
418                                 std::get_if<bool>(&propertyPair.second);
419                             if (domainEnabled != nullptr)
420                             {
421                                 ethData.domainv4Enabled = *domainEnabled;
422                             }
423                         }
424                         else if (propertyPair.first == "NTPEnabled")
425                         {
426                             const bool* ntpEnabled =
427                                 std::get_if<bool>(&propertyPair.second);
428                             if (ntpEnabled != nullptr)
429                             {
430                                 ethData.ntpv4Enabled = *ntpEnabled;
431                             }
432                         }
433                         else if (propertyPair.first == "HostNameEnabled")
434                         {
435                             const bool* hostNameEnabled =
436                                 std::get_if<bool>(&propertyPair.second);
437                             if (hostNameEnabled != nullptr)
438                             {
439                                 ethData.hostNamev4Enabled = *hostNameEnabled;
440                             }
441                         }
442                     }
443                 }
444             }
445 
446             sdbusplus::message::object_path dhcp6Path = path / ethifaceId /
447                                                         "dhcp6";
448 
449             if (sdbusplus::message::object_path(objpath.first) == dhcp6Path)
450             {
451                 if (ifacePair.first ==
452                     "xyz.openbmc_project.Network.DHCPConfiguration")
453                 {
454                     for (const auto& propertyPair : ifacePair.second)
455                     {
456                         if (propertyPair.first == "DNSEnabled")
457                         {
458                             const bool* dnsEnabled =
459                                 std::get_if<bool>(&propertyPair.second);
460                             if (dnsEnabled != nullptr)
461                             {
462                                 ethData.dnsv6Enabled = *dnsEnabled;
463                             }
464                         }
465                         if (propertyPair.first == "DomainEnabled")
466                         {
467                             const bool* domainEnabled =
468                                 std::get_if<bool>(&propertyPair.second);
469                             if (domainEnabled != nullptr)
470                             {
471                                 ethData.domainv6Enabled = *domainEnabled;
472                             }
473                         }
474                         else if (propertyPair.first == "NTPEnabled")
475                         {
476                             const bool* ntpEnabled =
477                                 std::get_if<bool>(&propertyPair.second);
478                             if (ntpEnabled != nullptr)
479                             {
480                                 ethData.ntpv6Enabled = *ntpEnabled;
481                             }
482                         }
483                         else if (propertyPair.first == "HostNameEnabled")
484                         {
485                             const bool* hostNameEnabled =
486                                 std::get_if<bool>(&propertyPair.second);
487                             if (hostNameEnabled != nullptr)
488                             {
489                                 ethData.hostNamev6Enabled = *hostNameEnabled;
490                             }
491                         }
492                     }
493                 }
494             }
495             // System configuration shows up in the global namespace, so no need
496             // to check eth number
497             if (ifacePair.first ==
498                 "xyz.openbmc_project.Network.SystemConfiguration")
499             {
500                 for (const auto& propertyPair : ifacePair.second)
501                 {
502                     if (propertyPair.first == "HostName")
503                     {
504                         const std::string* hostname =
505                             std::get_if<std::string>(&propertyPair.second);
506                         if (hostname != nullptr)
507                         {
508                             ethData.hostName = *hostname;
509                         }
510                     }
511                 }
512             }
513         }
514     }
515     return idFound;
516 }
517 
518 // Helper function that extracts data for single ethernet ipv6 address
519 inline void extractIPV6Data(const std::string& ethifaceId,
520                             const dbus::utility::ManagedObjectType& dbusData,
521                             std::vector<IPv6AddressData>& ipv6Config)
522 {
523     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
524                                     ethifaceId;
525 
526     // Since there might be several IPv6 configurations aligned with
527     // single ethernet interface, loop over all of them
528     for (const auto& objpath : dbusData)
529     {
530         // Check if proper pattern for object path appears
531         if (objpath.first.str.starts_with(ipPathStart + "/"))
532         {
533             for (const auto& interface : objpath.second)
534             {
535                 if (interface.first == "xyz.openbmc_project.Network.IP")
536                 {
537                     auto type = std::ranges::find_if(interface.second,
538                                                      [](const auto& property) {
539                         return property.first == "Type";
540                     });
541                     if (type == interface.second.end())
542                     {
543                         continue;
544                     }
545 
546                     const std::string* typeStr =
547                         std::get_if<std::string>(&type->second);
548 
549                     if (typeStr == nullptr ||
550                         (*typeStr !=
551                          "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
552                     {
553                         continue;
554                     }
555 
556                     // Instance IPv6AddressData structure, and set as
557                     // appropriate
558                     IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
559                     ipv6Address.id =
560                         objpath.first.str.substr(ipPathStart.size());
561                     for (const auto& property : interface.second)
562                     {
563                         if (property.first == "Address")
564                         {
565                             const std::string* address =
566                                 std::get_if<std::string>(&property.second);
567                             if (address != nullptr)
568                             {
569                                 ipv6Address.address = *address;
570                             }
571                         }
572                         else if (property.first == "Origin")
573                         {
574                             const std::string* origin =
575                                 std::get_if<std::string>(&property.second);
576                             if (origin != nullptr)
577                             {
578                                 ipv6Address.origin =
579                                     translateAddressOriginDbusToRedfish(*origin,
580                                                                         false);
581                             }
582                         }
583                         else if (property.first == "PrefixLength")
584                         {
585                             const uint8_t* prefix =
586                                 std::get_if<uint8_t>(&property.second);
587                             if (prefix != nullptr)
588                             {
589                                 ipv6Address.prefixLength = *prefix;
590                             }
591                         }
592                         else if (property.first == "Type" ||
593                                  property.first == "Gateway")
594                         {
595                             // Type & Gateway is not used
596                         }
597                         else
598                         {
599                             BMCWEB_LOG_ERROR(
600                                 "Got extra property: {} on the {} object",
601                                 property.first, objpath.first.str);
602                         }
603                     }
604                 }
605             }
606         }
607     }
608 }
609 
610 // Helper function that extracts data for single ethernet ipv4 address
611 inline void extractIPData(const std::string& ethifaceId,
612                           const dbus::utility::ManagedObjectType& dbusData,
613                           std::vector<IPv4AddressData>& ipv4Config)
614 {
615     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
616                                     ethifaceId;
617 
618     // Since there might be several IPv4 configurations aligned with
619     // single ethernet interface, loop over all of them
620     for (const auto& objpath : dbusData)
621     {
622         // Check if proper pattern for object path appears
623         if (objpath.first.str.starts_with(ipPathStart + "/"))
624         {
625             for (const auto& interface : objpath.second)
626             {
627                 if (interface.first == "xyz.openbmc_project.Network.IP")
628                 {
629                     auto type = std::ranges::find_if(interface.second,
630                                                      [](const auto& property) {
631                         return property.first == "Type";
632                     });
633                     if (type == interface.second.end())
634                     {
635                         continue;
636                     }
637 
638                     const std::string* typeStr =
639                         std::get_if<std::string>(&type->second);
640 
641                     if (typeStr == nullptr ||
642                         (*typeStr !=
643                          "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
644                     {
645                         continue;
646                     }
647 
648                     // Instance IPv4AddressData structure, and set as
649                     // appropriate
650                     IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
651                     ipv4Address.id =
652                         objpath.first.str.substr(ipPathStart.size());
653                     for (const auto& property : interface.second)
654                     {
655                         if (property.first == "Address")
656                         {
657                             const std::string* address =
658                                 std::get_if<std::string>(&property.second);
659                             if (address != nullptr)
660                             {
661                                 ipv4Address.address = *address;
662                             }
663                         }
664                         else if (property.first == "Origin")
665                         {
666                             const std::string* origin =
667                                 std::get_if<std::string>(&property.second);
668                             if (origin != nullptr)
669                             {
670                                 ipv4Address.origin =
671                                     translateAddressOriginDbusToRedfish(*origin,
672                                                                         true);
673                             }
674                         }
675                         else if (property.first == "PrefixLength")
676                         {
677                             const uint8_t* mask =
678                                 std::get_if<uint8_t>(&property.second);
679                             if (mask != nullptr)
680                             {
681                                 // convert it to the string
682                                 ipv4Address.netmask = getNetmask(*mask);
683                             }
684                         }
685                         else if (property.first == "Type" ||
686                                  property.first == "Gateway")
687                         {
688                             // Type & Gateway is not used
689                         }
690                         else
691                         {
692                             BMCWEB_LOG_ERROR(
693                                 "Got extra property: {} on the {} object",
694                                 property.first, objpath.first.str);
695                         }
696                     }
697                     // Check if given address is local, or global
698                     ipv4Address.linktype =
699                         ipv4Address.address.starts_with("169.254.")
700                             ? LinkType::Local
701                             : LinkType::Global;
702                 }
703             }
704         }
705     }
706 }
707 
708 /**
709  * @brief Modifies the default gateway assigned to the NIC
710  *
711  * @param[in] ifaceId     Id of network interface whose default gateway is to be
712  *                        changed
713  * @param[in] gateway     The new gateway value. Assigning an empty string
714  *                        causes the gateway to be deleted
715  * @param[io] asyncResp   Response object that will be returned to client
716  *
717  * @return None
718  */
719 inline void updateIPv4DefaultGateway(
720     const std::string& ifaceId, const std::string& gateway,
721     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
722 {
723     setDbusProperty(
724         asyncResp, "xyz.openbmc_project.Network",
725         sdbusplus::message::object_path("/xyz/openbmc_project/network") /
726             ifaceId,
727         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
728         "Gateway", gateway);
729 }
730 
731 /**
732  * @brief Deletes given static IP address for the interface
733  *
734  * @param[in] ifaceId     Id of interface whose IP should be deleted
735  * @param[in] ipHash      DBus Hash id of IP that should be deleted
736  * @param[io] asyncResp   Response object that will be returned to client
737  *
738  * @return None
739  */
740 inline void deleteIPAddress(const std::string& ifaceId,
741                             const std::string& ipHash,
742                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
743 {
744     crow::connections::systemBus->async_method_call(
745         [asyncResp](const boost::system::error_code& ec) {
746         if (ec)
747         {
748             messages::internalError(asyncResp->res);
749         }
750     },
751         "xyz.openbmc_project.Network",
752         "/xyz/openbmc_project/network/" + ifaceId + ipHash,
753         "xyz.openbmc_project.Object.Delete", "Delete");
754 }
755 
756 /**
757  * @brief Creates a static IPv4 entry
758  *
759  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
760  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
761  * @param[in] gateway      IPv4 address of this interfaces gateway
762  * @param[in] address      IPv4 address to assign to this interface
763  * @param[io] asyncResp    Response object that will be returned to client
764  *
765  * @return None
766  */
767 inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
768                        const std::string& gateway, const std::string& address,
769                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
770 {
771     auto createIpHandler = [asyncResp, ifaceId,
772                             gateway](const boost::system::error_code& ec) {
773         if (ec)
774         {
775             messages::internalError(asyncResp->res);
776             return;
777         }
778     };
779 
780     crow::connections::systemBus->async_method_call(
781         std::move(createIpHandler), "xyz.openbmc_project.Network",
782         "/xyz/openbmc_project/network/" + ifaceId,
783         "xyz.openbmc_project.Network.IP.Create", "IP",
784         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
785         gateway);
786 }
787 
788 /**
789  * @brief Deletes the IP entry for this interface and creates a replacement
790  * static entry
791  *
792  * @param[in] ifaceId        Id of interface upon which to create the IPv6 entry
793  * @param[in] id             The unique hash entry identifying the DBus entry
794  * @param[in] prefixLength   Prefix syntax for the subnet mask
795  * @param[in] address        Address to assign to this interface
796  * @param[in] numStaticAddrs Count of IPv4 static addresses
797  * @param[io] asyncResp      Response object that will be returned to client
798  *
799  * @return None
800  */
801 
802 inline void deleteAndCreateIPAddress(
803     IpVersion version, const std::string& ifaceId, const std::string& id,
804     uint8_t prefixLength, const std::string& address,
805     const std::string& gateway,
806     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
807 {
808     crow::connections::systemBus->async_method_call(
809         [asyncResp, version, ifaceId, address, prefixLength,
810          gateway](const boost::system::error_code& ec) {
811         if (ec)
812         {
813             messages::internalError(asyncResp->res);
814         }
815         std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
816         protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
817         crow::connections::systemBus->async_method_call(
818             [asyncResp](const boost::system::error_code& ec2) {
819             if (ec2)
820             {
821                 messages::internalError(asyncResp->res);
822             }
823         },
824             "xyz.openbmc_project.Network",
825             "/xyz/openbmc_project/network/" + ifaceId,
826             "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
827             prefixLength, gateway);
828     },
829         "xyz.openbmc_project.Network",
830         "/xyz/openbmc_project/network/" + ifaceId + id,
831         "xyz.openbmc_project.Object.Delete", "Delete");
832 }
833 
834 inline bool extractIPv6DefaultGatewayData(
835     const std::string& ethifaceId,
836     const dbus::utility::ManagedObjectType& dbusData,
837     std::vector<StaticGatewayData>& staticGatewayConfig)
838 {
839     std::string staticGatewayPathStart("/xyz/openbmc_project/network/");
840     staticGatewayPathStart += ethifaceId;
841 
842     for (const auto& objpath : dbusData)
843     {
844         if (!std::string_view(objpath.first.str)
845                  .starts_with(staticGatewayPathStart))
846         {
847             continue;
848         }
849         for (const auto& interface : objpath.second)
850         {
851             if (interface.first != "xyz.openbmc_project.Network.StaticGateway")
852             {
853                 continue;
854             }
855             StaticGatewayData& staticGateway =
856                 staticGatewayConfig.emplace_back();
857             staticGateway.id = objpath.first.filename();
858 
859             bool success = sdbusplus::unpackPropertiesNoThrow(
860                 redfish::dbus_utils::UnpackErrorPrinter(), interface.second,
861                 "Gateway", staticGateway.gateway, "PrefixLength",
862                 staticGateway.prefixLength, "ProtocolType",
863                 staticGateway.protocol);
864             if (!success)
865             {
866                 return false;
867             }
868         }
869     }
870     return true;
871 }
872 
873 /**
874  * @brief Creates IPv6 with given data
875  *
876  * @param[in] ifaceId      Id of interface whose IP should be added
877  * @param[in] prefixLength Prefix length that needs to be added
878  * @param[in] address      IP address that needs to be added
879  * @param[io] asyncResp    Response object that will be returned to client
880  *
881  * @return None
882  */
883 inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
884                        const std::string& address,
885                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
886 {
887     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
888     path /= ifaceId;
889 
890     auto createIpHandler = [asyncResp,
891                             address](const boost::system::error_code& ec) {
892         if (ec)
893         {
894             if (ec == boost::system::errc::io_error)
895             {
896                 messages::propertyValueFormatError(asyncResp->res, address,
897                                                    "Address");
898             }
899             else
900             {
901                 messages::internalError(asyncResp->res);
902             }
903         }
904     };
905     // Passing null for gateway, as per redfish spec IPv6StaticAddresses
906     // object does not have associated gateway property
907     crow::connections::systemBus->async_method_call(
908         std::move(createIpHandler), "xyz.openbmc_project.Network", path,
909         "xyz.openbmc_project.Network.IP.Create", "IP",
910         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
911         "");
912 }
913 
914 /**
915  * @brief Deletes given IPv6 Static Gateway
916  *
917  * @param[in] ifaceId     Id of interface whose IP should be deleted
918  * @param[in] ipHash      DBus Hash id of IP that should be deleted
919  * @param[io] asyncResp   Response object that will be returned to client
920  *
921  * @return None
922  */
923 inline void
924     deleteIPv6Gateway(std::string_view gatewayId,
925                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
926 {
927     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
928     path /= gatewayId;
929     crow::connections::systemBus->async_method_call(
930         [asyncResp](const boost::system::error_code& ec) {
931         if (ec)
932         {
933             messages::internalError(asyncResp->res);
934         }
935     },
936         "xyz.openbmc_project.Network", path,
937         "xyz.openbmc_project.Object.Delete", "Delete");
938 }
939 
940 /**
941  * @brief Creates IPv6 static default gateway with given data
942  *
943  * @param[in] ifaceId      Id of interface whose IP should be added
944  * @param[in] prefixLength Prefix length that needs to be added
945  * @param[in] gateway      Gateway address that needs to be added
946  * @param[io] asyncResp    Response object that will be returned to client
947  *
948  * @return None
949  */
950 inline void createIPv6DefaultGateway(
951     std::string_view ifaceId, size_t prefixLength, std::string_view gateway,
952     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
953 {
954     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
955     path /= ifaceId;
956     auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
957         if (ec)
958         {
959             messages::internalError(asyncResp->res);
960         }
961     };
962     crow::connections::systemBus->async_method_call(
963         std::move(createIpHandler), "xyz.openbmc_project.Network", path,
964         "xyz.openbmc_project.Network.StaticGateway.Create", "StaticGateway",
965         gateway, prefixLength, "xyz.openbmc_project.Network.IP.Protocol.IPv6");
966 }
967 
968 /**
969  * @brief Deletes the IPv6 default gateway entry for this interface and
970  * creates a replacement IPv6 default gateway entry
971  *
972  * @param[in] ifaceId      Id of interface upon which to create the IPv6
973  * entry
974  * @param[in] gateway      IPv6 gateway to assign to this interface
975  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
976  * @param[io] asyncResp    Response object that will be returned to client
977  *
978  * @return None
979  */
980 inline void deleteAndCreateIPv6DefaultGateway(
981     std::string_view ifaceId, std::string_view gatewayId,
982     std::string_view gateway, size_t prefixLength,
983     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
984 {
985     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
986     path /= gatewayId;
987     crow::connections::systemBus->async_method_call(
988         [asyncResp, ifaceId, gateway,
989          prefixLength](const boost::system::error_code& ec) {
990         if (ec)
991         {
992             messages::internalError(asyncResp->res);
993             return;
994         }
995         createIPv6DefaultGateway(ifaceId, prefixLength, gateway, asyncResp);
996     },
997         "xyz.openbmc_project.Network", path,
998         "xyz.openbmc_project.Object.Delete", "Delete");
999 }
1000 
1001 /**
1002  * @brief Sets IPv6 default gateway with given data
1003  *
1004  * @param[in] ifaceId      Id of interface whose gateway should be added
1005  * @param[in] input        Contains address that needs to be added
1006  * @param[in] staticGatewayData  Current static gateways in the system
1007  * @param[io] asyncResp    Response object that will be returned to client
1008  *
1009  * @return None
1010  */
1011 
1012 inline void handleIPv6DefaultGateway(
1013     const std::string& ifaceId,
1014     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1015     const std::vector<StaticGatewayData>& staticGatewayData,
1016     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1017 {
1018     size_t entryIdx = 1;
1019     std::vector<StaticGatewayData>::const_iterator staticGatewayEntry =
1020         staticGatewayData.begin();
1021 
1022     for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1023          input)
1024     {
1025         // find the next gateway entry
1026         while (staticGatewayEntry != staticGatewayData.end())
1027         {
1028             if (staticGatewayEntry->protocol ==
1029                 "xyz.openbmc_project.Network.IP.Protocol.IPv6")
1030             {
1031                 break;
1032             }
1033             staticGatewayEntry++;
1034         }
1035         std::string pathString = "IPv6StaticDefaultGateways/" +
1036                                  std::to_string(entryIdx);
1037         nlohmann::json::object_t* obj =
1038             std::get_if<nlohmann::json::object_t>(&thisJson);
1039         if (obj == nullptr)
1040         {
1041             if (staticGatewayEntry == staticGatewayData.end())
1042             {
1043                 messages::resourceCannotBeDeleted(asyncResp->res);
1044                 return;
1045             }
1046             deleteIPv6Gateway(staticGatewayEntry->id, asyncResp);
1047             return;
1048         }
1049         if (obj->empty())
1050         {
1051             // Do nothing, but make sure the entry exists.
1052             if (staticGatewayEntry == staticGatewayData.end())
1053             {
1054                 messages::propertyValueFormatError(asyncResp->res, *obj,
1055                                                    pathString);
1056                 return;
1057             }
1058         }
1059         std::optional<std::string> address;
1060         std::optional<size_t> prefixLength;
1061 
1062         if (!json_util::readJsonObject(*obj, asyncResp->res, "Address", address,
1063                                        "PrefixLength", prefixLength))
1064         {
1065             return;
1066         }
1067         const std::string* addr = nullptr;
1068         size_t prefix = 0;
1069         if (address)
1070         {
1071             addr = &(*address);
1072         }
1073         else if (staticGatewayEntry != staticGatewayData.end())
1074         {
1075             addr = &(staticGatewayEntry->gateway);
1076         }
1077         else
1078         {
1079             messages::propertyMissing(asyncResp->res, pathString + "/Address");
1080             return;
1081         }
1082         if (prefixLength)
1083         {
1084             prefix = *prefixLength;
1085         }
1086         else if (staticGatewayEntry != staticGatewayData.end())
1087         {
1088             prefix = staticGatewayEntry->prefixLength;
1089         }
1090         else
1091         {
1092             messages::propertyMissing(asyncResp->res,
1093                                       pathString + "/PrefixLength");
1094             return;
1095         }
1096         if (staticGatewayEntry != staticGatewayData.end())
1097         {
1098             deleteAndCreateIPv6DefaultGateway(ifaceId, staticGatewayEntry->id,
1099                                               *addr, prefix, asyncResp);
1100             staticGatewayEntry++;
1101         }
1102         else
1103         {
1104             createIPv6DefaultGateway(ifaceId, prefix, *addr, asyncResp);
1105         }
1106         entryIdx++;
1107     }
1108 }
1109 
1110 /**
1111  * Function that retrieves all properties for given Ethernet Interface
1112  * Object
1113  * from EntityManager Network Manager
1114  * @param ethiface_id a eth interface id to query on DBus
1115  * @param callback a function that shall be called to convert Dbus output
1116  * into JSON
1117  */
1118 template <typename CallbackFunc>
1119 void getEthernetIfaceData(const std::string& ethifaceId,
1120                           CallbackFunc&& callback)
1121 {
1122     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1123     dbus::utility::getManagedObjects(
1124         "xyz.openbmc_project.Network", path,
1125         [ethifaceId{std::string{ethifaceId}},
1126          callback = std::forward<CallbackFunc>(callback)](
1127             const boost::system::error_code& ec,
1128             const dbus::utility::ManagedObjectType& resp) mutable {
1129         EthernetInterfaceData ethData{};
1130         std::vector<IPv4AddressData> ipv4Data;
1131         std::vector<IPv6AddressData> ipv6Data;
1132         std::vector<StaticGatewayData> ipv6GatewayData;
1133 
1134         if (ec)
1135         {
1136             callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1137             return;
1138         }
1139 
1140         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
1141         if (!found)
1142         {
1143             callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1144             return;
1145         }
1146 
1147         extractIPData(ethifaceId, resp, ipv4Data);
1148         // Fix global GW
1149         for (IPv4AddressData& ipv4 : ipv4Data)
1150         {
1151             if (((ipv4.linktype == LinkType::Global) &&
1152                  (ipv4.gateway == "0.0.0.0")) ||
1153                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
1154             {
1155                 ipv4.gateway = ethData.defaultGateway;
1156             }
1157         }
1158 
1159         extractIPV6Data(ethifaceId, resp, ipv6Data);
1160         if (!extractIPv6DefaultGatewayData(ethifaceId, resp, ipv6GatewayData))
1161         {
1162             callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1163         }
1164         // Finally make a callback with useful data
1165         callback(true, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1166     });
1167 }
1168 
1169 /**
1170  * Function that retrieves all Ethernet Interfaces available through Network
1171  * Manager
1172  * @param callback a function that shall be called to convert Dbus output
1173  * into JSON.
1174  */
1175 template <typename CallbackFunc>
1176 void getEthernetIfaceList(CallbackFunc&& callback)
1177 {
1178     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1179     dbus::utility::getManagedObjects(
1180         "xyz.openbmc_project.Network", path,
1181         [callback = std::forward<CallbackFunc>(callback)](
1182             const boost::system::error_code& ec,
1183             const dbus::utility::ManagedObjectType& resp) {
1184         // Callback requires vector<string> to retrieve all available
1185         // ethernet interfaces
1186         std::vector<std::string> ifaceList;
1187         ifaceList.reserve(resp.size());
1188         if (ec)
1189         {
1190             callback(false, ifaceList);
1191             return;
1192         }
1193 
1194         // Iterate over all retrieved ObjectPaths.
1195         for (const auto& objpath : resp)
1196         {
1197             // And all interfaces available for certain ObjectPath.
1198             for (const auto& interface : objpath.second)
1199             {
1200                 // If interface is
1201                 // xyz.openbmc_project.Network.EthernetInterface, this is
1202                 // what we're looking for.
1203                 if (interface.first ==
1204                     "xyz.openbmc_project.Network.EthernetInterface")
1205                 {
1206                     std::string ifaceId = objpath.first.filename();
1207                     if (ifaceId.empty())
1208                     {
1209                         continue;
1210                     }
1211                     // and put it into output vector.
1212                     ifaceList.emplace_back(ifaceId);
1213                 }
1214             }
1215         }
1216 
1217         std::ranges::sort(ifaceList, AlphanumLess<std::string>());
1218 
1219         // Finally make a callback with useful data
1220         callback(true, ifaceList);
1221     });
1222 }
1223 
1224 inline void
1225     handleHostnamePatch(const std::string& hostname,
1226                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1227 {
1228     // SHOULD handle host names of up to 255 characters(RFC 1123)
1229     if (hostname.length() > 255)
1230     {
1231         messages::propertyValueFormatError(asyncResp->res, hostname,
1232                                            "HostName");
1233         return;
1234     }
1235     setDbusProperty(
1236         asyncResp, "xyz.openbmc_project.Network",
1237         sdbusplus::message::object_path("/xyz/openbmc_project/network/config"),
1238         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1239         "HostName", hostname);
1240 }
1241 
1242 inline void
1243     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1244                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1245 {
1246     sdbusplus::message::object_path objPath("/xyz/openbmc_project/network");
1247     objPath /= ifaceId;
1248     setDbusProperty(asyncResp, "xyz.openbmc_project.Network", objPath,
1249                     "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1250                     "MTUSize", mtuSize);
1251 }
1252 
1253 inline void
1254     handleDomainnamePatch(const std::string& ifaceId,
1255                           const std::string& domainname,
1256                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1257 {
1258     std::vector<std::string> vectorDomainname = {domainname};
1259     setDbusProperty(
1260         asyncResp, "xyz.openbmc_project.Network",
1261         sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1262             ifaceId,
1263         "xyz.openbmc_project.Network.EthernetInterface", "DomainName", "FQDN",
1264         vectorDomainname);
1265 }
1266 
1267 inline bool isHostnameValid(const std::string& hostname)
1268 {
1269     // A valid host name can never have the dotted-decimal form (RFC 1123)
1270     if (std::ranges::all_of(hostname, ::isdigit))
1271     {
1272         return false;
1273     }
1274     // Each label(hostname/subdomains) within a valid FQDN
1275     // MUST handle host names of up to 63 characters (RFC 1123)
1276     // labels cannot start or end with hyphens (RFC 952)
1277     // labels can start with numbers (RFC 1123)
1278     const static std::regex pattern(
1279         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1280 
1281     return std::regex_match(hostname, pattern);
1282 }
1283 
1284 inline bool isDomainnameValid(const std::string& domainname)
1285 {
1286     // Can have multiple subdomains
1287     // Top Level Domain's min length is 2 character
1288     const static std::regex pattern(
1289         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1290 
1291     return std::regex_match(domainname, pattern);
1292 }
1293 
1294 inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1295                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1296 {
1297     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1298     if (fqdn.length() > 255)
1299     {
1300         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1301         return;
1302     }
1303 
1304     size_t pos = fqdn.find('.');
1305     if (pos == std::string::npos)
1306     {
1307         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1308         return;
1309     }
1310 
1311     std::string hostname;
1312     std::string domainname;
1313     domainname = (fqdn).substr(pos + 1);
1314     hostname = (fqdn).substr(0, pos);
1315 
1316     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1317     {
1318         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1319         return;
1320     }
1321 
1322     handleHostnamePatch(hostname, asyncResp);
1323     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1324 }
1325 
1326 inline void
1327     handleMACAddressPatch(const std::string& ifaceId,
1328                           const std::string& macAddress,
1329                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1330 {
1331     setDbusProperty(
1332         asyncResp, "xyz.openbmc_project.Network",
1333         sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1334             ifaceId,
1335         "xyz.openbmc_project.Network.MACAddress", "MACAddress", "MACAddress",
1336         macAddress);
1337 }
1338 
1339 inline void setDHCPEnabled(const std::string& ifaceId,
1340                            const std::string& propertyName, const bool v4Value,
1341                            const bool v6Value,
1342                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1343 {
1344     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1345     setDbusProperty(
1346         asyncResp, "xyz.openbmc_project.Network",
1347         sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1348             ifaceId,
1349         "xyz.openbmc_project.Network.EthernetInterface", propertyName, "DHCPv4",
1350         dhcp);
1351 }
1352 
1353 enum class NetworkType
1354 {
1355     dhcp4,
1356     dhcp6
1357 };
1358 
1359 inline void setDHCPConfig(const std::string& propertyName, const bool& value,
1360                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1361                           const std::string& ethifaceId, NetworkType type)
1362 {
1363     BMCWEB_LOG_DEBUG("{} = {}", propertyName, value);
1364     std::string redfishPropertyName;
1365     sdbusplus::message::object_path path("/xyz/openbmc_project/network/");
1366     path /= ethifaceId;
1367 
1368     if (type == NetworkType::dhcp4)
1369     {
1370         path /= "dhcp4";
1371         redfishPropertyName = "DHCPv4";
1372     }
1373     else
1374     {
1375         path /= "dhcp6";
1376         redfishPropertyName = "DHCPv6";
1377     }
1378 
1379     setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1380                     "xyz.openbmc_project.Network.DHCPConfiguration",
1381                     propertyName, redfishPropertyName, value);
1382 }
1383 
1384 inline void handleSLAACAutoConfigPatch(
1385     const std::string& ifaceId, bool ipv6AutoConfigEnabled,
1386     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1387 {
1388     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1389     path /= ifaceId;
1390     setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1391                     "xyz.openbmc_project.Network.EthernetInterface",
1392                     "IPv6AcceptRA",
1393                     "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
1394                     ipv6AutoConfigEnabled);
1395 }
1396 
1397 inline void handleDHCPPatch(const std::string& ifaceId,
1398                             const EthernetInterfaceData& ethData,
1399                             const DHCPParameters& v4dhcpParms,
1400                             const DHCPParameters& v6dhcpParms,
1401                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1402 {
1403     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1404     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1405 
1406     if (ipv4Active)
1407     {
1408         updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1409     }
1410     bool nextv4DHCPState =
1411         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1412 
1413     bool nextv6DHCPState{};
1414     if (v6dhcpParms.dhcpv6OperatingMode)
1415     {
1416         if ((*v6dhcpParms.dhcpv6OperatingMode != "Enabled") &&
1417             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1418         {
1419             messages::propertyValueFormatError(asyncResp->res,
1420                                                *v6dhcpParms.dhcpv6OperatingMode,
1421                                                "OperatingMode");
1422             return;
1423         }
1424         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Enabled");
1425     }
1426     else
1427     {
1428         nextv6DHCPState = ipv6Active;
1429     }
1430 
1431     bool nextDNSv4 = ethData.dnsv4Enabled;
1432     bool nextDNSv6 = ethData.dnsv6Enabled;
1433     if (v4dhcpParms.useDnsServers)
1434     {
1435         nextDNSv4 = *v4dhcpParms.useDnsServers;
1436     }
1437     if (v6dhcpParms.useDnsServers)
1438     {
1439         nextDNSv6 = *v6dhcpParms.useDnsServers;
1440     }
1441 
1442     bool nextNTPv4 = ethData.ntpv4Enabled;
1443     bool nextNTPv6 = ethData.ntpv6Enabled;
1444     if (v4dhcpParms.useNtpServers)
1445     {
1446         nextNTPv4 = *v4dhcpParms.useNtpServers;
1447     }
1448     if (v6dhcpParms.useNtpServers)
1449     {
1450         nextNTPv6 = *v6dhcpParms.useNtpServers;
1451     }
1452 
1453     bool nextUsev4Domain = ethData.domainv4Enabled;
1454     bool nextUsev6Domain = ethData.domainv6Enabled;
1455     if (v4dhcpParms.useDomainName)
1456     {
1457         nextUsev4Domain = *v4dhcpParms.useDomainName;
1458     }
1459     if (v6dhcpParms.useDomainName)
1460     {
1461         nextUsev6Domain = *v6dhcpParms.useDomainName;
1462     }
1463 
1464     BMCWEB_LOG_DEBUG("set DHCPEnabled...");
1465     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1466                    asyncResp);
1467     BMCWEB_LOG_DEBUG("set DNSEnabled...");
1468     setDHCPConfig("DNSEnabled", nextDNSv4, asyncResp, ifaceId,
1469                   NetworkType::dhcp4);
1470     BMCWEB_LOG_DEBUG("set NTPEnabled...");
1471     setDHCPConfig("NTPEnabled", nextNTPv4, asyncResp, ifaceId,
1472                   NetworkType::dhcp4);
1473     BMCWEB_LOG_DEBUG("set DomainEnabled...");
1474     setDHCPConfig("DomainEnabled", nextUsev4Domain, asyncResp, ifaceId,
1475                   NetworkType::dhcp4);
1476     BMCWEB_LOG_DEBUG("set DNSEnabled for dhcp6...");
1477     setDHCPConfig("DNSEnabled", nextDNSv6, asyncResp, ifaceId,
1478                   NetworkType::dhcp6);
1479     BMCWEB_LOG_DEBUG("set NTPEnabled for dhcp6...");
1480     setDHCPConfig("NTPEnabled", nextNTPv6, asyncResp, ifaceId,
1481                   NetworkType::dhcp6);
1482     BMCWEB_LOG_DEBUG("set DomainEnabled for dhcp6...");
1483     setDHCPConfig("DomainEnabled", nextUsev6Domain, asyncResp, ifaceId,
1484                   NetworkType::dhcp6);
1485 }
1486 
1487 inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1488     const std::vector<IPv4AddressData>::const_iterator& head,
1489     const std::vector<IPv4AddressData>::const_iterator& end)
1490 {
1491     return std::find_if(head, end, [](const IPv4AddressData& value) {
1492         return value.origin == "Static";
1493     });
1494 }
1495 
1496 inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1497     const std::vector<IPv6AddressData>::const_iterator& head,
1498     const std::vector<IPv6AddressData>::const_iterator& end)
1499 {
1500     return std::find_if(head, end, [](const IPv6AddressData& value) {
1501         return value.origin == "Static";
1502     });
1503 }
1504 
1505 inline void handleIPv4StaticPatch(
1506     const std::string& ifaceId,
1507     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1508     const EthernetInterfaceData& ethData,
1509     const std::vector<IPv4AddressData>& ipv4Data,
1510     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1511 {
1512     unsigned entryIdx = 1;
1513     // Find the first static IP address currently active on the NIC and
1514     // match it to the first JSON element in the IPv4StaticAddresses array.
1515     // Match each subsequent JSON element to the next static IP programmed
1516     // into the NIC.
1517     std::vector<IPv4AddressData>::const_iterator nicIpEntry =
1518         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1519 
1520     bool gatewayValueAssigned{};
1521     std::string activePath{};
1522     std::string activeGateway{};
1523     if (!ethData.defaultGateway.empty() && ethData.defaultGateway != "0.0.0.0")
1524     {
1525         // The NIC is already configured with a default gateway. Use this if
1526         // the leading entry in the PATCH is '{}', which is preserving an active
1527         // static address.
1528         activeGateway = ethData.defaultGateway;
1529         activePath = "IPv4StaticAddresses/1";
1530         gatewayValueAssigned = true;
1531     }
1532 
1533     for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1534          input)
1535     {
1536         std::string pathString = "IPv4StaticAddresses/" +
1537                                  std::to_string(entryIdx);
1538         nlohmann::json::object_t* obj =
1539             std::get_if<nlohmann::json::object_t>(&thisJson);
1540         if (obj == nullptr)
1541         {
1542             if (nicIpEntry != ipv4Data.cend())
1543             {
1544                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
1545                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1546                                                   ipv4Data.cend());
1547                 if (!gatewayValueAssigned && (nicIpEntry == ipv4Data.cend()))
1548                 {
1549                     // All entries have been processed, and this last has
1550                     // requested the IP address be deleted. No prior entry
1551                     // performed an action that created or modified a
1552                     // gateway. Deleting this IP address means the default
1553                     // gateway entry has to be removed as well.
1554                     updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1555                 }
1556                 entryIdx++;
1557                 continue;
1558             }
1559             // Received a DELETE action on an entry not assigned to the NIC
1560             messages::resourceCannotBeDeleted(asyncResp->res);
1561             return;
1562         }
1563 
1564         // An Add/Modify action is requested
1565         if (!obj->empty())
1566         {
1567             std::optional<std::string> address;
1568             std::optional<std::string> subnetMask;
1569             std::optional<std::string> gateway;
1570 
1571             if (!json_util::readJsonObject(*obj, asyncResp->res, "Address",
1572                                            address, "SubnetMask", subnetMask,
1573                                            "Gateway", gateway))
1574             {
1575                 messages::propertyValueFormatError(asyncResp->res, *obj,
1576                                                    pathString);
1577                 return;
1578             }
1579 
1580             // Find the address/subnet/gateway values. Any values that are
1581             // not explicitly provided are assumed to be unmodified from the
1582             // current state of the interface. Merge existing state into the
1583             // current request.
1584             if (address)
1585             {
1586                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
1587                 {
1588                     messages::propertyValueFormatError(asyncResp->res, *address,
1589                                                        pathString + "/Address");
1590                     return;
1591                 }
1592             }
1593             else if (nicIpEntry != ipv4Data.cend())
1594             {
1595                 address = (nicIpEntry->address);
1596             }
1597             else
1598             {
1599                 messages::propertyMissing(asyncResp->res,
1600                                           pathString + "/Address");
1601                 return;
1602             }
1603 
1604             uint8_t prefixLength = 0;
1605             if (subnetMask)
1606             {
1607                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1608                                                          &prefixLength))
1609                 {
1610                     messages::propertyValueFormatError(
1611                         asyncResp->res, *subnetMask,
1612                         pathString + "/SubnetMask");
1613                     return;
1614                 }
1615             }
1616             else if (nicIpEntry != ipv4Data.cend())
1617             {
1618                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1619                                                          &prefixLength))
1620                 {
1621                     messages::propertyValueFormatError(
1622                         asyncResp->res, nicIpEntry->netmask,
1623                         pathString + "/SubnetMask");
1624                     return;
1625                 }
1626             }
1627             else
1628             {
1629                 messages::propertyMissing(asyncResp->res,
1630                                           pathString + "/SubnetMask");
1631                 return;
1632             }
1633 
1634             if (gateway)
1635             {
1636                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
1637                 {
1638                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1639                                                        pathString + "/Gateway");
1640                     return;
1641                 }
1642             }
1643             else if (nicIpEntry != ipv4Data.cend())
1644             {
1645                 gateway = nicIpEntry->gateway;
1646             }
1647             else
1648             {
1649                 messages::propertyMissing(asyncResp->res,
1650                                           pathString + "/Gateway");
1651                 return;
1652             }
1653 
1654             if (gatewayValueAssigned)
1655             {
1656                 if (activeGateway != gateway)
1657                 {
1658                     // A NIC can only have a single active gateway value.
1659                     // If any gateway in the array of static addresses
1660                     // mismatch the PATCH is in error.
1661                     std::string arg1 = pathString + "/Gateway";
1662                     std::string arg2 = activePath + "/Gateway";
1663                     messages::propertyValueConflict(asyncResp->res, arg1, arg2);
1664                     return;
1665                 }
1666             }
1667             else
1668             {
1669                 // Capture the very first gateway value from the incoming
1670                 // JSON record and use it at the default gateway.
1671                 updateIPv4DefaultGateway(ifaceId, *gateway, asyncResp);
1672                 activeGateway = *gateway;
1673                 activePath = pathString;
1674                 gatewayValueAssigned = true;
1675             }
1676 
1677             if (nicIpEntry != ipv4Data.cend())
1678             {
1679                 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
1680                                          nicIpEntry->id, prefixLength, *address,
1681                                          *gateway, asyncResp);
1682                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1683                                                   ipv4Data.cend());
1684             }
1685             else
1686             {
1687                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1688                            asyncResp);
1689             }
1690             entryIdx++;
1691         }
1692         else
1693         {
1694             // Received {}, do not modify this address
1695             if (nicIpEntry != ipv4Data.cend())
1696             {
1697                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1698                                                   ipv4Data.cend());
1699                 entryIdx++;
1700             }
1701             else
1702             {
1703                 // Requested a DO NOT MODIFY action on an entry not assigned
1704                 // to the NIC
1705                 messages::propertyValueFormatError(asyncResp->res, *obj,
1706                                                    pathString);
1707                 return;
1708             }
1709         }
1710     }
1711 }
1712 
1713 inline void handleStaticNameServersPatch(
1714     const std::string& ifaceId,
1715     const std::vector<std::string>& updatedStaticNameServers,
1716     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1717 {
1718     setDbusProperty(
1719         asyncResp, "xyz.openbmc_project.Network",
1720         sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1721             ifaceId,
1722         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1723         "StaticNameServers", updatedStaticNameServers);
1724 }
1725 
1726 inline void handleIPv6StaticAddressesPatch(
1727     const std::string& ifaceId,
1728     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1729     const std::vector<IPv6AddressData>& ipv6Data,
1730     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1731 {
1732     size_t entryIdx = 1;
1733     std::vector<IPv6AddressData>::const_iterator nicIpEntry =
1734         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1735     for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1736          input)
1737     {
1738         std::string pathString = "IPv6StaticAddresses/" +
1739                                  std::to_string(entryIdx);
1740         nlohmann::json::object_t* obj =
1741             std::get_if<nlohmann::json::object_t>(&thisJson);
1742         if (obj != nullptr && !obj->empty())
1743         {
1744             std::optional<std::string> address;
1745             std::optional<uint8_t> prefixLength;
1746             nlohmann::json::object_t thisJsonCopy = *obj;
1747             if (!json_util::readJsonObject(thisJsonCopy, asyncResp->res,
1748                                            "Address", address, "PrefixLength",
1749                                            prefixLength))
1750             {
1751                 messages::propertyValueFormatError(asyncResp->res, thisJsonCopy,
1752                                                    pathString);
1753                 return;
1754             }
1755 
1756             // Find the address and prefixLength values. Any values that are
1757             // not explicitly provided are assumed to be unmodified from the
1758             // current state of the interface. Merge existing state into the
1759             // current request.
1760             if (!address)
1761             {
1762                 if (nicIpEntry == ipv6Data.end())
1763                 {
1764                     messages::propertyMissing(asyncResp->res,
1765                                               pathString + "/Address");
1766                     return;
1767                 }
1768                 address = nicIpEntry->address;
1769             }
1770 
1771             if (!prefixLength)
1772             {
1773                 if (nicIpEntry == ipv6Data.end())
1774                 {
1775                     messages::propertyMissing(asyncResp->res,
1776                                               pathString + "/PrefixLength");
1777                     return;
1778                 }
1779                 prefixLength = nicIpEntry->prefixLength;
1780             }
1781 
1782             if (nicIpEntry != ipv6Data.end())
1783             {
1784                 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
1785                                          nicIpEntry->id, *prefixLength,
1786                                          *address, "", asyncResp);
1787                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1788                                                   ipv6Data.cend());
1789             }
1790             else
1791             {
1792                 createIPv6(ifaceId, *prefixLength, *address, asyncResp);
1793             }
1794             entryIdx++;
1795         }
1796         else
1797         {
1798             if (nicIpEntry == ipv6Data.end())
1799             {
1800                 // Requesting a DELETE/DO NOT MODIFY action for an item
1801                 // that isn't present on the eth(n) interface. Input JSON is
1802                 // in error, so bail out.
1803                 if (obj == nullptr)
1804                 {
1805                     messages::resourceCannotBeDeleted(asyncResp->res);
1806                     return;
1807                 }
1808                 messages::propertyValueFormatError(asyncResp->res, *obj,
1809                                                    pathString);
1810                 return;
1811             }
1812 
1813             if (obj == nullptr)
1814             {
1815                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
1816             }
1817             if (nicIpEntry != ipv6Data.cend())
1818             {
1819                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1820                                                   ipv6Data.cend());
1821             }
1822             entryIdx++;
1823         }
1824     }
1825 }
1826 
1827 inline std::string extractParentInterfaceName(const std::string& ifaceId)
1828 {
1829     std::size_t pos = ifaceId.find('_');
1830     return ifaceId.substr(0, pos);
1831 }
1832 
1833 inline void
1834     parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1835                        const std::string& ifaceId,
1836                        const EthernetInterfaceData& ethData,
1837                        const std::vector<IPv4AddressData>& ipv4Data,
1838                        const std::vector<IPv6AddressData>& ipv6Data,
1839                        const std::vector<StaticGatewayData>& ipv6GatewayData)
1840 {
1841     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1842     jsonResponse["Id"] = ifaceId;
1843     jsonResponse["@odata.id"] =
1844         boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
1845                             BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceId);
1846     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1847 
1848     if (ethData.nicEnabled)
1849     {
1850         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1851         jsonResponse["Status"]["State"] = "Enabled";
1852     }
1853     else
1854     {
1855         jsonResponse["LinkStatus"] = "NoLink";
1856         jsonResponse["Status"]["State"] = "Disabled";
1857     }
1858 
1859     jsonResponse["SpeedMbps"] = ethData.speed;
1860     jsonResponse["MTUSize"] = ethData.mtuSize;
1861     jsonResponse["MACAddress"] = ethData.macAddress;
1862     jsonResponse["DHCPv4"]["DHCPEnabled"] =
1863         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1864     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1865     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
1866     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNamev4Enabled;
1867     jsonResponse["DHCPv6"]["OperatingMode"] =
1868         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Enabled"
1869                                                                : "Disabled";
1870     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1871     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
1872     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNamev6Enabled;
1873     jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1874         ethData.ipv6AcceptRa;
1875 
1876     if (!ethData.hostName.empty())
1877     {
1878         jsonResponse["HostName"] = ethData.hostName;
1879 
1880         // When domain name is empty then it means, that it is a network
1881         // without domain names, and the host name itself must be treated as
1882         // FQDN
1883         std::string fqdn = ethData.hostName;
1884         if (!ethData.domainnames.empty())
1885         {
1886             fqdn += "." + ethData.domainnames[0];
1887         }
1888         jsonResponse["FQDN"] = fqdn;
1889     }
1890 
1891     if (ethData.vlanId)
1892     {
1893         jsonResponse["EthernetInterfaceType"] = "Virtual";
1894         jsonResponse["VLAN"]["VLANEnable"] = true;
1895         jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1896         jsonResponse["VLAN"]["Tagged"] = true;
1897 
1898         nlohmann::json::array_t relatedInterfaces;
1899         nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1900         parentInterface["@odata.id"] =
1901             boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1902                                 BMCWEB_REDFISH_MANAGER_URI_NAME,
1903                                 extractParentInterfaceName(ifaceId));
1904         jsonResponse["Links"]["RelatedInterfaces"] =
1905             std::move(relatedInterfaces);
1906     }
1907     else
1908     {
1909         jsonResponse["EthernetInterfaceType"] = "Physical";
1910     }
1911 
1912     jsonResponse["NameServers"] = ethData.nameServers;
1913     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1914 
1915     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1916     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1917     ipv4Array = nlohmann::json::array();
1918     ipv4StaticArray = nlohmann::json::array();
1919     for (const auto& ipv4Config : ipv4Data)
1920     {
1921         std::string gatewayStr = ipv4Config.gateway;
1922         if (gatewayStr.empty())
1923         {
1924             gatewayStr = "0.0.0.0";
1925         }
1926         nlohmann::json::object_t ipv4;
1927         ipv4["AddressOrigin"] = ipv4Config.origin;
1928         ipv4["SubnetMask"] = ipv4Config.netmask;
1929         ipv4["Address"] = ipv4Config.address;
1930         ipv4["Gateway"] = gatewayStr;
1931 
1932         if (ipv4Config.origin == "Static")
1933         {
1934             ipv4StaticArray.push_back(ipv4);
1935         }
1936 
1937         ipv4Array.emplace_back(std::move(ipv4));
1938     }
1939 
1940     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
1941     if (ipv6GatewayStr.empty())
1942     {
1943         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1944     }
1945 
1946     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1947 
1948     nlohmann::json::array_t ipv6StaticGatewayArray;
1949     for (const auto& ipv6GatewayConfig : ipv6GatewayData)
1950     {
1951         nlohmann::json::object_t ipv6Gateway;
1952         ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
1953         ipv6Gateway["PrefixLength"] = ipv6GatewayConfig.prefixLength;
1954         ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
1955     }
1956     jsonResponse["IPv6StaticDefaultGateways"] =
1957         std::move(ipv6StaticGatewayArray);
1958 
1959     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1960     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1961     ipv6Array = nlohmann::json::array();
1962     ipv6StaticArray = nlohmann::json::array();
1963     nlohmann::json& ipv6AddrPolicyTable =
1964         jsonResponse["IPv6AddressPolicyTable"];
1965     ipv6AddrPolicyTable = nlohmann::json::array();
1966     for (const auto& ipv6Config : ipv6Data)
1967     {
1968         nlohmann::json::object_t ipv6;
1969         ipv6["Address"] = ipv6Config.address;
1970         ipv6["PrefixLength"] = ipv6Config.prefixLength;
1971         ipv6["AddressOrigin"] = ipv6Config.origin;
1972 
1973         ipv6Array.emplace_back(std::move(ipv6));
1974         if (ipv6Config.origin == "Static")
1975         {
1976             nlohmann::json::object_t ipv6Static;
1977             ipv6Static["Address"] = ipv6Config.address;
1978             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1979             ipv6StaticArray.emplace_back(std::move(ipv6Static));
1980         }
1981     }
1982 }
1983 
1984 inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1985                         const std::string& ifaceId,
1986                         const boost::system::error_code& ec,
1987                         const sdbusplus::message_t& m)
1988 {
1989     if (!ec)
1990     {
1991         return;
1992     }
1993     const sd_bus_error* dbusError = m.get_error();
1994     if (dbusError == nullptr)
1995     {
1996         messages::internalError(asyncResp->res);
1997         return;
1998     }
1999     BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
2000 
2001     if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
2002         dbusError->name)
2003     {
2004         messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2005                                    ifaceId);
2006         return;
2007     }
2008     if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
2009         dbusError->name)
2010     {
2011         messages::resourceCannotBeDeleted(asyncResp->res);
2012         return;
2013     }
2014     messages::internalError(asyncResp->res);
2015 }
2016 
2017 inline void afterVlanCreate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2018                             const std::string& parentInterfaceUri,
2019                             const std::string& vlanInterface,
2020                             const boost::system::error_code& ec,
2021                             const sdbusplus::message_t& m
2022 
2023 )
2024 {
2025     if (ec)
2026     {
2027         const sd_bus_error* dbusError = m.get_error();
2028         if (dbusError == nullptr)
2029         {
2030             messages::internalError(asyncResp->res);
2031             return;
2032         }
2033         BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
2034 
2035         if (std::string_view(
2036                 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
2037             dbusError->name)
2038         {
2039             messages::propertyValueNotInList(
2040                 asyncResp->res, parentInterfaceUri,
2041                 "Links/RelatedInterfaces/0/@odata.id");
2042             return;
2043         }
2044         if (std::string_view(
2045                 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
2046             dbusError->name)
2047         {
2048             messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
2049                                             "Id", vlanInterface);
2050             return;
2051         }
2052         messages::internalError(asyncResp->res);
2053         return;
2054     }
2055 
2056     const boost::urls::url vlanInterfaceUri =
2057         boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2058                             BMCWEB_REDFISH_MANAGER_URI_NAME, vlanInterface);
2059     asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
2060 }
2061 
2062 inline void requestEthernetInterfacesRoutes(App& app)
2063 {
2064     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
2065         .privileges(redfish::privileges::getEthernetInterfaceCollection)
2066         .methods(boost::beast::http::verb::get)(
2067             [&app](const crow::Request& req,
2068                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2069                    const std::string& managerId) {
2070         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2071         {
2072             return;
2073         }
2074 
2075         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2076         {
2077             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2078             return;
2079         }
2080 
2081         asyncResp->res.jsonValue["@odata.type"] =
2082             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2083         asyncResp->res.jsonValue["@odata.id"] =
2084             boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
2085                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2086         asyncResp->res.jsonValue["Name"] =
2087             "Ethernet Network Interface Collection";
2088         asyncResp->res.jsonValue["Description"] =
2089             "Collection of EthernetInterfaces for this Manager";
2090 
2091         // Get eth interface list, and call the below callback for JSON
2092         // preparation
2093         getEthernetIfaceList(
2094             [asyncResp](const bool& success,
2095                         const std::vector<std::string>& ifaceList) {
2096             if (!success)
2097             {
2098                 messages::internalError(asyncResp->res);
2099                 return;
2100             }
2101 
2102             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
2103             ifaceArray = nlohmann::json::array();
2104             for (const std::string& ifaceItem : ifaceList)
2105             {
2106                 nlohmann::json::object_t iface;
2107                 iface["@odata.id"] = boost::urls::format(
2108                     "/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2109                     BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceItem);
2110                 ifaceArray.push_back(std::move(iface));
2111             }
2112 
2113             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2114             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2115                 "/redfish/v1/Managers/{}/EthernetInterfaces",
2116                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2117         });
2118     });
2119 
2120     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
2121         .privileges(redfish::privileges::postEthernetInterfaceCollection)
2122         .methods(boost::beast::http::verb::post)(
2123             [&app](const crow::Request& req,
2124                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2125                    const std::string& managerId) {
2126         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2127         {
2128             return;
2129         }
2130 
2131         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2132         {
2133             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2134             return;
2135         }
2136 
2137         bool vlanEnable = false;
2138         uint32_t vlanId = 0;
2139         std::vector<nlohmann::json::object_t> relatedInterfaces;
2140 
2141         if (!json_util::readJsonPatch(req, asyncResp->res, "VLAN/VLANEnable",
2142                                       vlanEnable, "VLAN/VLANId", vlanId,
2143                                       "Links/RelatedInterfaces",
2144                                       relatedInterfaces))
2145         {
2146             return;
2147         }
2148 
2149         if (relatedInterfaces.size() != 1)
2150         {
2151             messages::arraySizeTooLong(asyncResp->res,
2152                                        "Links/RelatedInterfaces",
2153                                        relatedInterfaces.size());
2154             return;
2155         }
2156 
2157         std::string parentInterfaceUri;
2158         if (!json_util::readJsonObject(relatedInterfaces[0], asyncResp->res,
2159                                        "@odata.id", parentInterfaceUri))
2160         {
2161             messages::propertyMissing(asyncResp->res,
2162                                       "Links/RelatedInterfaces/0/@odata.id");
2163             return;
2164         }
2165         BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
2166 
2167         boost::system::result<boost::urls::url_view> parsedUri =
2168             boost::urls::parse_relative_ref(parentInterfaceUri);
2169         if (!parsedUri)
2170         {
2171             messages::propertyValueFormatError(
2172                 asyncResp->res, parentInterfaceUri,
2173                 "Links/RelatedInterfaces/0/@odata.id");
2174             return;
2175         }
2176 
2177         std::string parentInterface;
2178         if (!crow::utility::readUrlSegments(
2179                 *parsedUri, "redfish", "v1", "Managers", "bmc",
2180                 "EthernetInterfaces", std::ref(parentInterface)))
2181         {
2182             messages::propertyValueNotInList(
2183                 asyncResp->res, parentInterfaceUri,
2184                 "Links/RelatedInterfaces/0/@odata.id");
2185             return;
2186         }
2187 
2188         if (!vlanEnable)
2189         {
2190             // In OpenBMC implementation, VLANEnable cannot be false on
2191             // create
2192             messages::propertyValueIncorrect(asyncResp->res, "VLAN/VLANEnable",
2193                                              "false");
2194             return;
2195         }
2196 
2197         std::string vlanInterface = parentInterface + "_" +
2198                                     std::to_string(vlanId);
2199         crow::connections::systemBus->async_method_call(
2200             [asyncResp, parentInterfaceUri,
2201              vlanInterface](const boost::system::error_code& ec,
2202                             const sdbusplus::message_t& m) {
2203             afterVlanCreate(asyncResp, parentInterfaceUri, vlanInterface, ec,
2204                             m);
2205         },
2206             "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
2207             "xyz.openbmc_project.Network.VLAN.Create", "VLAN", parentInterface,
2208             vlanId);
2209     });
2210 
2211     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
2212         .privileges(redfish::privileges::getEthernetInterface)
2213         .methods(boost::beast::http::verb::get)(
2214             [&app](const crow::Request& req,
2215                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2216                    const std::string& managerId, const std::string& ifaceId) {
2217         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2218         {
2219             return;
2220         }
2221 
2222         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2223         {
2224             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2225             return;
2226         }
2227 
2228         getEthernetIfaceData(
2229             ifaceId,
2230             [asyncResp,
2231              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2232                       const std::vector<IPv4AddressData>& ipv4Data,
2233                       const std::vector<IPv6AddressData>& ipv6Data,
2234                       const std::vector<StaticGatewayData>& ipv6GatewayData) {
2235             if (!success)
2236             {
2237                 // TODO(Pawel)consider distinguish between non
2238                 // existing object, and other errors
2239                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2240                                            ifaceId);
2241                 return;
2242             }
2243 
2244             asyncResp->res.jsonValue["@odata.type"] =
2245                 "#EthernetInterface.v1_9_0.EthernetInterface";
2246             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
2247             asyncResp->res.jsonValue["Description"] =
2248                 "Management Network Interface";
2249 
2250             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data,
2251                                ipv6GatewayData);
2252         });
2253     });
2254 
2255     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
2256         .privileges(redfish::privileges::patchEthernetInterface)
2257         .methods(boost::beast::http::verb::patch)(
2258             [&app](const crow::Request& req,
2259                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2260                    const std::string& managerId, const std::string& ifaceId) {
2261         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2262         {
2263             return;
2264         }
2265 
2266         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2267         {
2268             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2269             return;
2270         }
2271 
2272         std::optional<std::string> hostname;
2273         std::optional<std::string> fqdn;
2274         std::optional<std::string> macAddress;
2275         std::optional<std::string> ipv6DefaultGateway;
2276         std::optional<
2277             std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2278             ipv4StaticAddresses;
2279         std::optional<
2280             std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2281             ipv6StaticAddresses;
2282         std::optional<
2283             std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2284             ipv6StaticDefaultGateways;
2285         std::optional<std::vector<std::string>> staticNameServers;
2286         std::optional<bool> ipv6AutoConfigEnabled;
2287         std::optional<bool> interfaceEnabled;
2288         std::optional<size_t> mtuSize;
2289         DHCPParameters v4dhcpParms;
2290         DHCPParameters v6dhcpParms;
2291         // clang-format off
2292         if (!json_util::readJsonPatch(req, asyncResp->res,
2293                 "DHCPv4/DHCPEnabled",   v4dhcpParms.dhcpv4Enabled,
2294                 "DHCPv4/UseDNSServers", v4dhcpParms.useDnsServers,
2295                 "DHCPv4/UseDomainName", v4dhcpParms.useDomainName,
2296                 "DHCPv4/UseNTPServers", v4dhcpParms.useNtpServers,
2297                 "DHCPv6/OperatingMode", v6dhcpParms.dhcpv6OperatingMode,
2298                 "DHCPv6/UseDNSServers", v6dhcpParms.useDnsServers,
2299                 "DHCPv6/UseDomainName", v6dhcpParms.useDomainName,
2300                 "DHCPv6/UseNTPServers", v6dhcpParms.useNtpServers,
2301                 "FQDN", fqdn,
2302                 "HostName", hostname,
2303                 "IPv4StaticAddresses", ipv4StaticAddresses,
2304                 "IPv6DefaultGateway", ipv6DefaultGateway,
2305                 "IPv6StaticAddresses", ipv6StaticAddresses,
2306                 "IPv6StaticDefaultGateways", ipv6StaticDefaultGateways,
2307                 "InterfaceEnabled", interfaceEnabled,
2308                 "MACAddress", macAddress,
2309                 "MTUSize", mtuSize,
2310                 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled", ipv6AutoConfigEnabled,
2311                 "StaticNameServers", staticNameServers
2312                 )
2313             )
2314         {
2315             return;
2316         }
2317         // clang-format on
2318 
2319         // Get single eth interface data, and call the below callback
2320         // for JSON preparation
2321         getEthernetIfaceData(
2322             ifaceId,
2323             [asyncResp, ifaceId, hostname = std::move(hostname),
2324              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2325              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2326              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2327              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
2328              ipv6StaticDefaultGateway = std::move(ipv6StaticDefaultGateways),
2329              staticNameServers = std::move(staticNameServers), mtuSize,
2330              ipv6AutoConfigEnabled, v4dhcpParms = std::move(v4dhcpParms),
2331              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2332                 const bool success, const EthernetInterfaceData& ethData,
2333                 const std::vector<IPv4AddressData>& ipv4Data,
2334                 const std::vector<IPv6AddressData>& ipv6Data,
2335                 const std::vector<StaticGatewayData>& ipv6GatewayData) mutable {
2336             if (!success)
2337             {
2338                 // ... otherwise return error
2339                 // TODO(Pawel)consider distinguish between non
2340                 // existing object, and other errors
2341                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2342                                            ifaceId);
2343                 return;
2344             }
2345 
2346             handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
2347                             asyncResp);
2348 
2349             if (hostname)
2350             {
2351                 handleHostnamePatch(*hostname, asyncResp);
2352             }
2353 
2354             if (ipv6AutoConfigEnabled)
2355             {
2356                 handleSLAACAutoConfigPatch(ifaceId, *ipv6AutoConfigEnabled,
2357                                            asyncResp);
2358             }
2359 
2360             if (fqdn)
2361             {
2362                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2363             }
2364 
2365             if (macAddress)
2366             {
2367                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2368             }
2369 
2370             if (ipv4StaticAddresses)
2371             {
2372                 handleIPv4StaticPatch(ifaceId, *ipv4StaticAddresses, ethData,
2373                                       ipv4Data, asyncResp);
2374             }
2375 
2376             if (staticNameServers)
2377             {
2378                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2379                                              asyncResp);
2380             }
2381 
2382             if (ipv6DefaultGateway)
2383             {
2384                 messages::propertyNotWritable(asyncResp->res,
2385                                               "IPv6DefaultGateway");
2386             }
2387 
2388             if (ipv6StaticAddresses)
2389             {
2390                 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
2391                                                ipv6Data, asyncResp);
2392             }
2393 
2394             if (ipv6StaticDefaultGateway)
2395             {
2396                 handleIPv6DefaultGateway(ifaceId, *ipv6StaticDefaultGateway,
2397                                          ipv6GatewayData, asyncResp);
2398             }
2399 
2400             if (interfaceEnabled)
2401             {
2402                 setDbusProperty(asyncResp, "xyz.openbmc_project.Network",
2403                                 sdbusplus::message::object_path(
2404                                     "/xyz/openbmc_project/network") /
2405                                     ifaceId,
2406                                 "xyz.openbmc_project.Network.EthernetInterface",
2407                                 "NICEnabled", "InterfaceEnabled",
2408                                 *interfaceEnabled);
2409             }
2410 
2411             if (mtuSize)
2412             {
2413                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2414             }
2415         });
2416     });
2417 
2418     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
2419         .privileges(redfish::privileges::deleteEthernetInterface)
2420         .methods(boost::beast::http::verb::delete_)(
2421             [&app](const crow::Request& req,
2422                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2423                    const std::string& managerId, const std::string& ifaceId) {
2424         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2425         {
2426             return;
2427         }
2428 
2429         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2430         {
2431             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2432             return;
2433         }
2434 
2435         crow::connections::systemBus->async_method_call(
2436             [asyncResp, ifaceId](const boost::system::error_code& ec,
2437                                  const sdbusplus::message_t& m) {
2438             afterDelete(asyncResp, ifaceId, ec, m);
2439         },
2440             "xyz.openbmc_project.Network",
2441             std::string("/xyz/openbmc_project/network/") + ifaceId,
2442             "xyz.openbmc_project.Object.Delete", "Delete");
2443     });
2444 }
2445 
2446 } // namespace redfish
2447