blob: 1031e3d3f73002341eecf4710f867b88d280988e [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01004#pragma once
5
Ed Tanousd7857202025-01-28 15:32:26 -08006#include "bmcweb_config.h"
7
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "app.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009#include "async_resp.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080010#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080011#include "error_messages.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070012#include "generated/enums/ethernet_interface.hpp"
13#include "generated/enums/resource.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080014#include "http_request.hpp"
15#include "http_response.hpp"
Ed Tanous2c5875a2023-05-15 09:56:06 -070016#include "human_sort.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080017#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "query.hpp"
19#include "registries/privilege_registry.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080020#include "utility.hpp"
21#include "utils/dbus_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070022#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070024
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <systemd/sd-bus.h>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
Ed Tanousd7857202025-01-28 15:32:26 -080027#include <boost/beast/http/verb.hpp>
28#include <boost/system/error_code.hpp>
29#include <boost/system/result.hpp>
30#include <boost/url/format.hpp>
31#include <boost/url/parse.hpp>
32#include <boost/url/url.hpp>
33#include <boost/url/url_view.hpp>
34#include <sdbusplus/message.hpp>
35#include <sdbusplus/message/native_types.hpp>
36#include <sdbusplus/unpack_properties.hpp>
37
38#include <algorithm>
39#include <cctype>
Ed Tanous3dfed532024-03-06 14:41:27 -080040#include <cstddef>
Ed Tanousd7857202025-01-28 15:32:26 -080041#include <cstdint>
42#include <format>
43#include <functional>
Sunitha Harishce73d5c2023-04-07 06:46:49 -050044#include <memory>
Ed Tanousa24526d2018-12-10 15:17:59 -080045#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070046#include <ranges>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053047#include <regex>
Ed Tanousd7857202025-01-28 15:32:26 -080048#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080049#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080050#include <utility>
Ed Tanous3dfed532024-03-06 14:41:27 -080051#include <variant>
Ed Tanous77179532023-02-28 10:45:28 -080052#include <vector>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010053
Ed Tanous1abe55e2018-09-05 08:30:59 -070054namespace redfish
55{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010056
Ed Tanous4a0cb852018-10-15 07:55:04 -070057enum class LinkType
58{
59 Local,
60 Global
61};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010062
Johnathan Mantey743eb1c2024-04-03 12:05:57 -070063enum class IpVersion
64{
65 IpV4,
66 IpV6
67};
68
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010069/**
70 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010071 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070072struct IPv4AddressData
73{
74 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070075 std::string address;
76 std::string domain;
77 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 std::string netmask;
79 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080080 LinkType linktype{};
81 bool isActive{};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010082};
83
84/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050085 * Structure for keeping IPv6 data required by Redfish
86 */
87struct IPv6AddressData
88{
89 std::string id;
90 std::string address;
91 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080092 uint8_t prefixLength = 0;
Ravi Tejae48c0fc2019-04-16 08:37:20 -050093};
Sunitha Harishce73d5c2023-04-07 06:46:49 -050094
95/**
96 * Structure for keeping static route data required by Redfish
97 */
98struct StaticGatewayData
99{
100 std::string id;
101 std::string gateway;
102 size_t prefixLength = 0;
103 std::string protocol;
104};
105
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500106/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100107 * Structure for keeping basic single Ethernet Interface information
108 * available from DBus
109 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110struct EthernetInterfaceData
111{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700112 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +0530113 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800114 bool autoNeg;
Jishnu CMe4588152023-05-11 00:04:40 -0500115 bool dnsv4Enabled;
116 bool dnsv6Enabled;
Ravi Teja91c441e2024-02-23 09:03:43 -0600117 bool domainv4Enabled;
118 bool domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -0500119 bool ntpv4Enabled;
120 bool ntpv6Enabled;
121 bool hostNamev4Enabled;
122 bool hostNamev6Enabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800123 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700124 bool nicEnabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -0500125 bool ipv6AcceptRa;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800126 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700127 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800128 std::string hostName;
129 std::string defaultGateway;
130 std::string ipv6DefaultGateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500131 std::string ipv6StaticDefaultGateway;
Asmitha Karunanithi4652c642024-07-30 11:35:53 -0500132 std::optional<std::string> macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800133 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500134 std::vector<std::string> nameServers;
135 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800136 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100137};
138
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700139struct DHCPParameters
140{
141 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800142 std::optional<bool> useDnsServers;
143 std::optional<bool> useNtpServers;
144 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700145 std::optional<std::string> dhcpv6OperatingMode;
146};
147
Ed Tanous4a0cb852018-10-15 07:55:04 -0700148// Helper function that changes bits netmask notation (i.e. /24)
149// into full dot notation
150inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700151{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700152 uint32_t value = 0xffffffff << (32 - bits);
153 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
154 std::to_string((value >> 16) & 0xff) + "." +
155 std::to_string((value >> 8) & 0xff) + "." +
156 std::to_string(value & 0xff);
157 return netmask;
158}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100159
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800160inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700161 bool isIPv4)
162{
163 if (isIPv4)
164 {
165 return (
166 (inputDHCP ==
167 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
168 (inputDHCP ==
Asmitha Karunanithi6e78b682024-12-13 04:45:31 -0600169 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both") ||
170 (inputDHCP ==
171 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4v6stateless"));
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700172 }
173 return ((inputDHCP ==
174 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
175 (inputDHCP ==
176 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
177}
178
Ed Tanous2c70f802020-09-28 14:29:23 -0700179inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700180{
181 if (isIPv4 && isIPv6)
182 {
183 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
184 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700185 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700186 {
187 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
188 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700189 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700190 {
191 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
192 }
193 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
194}
195
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400196inline std::string translateAddressOriginDbusToRedfish(
197 const std::string& inputOrigin, bool isIPv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700198{
199 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700201 return "Static";
202 }
203 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
204 {
205 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700206 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700207 return "IPv4LinkLocal";
208 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700209 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700210 }
211 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
212 {
213 if (isIPv4)
214 {
215 return "DHCP";
216 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700217 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700218 }
219 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
220 {
221 return "SLAAC";
222 }
223 return "";
224}
225
Ed Tanous02cad962022-06-30 16:50:15 -0700226inline bool extractEthernetInterfaceData(
227 const std::string& ethifaceId,
228 const dbus::utility::ManagedObjectType& dbusData,
229 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700230{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700231 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700232 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 {
Ed Tanous02cad962022-06-30 16:50:15 -0700234 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700235 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000236 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700238 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500241 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700244 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500245 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800246 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 if (mac != nullptr)
248 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800249 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700250 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700252 }
253 }
254 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
255 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500256 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700257 {
258 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500260 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800261 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700262 if (id != nullptr)
263 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800264 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700265 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700266 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700267 }
268 }
269 else if (ifacePair.first ==
270 "xyz.openbmc_project.Network.EthernetInterface")
271 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500272 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700273 {
274 if (propertyPair.first == "AutoNeg")
275 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700276 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800277 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700278 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700279 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800280 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700281 }
282 }
283 else if (propertyPair.first == "Speed")
284 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500285 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800286 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700287 if (speed != nullptr)
288 {
289 ethData.speed = *speed;
290 }
291 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530292 else if (propertyPair.first == "MTU")
293 {
Anthony3e7a8da2023-10-23 14:22:43 +0800294 const size_t* mtuSize =
295 std::get_if<size_t>(&propertyPair.second);
Tejas Patil35fb5312021-09-20 15:35:20 +0530296 if (mtuSize != nullptr)
297 {
298 ethData.mtuSize = *mtuSize;
299 }
300 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800301 else if (propertyPair.first == "LinkUp")
302 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500303 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800304 std::get_if<bool>(&propertyPair.second);
305 if (linkUp != nullptr)
306 {
307 ethData.linkUp = *linkUp;
308 }
309 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700310 else if (propertyPair.first == "NICEnabled")
311 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500312 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700313 std::get_if<bool>(&propertyPair.second);
314 if (nicEnabled != nullptr)
315 {
316 ethData.nicEnabled = *nicEnabled;
317 }
318 }
Ravi Tejab10d8db2022-05-24 09:04:12 -0500319 else if (propertyPair.first == "IPv6AcceptRA")
320 {
321 const bool* ipv6AcceptRa =
322 std::get_if<bool>(&propertyPair.second);
323 if (ipv6AcceptRa != nullptr)
324 {
325 ethData.ipv6AcceptRa = *ipv6AcceptRa;
326 }
327 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500328 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700329 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500330 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500331 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800332 &propertyPair.second);
333 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700334 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700335 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500336 }
337 }
338 else if (propertyPair.first == "StaticNameServers")
339 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500340 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500341 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500342 &propertyPair.second);
343 if (staticNameServers != nullptr)
344 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700345 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700346 }
347 }
manojkiraneda2a133282019-02-19 13:09:43 +0530348 else if (propertyPair.first == "DHCPEnabled")
349 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700350 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700351 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700352 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530353 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800354 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530355 }
356 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800357 else if (propertyPair.first == "DomainName")
358 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500359 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500360 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800361 &propertyPair.second);
362 if (domainNames != nullptr)
363 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700364 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800365 }
366 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500367 else if (propertyPair.first == "DefaultGateway")
368 {
369 const std::string* defaultGateway =
370 std::get_if<std::string>(&propertyPair.second);
371 if (defaultGateway != nullptr)
372 {
373 std::string defaultGatewayStr = *defaultGateway;
374 if (defaultGatewayStr.empty())
375 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800376 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500377 }
378 else
379 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800380 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500381 }
382 }
383 }
384 else if (propertyPair.first == "DefaultGateway6")
385 {
386 const std::string* defaultGateway6 =
387 std::get_if<std::string>(&propertyPair.second);
388 if (defaultGateway6 != nullptr)
389 {
390 std::string defaultGateway6Str =
391 *defaultGateway6;
392 if (defaultGateway6Str.empty())
393 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800394 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500395 "0:0:0:0:0:0:0:0";
396 }
397 else
398 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800399 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500400 defaultGateway6Str;
401 }
402 }
403 }
Ed Tanous029573d2019-02-01 10:57:49 -0800404 }
405 }
406 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700407
Jishnu CMe4588152023-05-11 00:04:40 -0500408 sdbusplus::message::object_path path(
409 "/xyz/openbmc_project/network");
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400410 sdbusplus::message::object_path dhcp4Path =
411 path / ethifaceId / "dhcp4";
Jishnu CMe4588152023-05-11 00:04:40 -0500412
413 if (sdbusplus::message::object_path(objpath.first) == dhcp4Path)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700414 {
415 if (ifacePair.first ==
416 "xyz.openbmc_project.Network.DHCPConfiguration")
417 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500418 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700419 {
420 if (propertyPair.first == "DNSEnabled")
421 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700422 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700423 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700424 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700425 {
Jishnu CMe4588152023-05-11 00:04:40 -0500426 ethData.dnsv4Enabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700427 }
428 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600429 else if (propertyPair.first == "DomainEnabled")
430 {
431 const bool* domainEnabled =
432 std::get_if<bool>(&propertyPair.second);
433 if (domainEnabled != nullptr)
434 {
435 ethData.domainv4Enabled = *domainEnabled;
436 }
437 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700438 else if (propertyPair.first == "NTPEnabled")
439 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700440 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700441 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700442 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700443 {
Jishnu CMe4588152023-05-11 00:04:40 -0500444 ethData.ntpv4Enabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700445 }
446 }
447 else if (propertyPair.first == "HostNameEnabled")
448 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700449 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700450 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700451 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700452 {
Jishnu CMe4588152023-05-11 00:04:40 -0500453 ethData.hostNamev4Enabled = *hostNameEnabled;
454 }
455 }
456 }
457 }
458 }
459
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400460 sdbusplus::message::object_path dhcp6Path =
461 path / ethifaceId / "dhcp6";
Jishnu CMe4588152023-05-11 00:04:40 -0500462
463 if (sdbusplus::message::object_path(objpath.first) == dhcp6Path)
464 {
465 if (ifacePair.first ==
466 "xyz.openbmc_project.Network.DHCPConfiguration")
467 {
468 for (const auto& propertyPair : ifacePair.second)
469 {
470 if (propertyPair.first == "DNSEnabled")
471 {
472 const bool* dnsEnabled =
473 std::get_if<bool>(&propertyPair.second);
474 if (dnsEnabled != nullptr)
475 {
476 ethData.dnsv6Enabled = *dnsEnabled;
477 }
478 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600479 if (propertyPair.first == "DomainEnabled")
480 {
481 const bool* domainEnabled =
482 std::get_if<bool>(&propertyPair.second);
483 if (domainEnabled != nullptr)
484 {
485 ethData.domainv6Enabled = *domainEnabled;
486 }
487 }
Jishnu CMe4588152023-05-11 00:04:40 -0500488 else if (propertyPair.first == "NTPEnabled")
489 {
490 const bool* ntpEnabled =
491 std::get_if<bool>(&propertyPair.second);
492 if (ntpEnabled != nullptr)
493 {
494 ethData.ntpv6Enabled = *ntpEnabled;
495 }
496 }
497 else if (propertyPair.first == "HostNameEnabled")
498 {
499 const bool* hostNameEnabled =
500 std::get_if<bool>(&propertyPair.second);
501 if (hostNameEnabled != nullptr)
502 {
503 ethData.hostNamev6Enabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700504 }
505 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700506 }
507 }
508 }
Ed Tanous029573d2019-02-01 10:57:49 -0800509 // System configuration shows up in the global namespace, so no need
510 // to check eth number
511 if (ifacePair.first ==
512 "xyz.openbmc_project.Network.SystemConfiguration")
513 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500514 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800515 {
516 if (propertyPair.first == "HostName")
517 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500518 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500519 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800520 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700521 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800522 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800523 }
524 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700525 }
526 }
527 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700528 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700529 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700530}
531
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500532// Helper function that extracts data for single ethernet ipv6 address
Ed Tanous77179532023-02-28 10:45:28 -0800533inline void extractIPV6Data(const std::string& ethifaceId,
534 const dbus::utility::ManagedObjectType& dbusData,
535 std::vector<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500536{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400537 const std::string ipPathStart =
538 "/xyz/openbmc_project/network/" + ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500539
540 // Since there might be several IPv6 configurations aligned with
541 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000542 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500543 {
544 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800545 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500546 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800547 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500548 {
549 if (interface.first == "xyz.openbmc_project.Network.IP")
550 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400551 auto type = std::ranges::find_if(
552 interface.second, [](const auto& property) {
553 return property.first == "Type";
554 });
Tony Lee353163e2022-11-23 11:06:10 +0800555 if (type == interface.second.end())
556 {
557 continue;
558 }
559
560 const std::string* typeStr =
561 std::get_if<std::string>(&type->second);
562
563 if (typeStr == nullptr ||
564 (*typeStr !=
565 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
566 {
567 continue;
568 }
569
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500570 // Instance IPv6AddressData structure, and set as
571 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800572 IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700573 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800574 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800575 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500576 {
577 if (property.first == "Address")
578 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500579 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500580 std::get_if<std::string>(&property.second);
581 if (address != nullptr)
582 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700583 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500584 }
585 }
586 else if (property.first == "Origin")
587 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500588 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500589 std::get_if<std::string>(&property.second);
590 if (origin != nullptr)
591 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700592 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500593 translateAddressOriginDbusToRedfish(*origin,
594 false);
595 }
596 }
597 else if (property.first == "PrefixLength")
598 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500599 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500600 std::get_if<uint8_t>(&property.second);
601 if (prefix != nullptr)
602 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700603 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500604 }
605 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600606 else if (property.first == "Type" ||
607 property.first == "Gateway")
608 {
609 // Type & Gateway is not used
610 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500611 else
612 {
Ed Tanous62598e32023-07-17 17:06:25 -0700613 BMCWEB_LOG_ERROR(
614 "Got extra property: {} on the {} object",
615 property.first, objpath.first.str);
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500616 }
617 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500618 }
619 }
620 }
621 }
622}
623
Ed Tanous4a0cb852018-10-15 07:55:04 -0700624// Helper function that extracts data for single ethernet ipv4 address
Ed Tanous77179532023-02-28 10:45:28 -0800625inline void extractIPData(const std::string& ethifaceId,
626 const dbus::utility::ManagedObjectType& dbusData,
627 std::vector<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700628{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400629 const std::string ipPathStart =
630 "/xyz/openbmc_project/network/" + ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700631
632 // Since there might be several IPv4 configurations aligned with
633 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000634 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700635 {
636 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800637 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800639 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700640 {
641 if (interface.first == "xyz.openbmc_project.Network.IP")
642 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400643 auto type = std::ranges::find_if(
644 interface.second, [](const auto& property) {
645 return property.first == "Type";
646 });
Tony Lee353163e2022-11-23 11:06:10 +0800647 if (type == interface.second.end())
648 {
649 continue;
650 }
651
652 const std::string* typeStr =
653 std::get_if<std::string>(&type->second);
654
655 if (typeStr == nullptr ||
656 (*typeStr !=
657 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
658 {
659 continue;
660 }
661
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 // Instance IPv4AddressData structure, and set as
663 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800664 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700665 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800666 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800667 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700668 {
669 if (property.first == "Address")
670 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500671 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800672 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700673 if (address != nullptr)
674 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700675 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700676 }
677 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700678 else if (property.first == "Origin")
679 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500680 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800681 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700682 if (origin != nullptr)
683 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700684 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700685 translateAddressOriginDbusToRedfish(*origin,
686 true);
687 }
688 }
689 else if (property.first == "PrefixLength")
690 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500691 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800692 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700693 if (mask != nullptr)
694 {
695 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700696 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700697 }
698 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600699 else if (property.first == "Type" ||
700 property.first == "Gateway")
701 {
702 // Type & Gateway is not used
703 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700704 else
705 {
Ed Tanous62598e32023-07-17 17:06:25 -0700706 BMCWEB_LOG_ERROR(
707 "Got extra property: {} on the {} object",
708 property.first, objpath.first.str);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700709 }
710 }
711 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700712 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700713 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700714 ? LinkType::Local
715 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700716 }
717 }
718 }
719 }
720}
721
722/**
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700723 * @brief Modifies the default gateway assigned to the NIC
724 *
725 * @param[in] ifaceId Id of network interface whose default gateway is to be
726 * changed
727 * @param[in] gateway The new gateway value. Assigning an empty string
728 * causes the gateway to be deleted
729 * @param[io] asyncResp Response object that will be returned to client
730 *
731 * @return None
732 */
733inline void updateIPv4DefaultGateway(
734 const std::string& ifaceId, const std::string& gateway,
735 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
736{
737 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +0530738 asyncResp, "Gateway", "xyz.openbmc_project.Network",
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700739 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
740 ifaceId,
741 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ginu Georgee93abac2024-06-14 17:35:27 +0530742 gateway);
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700743}
744
745/**
746 * @brief Deletes given static IP address for the interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700747 *
748 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700749 * @param[in] ipHash DBus Hash id of IP that should be deleted
750 * @param[io] asyncResp Response object that will be returned to client
751 *
752 * @return None
753 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600754inline void deleteIPAddress(const std::string& ifaceId,
755 const std::string& ipHash,
756 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700757{
Ed Tanous177612a2025-02-14 15:16:09 -0800758 dbus::utility::async_method_call(
759 asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800760 [asyncResp](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400761 if (ec)
762 {
763 messages::internalError(asyncResp->res);
764 }
765 },
Ed Tanous4a0cb852018-10-15 07:55:04 -0700766 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600767 "/xyz/openbmc_project/network/" + ifaceId + ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700768 "xyz.openbmc_project.Object.Delete", "Delete");
769}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700770
Ed Tanous4a0cb852018-10-15 07:55:04 -0700771/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700772 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700773 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700774 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
775 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
776 * @param[in] gateway IPv4 address of this interfaces gateway
777 * @param[in] address IPv4 address to assign to this interface
778 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700779 *
780 * @return None
781 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000782inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
783 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800784 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700785{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400786 auto createIpHandler =
787 [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
788 if (ec)
789 {
790 messages::internalError(asyncResp->res);
791 return;
792 }
793 };
Ravi Teja9010ec22019-08-01 23:30:25 -0500794
Ed Tanous177612a2025-02-14 15:16:09 -0800795 dbus::utility::async_method_call(
796 asyncResp, std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700797 "/xyz/openbmc_project/network/" + ifaceId,
798 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700799 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700800 gateway);
801}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500802
803/**
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700804 * @brief Deletes the IP entry for this interface and creates a replacement
805 * static entry
Johnathan Mantey01784822019-06-18 12:44:21 -0700806 *
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700807 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
808 * @param[in] id The unique hash entry identifying the DBus entry
809 * @param[in] prefixLength Prefix syntax for the subnet mask
810 * @param[in] address Address to assign to this interface
811 * @param[in] numStaticAddrs Count of IPv4 static addresses
812 * @param[io] asyncResp Response object that will be returned to client
Johnathan Mantey01784822019-06-18 12:44:21 -0700813 *
814 * @return None
815 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600816
Ravi Teja9c5e5852023-02-26 21:33:52 -0600817inline void deleteAndCreateIPAddress(
818 IpVersion version, const std::string& ifaceId, const std::string& id,
819 uint8_t prefixLength, const std::string& address,
820 const std::string& gateway,
821 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700822{
Ed Tanous177612a2025-02-14 15:16:09 -0800823 dbus::utility::async_method_call(
824 asyncResp,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600825 [asyncResp, version, ifaceId, address, prefixLength,
826 gateway](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400827 if (ec)
Johnathan Mantey01784822019-06-18 12:44:21 -0700828 {
829 messages::internalError(asyncResp->res);
830 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400831 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
832 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous177612a2025-02-14 15:16:09 -0800833 dbus::utility::async_method_call(
834 asyncResp,
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400835 [asyncResp](const boost::system::error_code& ec2) {
836 if (ec2)
837 {
838 messages::internalError(asyncResp->res);
839 }
840 },
841 "xyz.openbmc_project.Network",
842 "/xyz/openbmc_project/network/" + ifaceId,
843 "xyz.openbmc_project.Network.IP.Create", "IP", protocol,
844 address, prefixLength, gateway);
Patrick Williams5a39f772023-10-20 11:20:21 -0500845 },
Johnathan Mantey01784822019-06-18 12:44:21 -0700846 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600847 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700848 "xyz.openbmc_project.Object.Delete", "Delete");
849}
850
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500851inline bool extractIPv6DefaultGatewayData(
852 const std::string& ethifaceId,
853 const dbus::utility::ManagedObjectType& dbusData,
854 std::vector<StaticGatewayData>& staticGatewayConfig)
855{
856 std::string staticGatewayPathStart("/xyz/openbmc_project/network/");
857 staticGatewayPathStart += ethifaceId;
858
859 for (const auto& objpath : dbusData)
860 {
861 if (!std::string_view(objpath.first.str)
862 .starts_with(staticGatewayPathStart))
863 {
864 continue;
865 }
866 for (const auto& interface : objpath.second)
867 {
868 if (interface.first != "xyz.openbmc_project.Network.StaticGateway")
869 {
870 continue;
871 }
872 StaticGatewayData& staticGateway =
873 staticGatewayConfig.emplace_back();
874 staticGateway.id = objpath.first.filename();
875
876 bool success = sdbusplus::unpackPropertiesNoThrow(
877 redfish::dbus_utils::UnpackErrorPrinter(), interface.second,
Ravi Tejaab0d4392024-09-03 12:27:40 -0500878 "Gateway", staticGateway.gateway, "ProtocolType",
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500879 staticGateway.protocol);
880 if (!success)
881 {
882 return false;
883 }
884 }
885 }
886 return true;
887}
888
Johnathan Mantey01784822019-06-18 12:44:21 -0700889/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500890 * @brief Creates IPv6 with given data
891 *
892 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500893 * @param[in] prefixLength Prefix length that needs to be added
894 * @param[in] address IP address that needs to be added
895 * @param[io] asyncResp Response object that will be returned to client
896 *
897 * @return None
898 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500899inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
900 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800901 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500902{
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500903 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
904 path /= ifaceId;
905
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400906 auto createIpHandler =
907 [asyncResp, address](const boost::system::error_code& ec) {
908 if (ec)
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500909 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400910 if (ec == boost::system::errc::io_error)
911 {
912 messages::propertyValueFormatError(asyncResp->res, address,
913 "Address");
914 }
915 else
916 {
917 messages::internalError(asyncResp->res);
918 }
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500919 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400920 };
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500921 // Passing null for gateway, as per redfish spec IPv6StaticAddresses
922 // object does not have associated gateway property
Ed Tanous177612a2025-02-14 15:16:09 -0800923 dbus::utility::async_method_call(
924 asyncResp, std::move(createIpHandler), "xyz.openbmc_project.Network",
925 path, "xyz.openbmc_project.Network.IP.Create", "IP",
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500926 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
927 "");
928}
929
Ed Tanous4a0cb852018-10-15 07:55:04 -0700930/**
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500931 * @brief Deletes given IPv6 Static Gateway
932 *
933 * @param[in] ifaceId Id of interface whose IP should be deleted
934 * @param[in] ipHash DBus Hash id of IP that should be deleted
935 * @param[io] asyncResp Response object that will be returned to client
936 *
937 * @return None
938 */
Patrick Williams504af5a2025-02-03 14:29:03 -0500939inline void deleteIPv6Gateway(
940 std::string_view ifaceId, std::string_view gatewayId,
941 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500942{
943 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
Ravi Teja739b27b2024-08-27 21:03:53 -0500944 path /= ifaceId;
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500945 path /= gatewayId;
Ed Tanous177612a2025-02-14 15:16:09 -0800946 dbus::utility::async_method_call(
947 asyncResp,
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500948 [asyncResp](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400949 if (ec)
950 {
951 messages::internalError(asyncResp->res);
952 }
953 },
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500954 "xyz.openbmc_project.Network", path,
955 "xyz.openbmc_project.Object.Delete", "Delete");
956}
957
958/**
959 * @brief Creates IPv6 static default gateway with given data
960 *
961 * @param[in] ifaceId Id of interface whose IP should be added
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500962 * @param[in] gateway Gateway address that needs to be added
963 * @param[io] asyncResp Response object that will be returned to client
964 *
965 * @return None
966 */
967inline void createIPv6DefaultGateway(
Asmitha Karunanithicf91c8c2025-01-24 04:39:15 -0600968 std::string_view ifaceId, const std::string& gateway,
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500969 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
970{
971 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
972 path /= ifaceId;
973 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
974 if (ec)
975 {
976 messages::internalError(asyncResp->res);
977 }
978 };
Ed Tanous177612a2025-02-14 15:16:09 -0800979 dbus::utility::async_method_call(
980 asyncResp, std::move(createIpHandler), "xyz.openbmc_project.Network",
981 path, "xyz.openbmc_project.Network.StaticGateway.Create",
982 "StaticGateway", gateway,
983 "xyz.openbmc_project.Network.IP.Protocol.IPv6");
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500984}
985
986/**
987 * @brief Deletes the IPv6 default gateway entry for this interface and
988 * creates a replacement IPv6 default gateway entry
989 *
990 * @param[in] ifaceId Id of interface upon which to create the IPv6
991 * entry
992 * @param[in] gateway IPv6 gateway to assign to this interface
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500993 * @param[io] asyncResp Response object that will be returned to client
994 *
995 * @return None
996 */
997inline void deleteAndCreateIPv6DefaultGateway(
998 std::string_view ifaceId, std::string_view gatewayId,
Asmitha Karunanithicf91c8c2025-01-24 04:39:15 -0600999 const std::string& gateway,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001000 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1001{
1002 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
Ravi Teja739b27b2024-08-27 21:03:53 -05001003 path /= ifaceId;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001004 path /= gatewayId;
Ed Tanous177612a2025-02-14 15:16:09 -08001005 dbus::utility::async_method_call(
1006 asyncResp,
Ravi Tejaab0d4392024-09-03 12:27:40 -05001007 [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001008 if (ec)
1009 {
1010 messages::internalError(asyncResp->res);
1011 return;
1012 }
Ravi Tejaab0d4392024-09-03 12:27:40 -05001013 createIPv6DefaultGateway(ifaceId, gateway, asyncResp);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001014 },
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001015 "xyz.openbmc_project.Network", path,
1016 "xyz.openbmc_project.Object.Delete", "Delete");
1017}
1018
1019/**
1020 * @brief Sets IPv6 default gateway with given data
1021 *
1022 * @param[in] ifaceId Id of interface whose gateway should be added
1023 * @param[in] input Contains address that needs to be added
1024 * @param[in] staticGatewayData Current static gateways in the system
1025 * @param[io] asyncResp Response object that will be returned to client
1026 *
1027 * @return None
1028 */
1029
1030inline void handleIPv6DefaultGateway(
Ed Tanous3dfed532024-03-06 14:41:27 -08001031 const std::string& ifaceId,
1032 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001033 const std::vector<StaticGatewayData>& staticGatewayData,
1034 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1035{
1036 size_t entryIdx = 1;
1037 std::vector<StaticGatewayData>::const_iterator staticGatewayEntry =
1038 staticGatewayData.begin();
1039
Ed Tanous3dfed532024-03-06 14:41:27 -08001040 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1041 input)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001042 {
1043 // find the next gateway entry
1044 while (staticGatewayEntry != staticGatewayData.end())
1045 {
1046 if (staticGatewayEntry->protocol ==
1047 "xyz.openbmc_project.Network.IP.Protocol.IPv6")
1048 {
1049 break;
1050 }
1051 staticGatewayEntry++;
1052 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001053 std::string pathString =
1054 "IPv6StaticDefaultGateways/" + std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001055 nlohmann::json::object_t* obj =
1056 std::get_if<nlohmann::json::object_t>(&thisJson);
1057 if (obj == nullptr)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001058 {
1059 if (staticGatewayEntry == staticGatewayData.end())
1060 {
1061 messages::resourceCannotBeDeleted(asyncResp->res);
1062 return;
1063 }
Ravi Teja739b27b2024-08-27 21:03:53 -05001064 deleteIPv6Gateway(ifaceId, staticGatewayEntry->id, asyncResp);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001065 return;
1066 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001067 if (obj->empty())
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001068 {
1069 // Do nothing, but make sure the entry exists.
1070 if (staticGatewayEntry == staticGatewayData.end())
1071 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001072 messages::propertyValueFormatError(asyncResp->res, *obj,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001073 pathString);
1074 return;
1075 }
1076 }
1077 std::optional<std::string> address;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001078
Ravi Tejaab0d4392024-09-03 12:27:40 -05001079 if (!json_util::readJsonObject(*obj, asyncResp->res, "Address",
1080 address))
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001081 {
1082 return;
1083 }
1084 const std::string* addr = nullptr;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001085 if (address)
1086 {
1087 addr = &(*address);
1088 }
1089 else if (staticGatewayEntry != staticGatewayData.end())
1090 {
1091 addr = &(staticGatewayEntry->gateway);
1092 }
1093 else
1094 {
1095 messages::propertyMissing(asyncResp->res, pathString + "/Address");
1096 return;
1097 }
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001098 if (staticGatewayEntry != staticGatewayData.end())
1099 {
1100 deleteAndCreateIPv6DefaultGateway(ifaceId, staticGatewayEntry->id,
Ravi Tejaab0d4392024-09-03 12:27:40 -05001101 *addr, asyncResp);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001102 staticGatewayEntry++;
1103 }
1104 else
1105 {
Ravi Tejaab0d4392024-09-03 12:27:40 -05001106 createIPv6DefaultGateway(ifaceId, *addr, asyncResp);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001107 }
1108 entryIdx++;
1109 }
1110}
1111
1112/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001113 * Function that retrieves all properties for given Ethernet Interface
1114 * Object
1115 * from EntityManager Network Manager
1116 * @param ethiface_id a eth interface id to query on DBus
1117 * @param callback a function that shall be called to convert Dbus output
1118 * into JSON
1119 */
1120template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +00001121void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001122 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001123{
George Liuf5892d02023-03-01 10:37:08 +08001124 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1125 dbus::utility::getManagedObjects(
1126 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001127 [ethifaceId{std::string{ethifaceId}},
Ed Tanous8cb2c022024-03-27 16:31:46 -07001128 callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001129 const boost::system::error_code& ec,
Ed Tanous3dfed532024-03-06 14:41:27 -08001130 const dbus::utility::ManagedObjectType& resp) mutable {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001131 EthernetInterfaceData ethData{};
1132 std::vector<IPv4AddressData> ipv4Data;
1133 std::vector<IPv6AddressData> ipv6Data;
1134 std::vector<StaticGatewayData> ipv6GatewayData;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001135
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001136 if (ec)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001137 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001138 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1139 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001140 }
1141
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001142 bool found =
1143 extractEthernetInterfaceData(ethifaceId, resp, ethData);
1144 if (!found)
1145 {
1146 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1147 return;
1148 }
1149
1150 extractIPData(ethifaceId, resp, ipv4Data);
1151 // Fix global GW
1152 for (IPv4AddressData& ipv4 : ipv4Data)
1153 {
1154 if (((ipv4.linktype == LinkType::Global) &&
1155 (ipv4.gateway == "0.0.0.0")) ||
1156 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
1157 {
1158 ipv4.gateway = ethData.defaultGateway;
1159 }
1160 }
1161
1162 extractIPV6Data(ethifaceId, resp, ipv6Data);
1163 if (!extractIPv6DefaultGatewayData(ethifaceId, resp,
1164 ipv6GatewayData))
1165 {
1166 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1167 }
1168 // Finally make a callback with useful data
1169 callback(true, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1170 });
Ed Tanous271584a2019-07-09 16:24:22 -07001171}
Ed Tanous4a0cb852018-10-15 07:55:04 -07001172
1173/**
1174 * Function that retrieves all Ethernet Interfaces available through Network
1175 * Manager
1176 * @param callback a function that shall be called to convert Dbus output
1177 * into JSON.
1178 */
1179template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001180void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001181{
George Liuf5892d02023-03-01 10:37:08 +08001182 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1183 dbus::utility::getManagedObjects(
1184 "xyz.openbmc_project.Network", path,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001185 [callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001186 const boost::system::error_code& ec,
George Liuf5892d02023-03-01 10:37:08 +08001187 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001188 // Callback requires vector<string> to retrieve all available
1189 // ethernet interfaces
1190 std::vector<std::string> ifaceList;
1191 ifaceList.reserve(resp.size());
1192 if (ec)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001193 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001194 callback(false, ifaceList);
1195 return;
1196 }
1197
1198 // Iterate over all retrieved ObjectPaths.
1199 for (const auto& objpath : resp)
1200 {
1201 // And all interfaces available for certain ObjectPath.
1202 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001203 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001204 // If interface is
1205 // xyz.openbmc_project.Network.EthernetInterface, this is
1206 // what we're looking for.
1207 if (interface.first ==
1208 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001209 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001210 std::string ifaceId = objpath.first.filename();
1211 if (ifaceId.empty())
1212 {
1213 continue;
1214 }
1215 // and put it into output vector.
1216 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001217 }
1218 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001219 }
Ed Tanous2c5875a2023-05-15 09:56:06 -07001220
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001221 std::ranges::sort(ifaceList, AlphanumLess<std::string>());
Ed Tanous2c5875a2023-05-15 09:56:06 -07001222
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001223 // Finally make a callback with useful data
1224 callback(true, ifaceList);
1225 });
Ed Tanous271584a2019-07-09 16:24:22 -07001226}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001227
Patrick Williams504af5a2025-02-03 14:29:03 -05001228inline void handleHostnamePatch(
1229 const std::string& hostname,
1230 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001231{
Ed Tanousbf648f72021-06-03 15:00:14 -07001232 // SHOULD handle host names of up to 255 characters(RFC 1123)
1233 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001234 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001235 messages::propertyValueFormatError(asyncResp->res, hostname,
1236 "HostName");
1237 return;
1238 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001239 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301240 asyncResp, "HostName", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001241 sdbusplus::message::object_path("/xyz/openbmc_project/network/config"),
1242 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ginu Georgee93abac2024-06-14 17:35:27 +05301243 hostname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001244}
1245
Patrick Williams504af5a2025-02-03 14:29:03 -05001246inline void handleMTUSizePatch(
1247 const std::string& ifaceId, const size_t mtuSize,
1248 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Tejas Patil35fb5312021-09-20 15:35:20 +05301249{
Ed Tanousd02aad32024-02-13 14:43:34 -08001250 sdbusplus::message::object_path objPath("/xyz/openbmc_project/network");
1251 objPath /= ifaceId;
Ginu Georgee93abac2024-06-14 17:35:27 +05301252 setDbusProperty(asyncResp, "MTUSize", "xyz.openbmc_project.Network",
1253 objPath, "xyz.openbmc_project.Network.EthernetInterface",
1254 "MTU", mtuSize);
Tejas Patil35fb5312021-09-20 15:35:20 +05301255}
1256
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001257inline void handleDomainnamePatch(
1258 const std::string& ifaceId, const std::string& domainname,
1259 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001260{
1261 std::vector<std::string> vectorDomainname = {domainname};
Ed Tanousd02aad32024-02-13 14:43:34 -08001262 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301263 asyncResp, "FQDN", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001264 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1265 ifaceId,
Ginu Georgee93abac2024-06-14 17:35:27 +05301266 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanousd02aad32024-02-13 14:43:34 -08001267 vectorDomainname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001268}
1269
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001270inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001271{
1272 // A valid host name can never have the dotted-decimal form (RFC 1123)
Ed Tanous3544d2a2023-08-06 18:12:20 -07001273 if (std::ranges::all_of(hostname, ::isdigit))
Ed Tanousbf648f72021-06-03 15:00:14 -07001274 {
1275 return false;
1276 }
1277 // Each label(hostname/subdomains) within a valid FQDN
1278 // MUST handle host names of up to 63 characters (RFC 1123)
1279 // labels cannot start or end with hyphens (RFC 952)
1280 // labels can start with numbers (RFC 1123)
Ed Tanous4b242742023-05-11 09:51:51 -07001281 const static std::regex pattern(
Ed Tanousbf648f72021-06-03 15:00:14 -07001282 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1283
1284 return std::regex_match(hostname, pattern);
1285}
1286
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001287inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001288{
1289 // Can have multiple subdomains
1290 // Top Level Domain's min length is 2 character
Ed Tanous4b242742023-05-11 09:51:51 -07001291 const static std::regex pattern(
George Liu0fda0f12021-11-16 10:06:17 +08001292 "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
Ed Tanousbf648f72021-06-03 15:00:14 -07001293
1294 return std::regex_match(domainname, pattern);
1295}
1296
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001297inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1298 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001299{
1300 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1301 if (fqdn.length() > 255)
1302 {
1303 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1304 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001305 }
1306
Ed Tanousbf648f72021-06-03 15:00:14 -07001307 size_t pos = fqdn.find('.');
1308 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001309 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001310 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1311 return;
1312 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001313
Ed Tanousbf648f72021-06-03 15:00:14 -07001314 std::string hostname;
1315 std::string domainname;
1316 domainname = (fqdn).substr(pos + 1);
1317 hostname = (fqdn).substr(0, pos);
1318
1319 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1320 {
1321 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1322 return;
1323 }
1324
1325 handleHostnamePatch(hostname, asyncResp);
1326 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1327}
1328
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001329inline void handleMACAddressPatch(
1330 const std::string& ifaceId, const std::string& macAddress,
1331 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001332{
Ed Tanousd02aad32024-02-13 14:43:34 -08001333 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301334 asyncResp, "MACAddress", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001335 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1336 ifaceId,
Ginu Georgee93abac2024-06-14 17:35:27 +05301337 "xyz.openbmc_project.Network.MACAddress", "MACAddress", macAddress);
Ed Tanousbf648f72021-06-03 15:00:14 -07001338}
1339
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001340inline void setDHCPEnabled(const std::string& ifaceId,
1341 const std::string& propertyName, const bool v4Value,
1342 const bool v6Value,
1343 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001344{
1345 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
Ed Tanousd02aad32024-02-13 14:43:34 -08001346 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301347 asyncResp, "DHCPv4", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001348 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1349 ifaceId,
Ginu Georgee93abac2024-06-14 17:35:27 +05301350 "xyz.openbmc_project.Network.EthernetInterface", propertyName, dhcp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001351}
1352
Jishnu CMe4588152023-05-11 00:04:40 -05001353enum class NetworkType
1354{
1355 dhcp4,
1356 dhcp6
1357};
1358
1359inline void setDHCPConfig(const std::string& propertyName, const bool& value,
1360 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1361 const std::string& ethifaceId, NetworkType type)
Ed Tanousbf648f72021-06-03 15:00:14 -07001362{
Ed Tanous62598e32023-07-17 17:06:25 -07001363 BMCWEB_LOG_DEBUG("{} = {}", propertyName, value);
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001364 std::string redfishPropertyName;
Jishnu CMe4588152023-05-11 00:04:40 -05001365 sdbusplus::message::object_path path("/xyz/openbmc_project/network/");
1366 path /= ethifaceId;
1367
1368 if (type == NetworkType::dhcp4)
1369 {
1370 path /= "dhcp4";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001371 redfishPropertyName = "DHCPv4";
Jishnu CMe4588152023-05-11 00:04:40 -05001372 }
1373 else
1374 {
1375 path /= "dhcp6";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001376 redfishPropertyName = "DHCPv6";
Jishnu CMe4588152023-05-11 00:04:40 -05001377 }
1378
Ginu Georgee93abac2024-06-14 17:35:27 +05301379 setDbusProperty(
1380 asyncResp, redfishPropertyName, "xyz.openbmc_project.Network", path,
1381 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, value);
Ed Tanousbf648f72021-06-03 15:00:14 -07001382}
1383
Ravi Tejab10d8db2022-05-24 09:04:12 -05001384inline 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;
Ginu Georgee93abac2024-06-14 17:35:27 +05301390 setDbusProperty(asyncResp,
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001391 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
Ginu Georgee93abac2024-06-14 17:35:27 +05301392 "xyz.openbmc_project.Network", path,
1393 "xyz.openbmc_project.Network.EthernetInterface",
1394 "IPv6AcceptRA", ipv6AutoConfigEnabled);
Ravi Tejab10d8db2022-05-24 09:04:12 -05001395}
1396
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001397inline void handleDHCPPatch(
1398 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1399 const DHCPParameters& v4dhcpParms, const DHCPParameters& v6dhcpParms,
1400 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001401{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001402 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1403 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001404
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001405 if (ipv4Active)
1406 {
1407 updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1408 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001409 bool nextv4DHCPState =
1410 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1411
1412 bool nextv6DHCPState{};
1413 if (v6dhcpParms.dhcpv6OperatingMode)
1414 {
Ravi Tejab10d8db2022-05-24 09:04:12 -05001415 if ((*v6dhcpParms.dhcpv6OperatingMode != "Enabled") &&
Ed Tanousbf648f72021-06-03 15:00:14 -07001416 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1417 {
1418 messages::propertyValueFormatError(asyncResp->res,
1419 *v6dhcpParms.dhcpv6OperatingMode,
1420 "OperatingMode");
1421 return;
1422 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05001423 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Enabled");
Ed Tanousbf648f72021-06-03 15:00:14 -07001424 }
1425 else
1426 {
1427 nextv6DHCPState = ipv6Active;
1428 }
1429
Jishnu CMe4588152023-05-11 00:04:40 -05001430 bool nextDNSv4 = ethData.dnsv4Enabled;
1431 bool nextDNSv6 = ethData.dnsv6Enabled;
1432 if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001433 {
Jishnu CMe4588152023-05-11 00:04:40 -05001434 nextDNSv4 = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001435 }
Jishnu CMe4588152023-05-11 00:04:40 -05001436 if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001437 {
Jishnu CMe4588152023-05-11 00:04:40 -05001438 nextDNSv6 = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001439 }
1440
Jishnu CMe4588152023-05-11 00:04:40 -05001441 bool nextNTPv4 = ethData.ntpv4Enabled;
1442 bool nextNTPv6 = ethData.ntpv6Enabled;
1443 if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001444 {
Jishnu CMe4588152023-05-11 00:04:40 -05001445 nextNTPv4 = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001446 }
Jishnu CMe4588152023-05-11 00:04:40 -05001447 if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001448 {
Jishnu CMe4588152023-05-11 00:04:40 -05001449 nextNTPv6 = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001450 }
1451
Ravi Teja91c441e2024-02-23 09:03:43 -06001452 bool nextUsev4Domain = ethData.domainv4Enabled;
1453 bool nextUsev6Domain = ethData.domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -05001454 if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001455 {
Jishnu CMe4588152023-05-11 00:04:40 -05001456 nextUsev4Domain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001457 }
Jishnu CMe4588152023-05-11 00:04:40 -05001458 if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001459 {
Jishnu CMe4588152023-05-11 00:04:40 -05001460 nextUsev6Domain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001461 }
1462
Ed Tanous62598e32023-07-17 17:06:25 -07001463 BMCWEB_LOG_DEBUG("set DHCPEnabled...");
Ed Tanousbf648f72021-06-03 15:00:14 -07001464 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1465 asyncResp);
Ed Tanous62598e32023-07-17 17:06:25 -07001466 BMCWEB_LOG_DEBUG("set DNSEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001467 setDHCPConfig("DNSEnabled", nextDNSv4, asyncResp, ifaceId,
1468 NetworkType::dhcp4);
Ed Tanous62598e32023-07-17 17:06:25 -07001469 BMCWEB_LOG_DEBUG("set NTPEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001470 setDHCPConfig("NTPEnabled", nextNTPv4, asyncResp, ifaceId,
1471 NetworkType::dhcp4);
Ravi Teja91c441e2024-02-23 09:03:43 -06001472 BMCWEB_LOG_DEBUG("set DomainEnabled...");
1473 setDHCPConfig("DomainEnabled", nextUsev4Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001474 NetworkType::dhcp4);
1475 BMCWEB_LOG_DEBUG("set DNSEnabled for dhcp6...");
1476 setDHCPConfig("DNSEnabled", nextDNSv6, asyncResp, ifaceId,
1477 NetworkType::dhcp6);
1478 BMCWEB_LOG_DEBUG("set NTPEnabled for dhcp6...");
1479 setDHCPConfig("NTPEnabled", nextNTPv6, asyncResp, ifaceId,
1480 NetworkType::dhcp6);
Ravi Teja91c441e2024-02-23 09:03:43 -06001481 BMCWEB_LOG_DEBUG("set DomainEnabled for dhcp6...");
1482 setDHCPConfig("DomainEnabled", nextUsev6Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001483 NetworkType::dhcp6);
Ed Tanousbf648f72021-06-03 15:00:14 -07001484}
1485
Ed Tanous77179532023-02-28 10:45:28 -08001486inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1487 const std::vector<IPv4AddressData>::const_iterator& head,
1488 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001489{
1490 return std::find_if(head, end, [](const IPv4AddressData& value) {
1491 return value.origin == "Static";
1492 });
1493}
1494
Ed Tanous77179532023-02-28 10:45:28 -08001495inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1496 const std::vector<IPv6AddressData>::const_iterator& head,
1497 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001498{
1499 return std::find_if(head, end, [](const IPv6AddressData& value) {
1500 return value.origin == "Static";
1501 });
1502}
1503
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001504enum class AddrChange
Ed Tanousbf648f72021-06-03 15:00:14 -07001505{
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001506 Noop,
1507 Delete,
1508 Update,
1509};
1510
1511// Struct representing a dbus change
1512struct AddressPatch
1513{
1514 std::string address;
1515 std::string gateway;
1516 uint8_t prefixLength = 0;
1517 std::string existingDbusId;
1518 AddrChange operation = AddrChange::Noop;
1519};
1520
1521inline bool parseAddresses(
1522 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1523 const std::vector<IPv4AddressData>& ipv4Data, crow::Response& res,
1524 std::vector<AddressPatch>& addressesOut, std::string& gatewayOut)
1525{
Ed Tanous77179532023-02-28 10:45:28 -08001526 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001527 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1528
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001529 std::string lastGatewayPath;
1530 size_t entryIdx = 0;
Ed Tanous3dfed532024-03-06 14:41:27 -08001531 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1532 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001533 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001534 std::string pathString =
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001535 std::format("IPv4StaticAddresses/{}", entryIdx);
1536 AddressPatch& thisAddress = addressesOut.emplace_back();
Ed Tanous3dfed532024-03-06 14:41:27 -08001537 nlohmann::json::object_t* obj =
1538 std::get_if<nlohmann::json::object_t>(&thisJson);
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001539 if (nicIpEntry != ipv4Data.cend())
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001540 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001541 thisAddress.existingDbusId = nicIpEntry->id;
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001542 }
1543
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001544 if (obj == nullptr)
1545 {
1546 if (thisAddress.existingDbusId.empty())
1547 {
1548 // Received a DELETE action on an entry not assigned to the NIC
1549 messages::resourceCannotBeDeleted(res);
1550 return false;
1551 }
1552 thisAddress.operation = AddrChange::Delete;
1553 }
1554 else
Ed Tanousbf648f72021-06-03 15:00:14 -07001555 {
1556 std::optional<std::string> address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001557 std::optional<std::string> gateway;
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001558 std::optional<std::string> subnetMask;
1559 if (!obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001560 {
Patrick Williams504af5a2025-02-03 14:29:03 -05001561 if (!json_util::readJsonObject( //
1562 *obj, res, //
1563 "Address", address, //
1564 "Gateway", gateway, //
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001565 "SubnetMask", subnetMask //
1566 ))
1567 {
1568 messages::propertyValueFormatError(res, *obj, pathString);
1569 return false;
1570 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001571 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001572 // Find the address/subnet/gateway values. Any values that are
1573 // not explicitly provided are assumed to be unmodified from the
1574 // current state of the interface. Merge existing state into the
1575 // current request.
Ed Tanousbf648f72021-06-03 15:00:14 -07001576 if (address)
1577 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001578 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001579 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001580 messages::propertyValueFormatError(res, *address,
Ed Tanousbf648f72021-06-03 15:00:14 -07001581 pathString + "/Address");
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001582 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001583 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001584 thisAddress.operation = AddrChange::Update;
1585 thisAddress.address = *address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001586 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001587 else if (thisAddress.existingDbusId.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001588 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001589 messages::propertyMissing(res, pathString + "/Address");
1590 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001591 }
1592 else
1593 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001594 thisAddress.address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001595 }
1596
1597 if (subnetMask)
1598 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001599 uint8_t prefixLength = 0;
Ed Tanous033f1e42022-08-15 09:47:37 -07001600 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1601 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001602 {
1603 messages::propertyValueFormatError(
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001604 res, *subnetMask, pathString + "/SubnetMask");
1605 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001606 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001607 thisAddress.prefixLength = prefixLength;
1608 thisAddress.operation = AddrChange::Update;
Ed Tanousbf648f72021-06-03 15:00:14 -07001609 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001610 else if (thisAddress.existingDbusId.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001611 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001612 messages::propertyMissing(res, pathString + "/SubnetMask");
1613 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001614 }
1615 else
1616 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001617 uint8_t prefixLength = 0;
1618 // Ignore return code. It came from internal, it's it's invalid
1619 // nothing we can do
1620 ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1621 &prefixLength);
Ed Tanousbf648f72021-06-03 15:00:14 -07001622
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001623 thisAddress.prefixLength = prefixLength;
1624 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001625 if (gateway)
1626 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001627 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001628 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001629 messages::propertyValueFormatError(res, *gateway,
Ed Tanousbf648f72021-06-03 15:00:14 -07001630 pathString + "/Gateway");
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001631 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001632 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001633 thisAddress.operation = AddrChange::Update;
1634 thisAddress.gateway = *gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001635 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001636 else if (thisAddress.existingDbusId.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001637 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001638 // Default to null gateway
1639 gateway = "";
Ed Tanousbf648f72021-06-03 15:00:14 -07001640 }
1641 else
1642 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001643 thisAddress.gateway = nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001644 }
1645
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001646 // Changing gateway from existing
1647 if (!thisAddress.gateway.empty() &&
1648 thisAddress.gateway != "0.0.0.0")
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001649 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001650 if (!gatewayOut.empty() && gatewayOut != thisAddress.gateway)
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001651 {
1652 // A NIC can only have a single active gateway value.
1653 // If any gateway in the array of static addresses
1654 // mismatch the PATCH is in error.
1655 std::string arg1 = pathString + "/Gateway";
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001656 std::string arg2 = lastGatewayPath + "/Gateway";
1657 messages::propertyValueConflict(res, arg1, arg2);
1658 return false;
1659 }
1660 gatewayOut = thisAddress.gateway;
1661 lastGatewayPath = pathString;
1662 }
1663 }
1664 nicIpEntry++;
1665 nicIpEntry = getNextStaticIpEntry(nicIpEntry, ipv4Data.cend());
1666 entryIdx++;
1667 }
1668
1669 // Delete the remaining IPs
1670 while (nicIpEntry != ipv4Data.cend())
1671 {
1672 AddressPatch& thisAddress = addressesOut.emplace_back();
1673 thisAddress.operation = AddrChange::Delete;
1674 thisAddress.existingDbusId = nicIpEntry->id;
1675 nicIpEntry++;
1676 nicIpEntry = getNextStaticIpEntry(nicIpEntry, ipv4Data.cend());
1677 }
1678
1679 return true;
1680}
1681
1682inline void handleIPv4StaticPatch(
1683 const std::string& ifaceId,
1684 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1685 const EthernetInterfaceData& ethData,
1686 const std::vector<IPv4AddressData>& ipv4Data,
1687 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1688{
1689 std::vector<AddressPatch> addresses;
1690 std::string gatewayOut;
1691 if (!parseAddresses(input, ipv4Data, asyncResp->res, addresses, gatewayOut))
1692 {
1693 return;
1694 }
1695
1696 // If we're setting the gateway to something new, delete the
1697 // existing so we won't conflict
1698 if (!ethData.defaultGateway.empty() && ethData.defaultGateway != gatewayOut)
1699 {
1700 updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1701 }
1702
1703 for (const AddressPatch& address : addresses)
1704 {
1705 switch (address.operation)
1706 {
1707 case AddrChange::Delete:
1708 {
1709 BMCWEB_LOG_ERROR("Deleting id {} on interface {}",
1710 address.existingDbusId, ifaceId);
1711 deleteIPAddress(ifaceId, address.existingDbusId, asyncResp);
1712 }
1713 break;
1714 case AddrChange::Update:
1715 {
1716 // Update is a delete then a recreate
1717 // Only need to update if there is an existing ip at this index
1718 if (!address.existingDbusId.empty())
1719 {
1720 BMCWEB_LOG_ERROR("Deleting id {} on interface {}",
1721 address.existingDbusId, ifaceId);
1722 deleteAndCreateIPAddress(
1723 IpVersion::IpV4, ifaceId, address.existingDbusId,
1724 address.prefixLength, address.address, address.gateway,
1725 asyncResp);
1726 }
1727 else
1728 {
1729 // Otherwise, just create a new one
1730 BMCWEB_LOG_ERROR(
1731 "creating ip {} prefix {} gateway {} on interface {}",
1732 address.address, address.prefixLength, address.gateway,
1733 ifaceId);
1734 createIPv4(ifaceId, address.prefixLength, address.gateway,
1735 address.address, asyncResp);
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001736 }
1737 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001738 break;
1739 default:
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001740 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001741 // Leave alone
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001742 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001743 break;
1744 }
1745 }
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001746
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001747 // now update to the new gateway.
1748 // Default gateway is already empty, so no need to update if we're clearing
1749 if (!gatewayOut.empty() && ethData.defaultGateway != gatewayOut)
1750 {
1751 updateIPv4DefaultGateway(ifaceId, gatewayOut, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001752 }
1753}
1754
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001755inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001756 const std::string& ifaceId,
1757 const std::vector<std::string>& updatedStaticNameServers,
1758 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1759{
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001760 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301761 asyncResp, "StaticNameServers", "xyz.openbmc_project.Network",
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001762 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1763 ifaceId,
George Liu9ae226f2023-06-21 17:56:46 +08001764 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ginu Georgee93abac2024-06-14 17:35:27 +05301765 updatedStaticNameServers);
Ed Tanousbf648f72021-06-03 15:00:14 -07001766}
1767
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001768inline void handleIPv6StaticAddressesPatch(
Ed Tanous3dfed532024-03-06 14:41:27 -08001769 const std::string& ifaceId,
1770 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Ed Tanous77179532023-02-28 10:45:28 -08001771 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001772 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1773{
Ed Tanousbf648f72021-06-03 15:00:14 -07001774 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001775 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001776 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanous3dfed532024-03-06 14:41:27 -08001777 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1778 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001779 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001780 std::string pathString =
1781 "IPv6StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001782 nlohmann::json::object_t* obj =
1783 std::get_if<nlohmann::json::object_t>(&thisJson);
1784 if (obj != nullptr && !obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001785 {
1786 std::optional<std::string> address;
1787 std::optional<uint8_t> prefixLength;
Ed Tanous3dfed532024-03-06 14:41:27 -08001788 nlohmann::json::object_t thisJsonCopy = *obj;
Patrick Williams504af5a2025-02-03 14:29:03 -05001789 if (!json_util::readJsonObject( //
Myung Baeafc474a2024-10-09 00:53:29 -07001790 thisJsonCopy, asyncResp->res, //
Patrick Williams504af5a2025-02-03 14:29:03 -05001791 "Address", address, //
1792 "PrefixLength", prefixLength //
Myung Baeafc474a2024-10-09 00:53:29 -07001793 ))
Ed Tanousbf648f72021-06-03 15:00:14 -07001794 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001795 messages::propertyValueFormatError(asyncResp->res, thisJsonCopy,
Ed Tanousf818b042022-06-27 13:17:35 -07001796 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001797 return;
1798 }
1799
Ed Tanousbf648f72021-06-03 15:00:14 -07001800 // Find the address and prefixLength values. Any values that are
1801 // not explicitly provided are assumed to be unmodified from the
1802 // current state of the interface. Merge existing state into the
1803 // current request.
Ed Tanousd547d8d2024-03-16 18:04:41 -07001804 if (!address)
Ed Tanousbf648f72021-06-03 15:00:14 -07001805 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001806 if (nicIpEntry == ipv6Data.end())
1807 {
1808 messages::propertyMissing(asyncResp->res,
1809 pathString + "/Address");
1810 return;
1811 }
1812 address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001813 }
1814
Ed Tanousd547d8d2024-03-16 18:04:41 -07001815 if (!prefixLength)
Ed Tanousbf648f72021-06-03 15:00:14 -07001816 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001817 if (nicIpEntry == ipv6Data.end())
1818 {
1819 messages::propertyMissing(asyncResp->res,
1820 pathString + "/PrefixLength");
1821 return;
1822 }
1823 prefixLength = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001824 }
1825
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001826 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001827 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001828 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
Ed Tanousd547d8d2024-03-16 18:04:41 -07001829 nicIpEntry->id, *prefixLength,
1830 *address, "", asyncResp);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001831 nicIpEntry =
1832 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001833 }
1834 else
1835 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001836 createIPv6(ifaceId, *prefixLength, *address, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001837 }
1838 entryIdx++;
1839 }
1840 else
1841 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001842 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001843 {
1844 // Requesting a DELETE/DO NOT MODIFY action for an item
1845 // that isn't present on the eth(n) interface. Input JSON is
1846 // in error, so bail out.
Ed Tanous3dfed532024-03-06 14:41:27 -08001847 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001848 {
1849 messages::resourceCannotBeDeleted(asyncResp->res);
1850 return;
1851 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001852 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001853 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001854 return;
1855 }
1856
Ed Tanous3dfed532024-03-06 14:41:27 -08001857 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001858 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001859 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001860 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001861 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001862 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001863 nicIpEntry =
1864 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001865 }
1866 entryIdx++;
1867 }
1868 }
1869}
1870
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001871inline std::string extractParentInterfaceName(const std::string& ifaceId)
1872{
1873 std::size_t pos = ifaceId.find('_');
1874 return ifaceId.substr(0, pos);
1875}
1876
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001877inline void parseInterfaceData(
1878 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1879 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1880 const std::vector<IPv4AddressData>& ipv4Data,
1881 const std::vector<IPv6AddressData>& ipv6Data,
1882 const std::vector<StaticGatewayData>& ipv6GatewayData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001883{
Ed Tanousbf648f72021-06-03 15:00:14 -07001884 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1885 jsonResponse["Id"] = ifaceId;
Ed Tanous253f11b2024-05-16 09:38:31 -07001886 jsonResponse["@odata.id"] =
1887 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
1888 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001889 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1890
Ed Tanousbf648f72021-06-03 15:00:14 -07001891 if (ethData.nicEnabled)
1892 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001893 jsonResponse["LinkStatus"] =
1894 ethData.linkUp ? ethernet_interface::LinkStatus::LinkUp
1895 : ethernet_interface::LinkStatus::LinkDown;
1896 jsonResponse["Status"]["State"] = resource::State::Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001897 }
1898 else
1899 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001900 jsonResponse["LinkStatus"] = ethernet_interface::LinkStatus::NoLink;
1901 jsonResponse["Status"]["State"] = resource::State::Disabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001902 }
1903
Ed Tanousbf648f72021-06-03 15:00:14 -07001904 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301905 jsonResponse["MTUSize"] = ethData.mtuSize;
Asmitha Karunanithi4652c642024-07-30 11:35:53 -05001906 if (ethData.macAddress)
1907 {
1908 jsonResponse["MACAddress"] = *ethData.macAddress;
1909 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001910 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001911 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
Jishnu CMe4588152023-05-11 00:04:40 -05001912 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1913 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
Ravi Tejade9ad762024-06-03 02:00:15 -05001914 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.domainv4Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001915 jsonResponse["DHCPv6"]["OperatingMode"] =
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001916 translateDhcpEnabledToBool(ethData.dhcpEnabled, false)
1917 ? "Enabled"
1918 : "Disabled";
Jishnu CMe4588152023-05-11 00:04:40 -05001919 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1920 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
Ravi Tejade9ad762024-06-03 02:00:15 -05001921 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.domainv6Enabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -05001922 jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1923 ethData.ipv6AcceptRa;
Ed Tanousbf648f72021-06-03 15:00:14 -07001924
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001925 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001926 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001927 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001928
1929 // When domain name is empty then it means, that it is a network
1930 // without domain names, and the host name itself must be treated as
1931 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001932 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001933 if (!ethData.domainnames.empty())
1934 {
1935 fqdn += "." + ethData.domainnames[0];
1936 }
1937 jsonResponse["FQDN"] = fqdn;
1938 }
1939
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001940 if (ethData.vlanId)
1941 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001942 jsonResponse["EthernetInterfaceType"] =
1943 ethernet_interface::EthernetDeviceType::Virtual;
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001944 jsonResponse["VLAN"]["VLANEnable"] = true;
1945 jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1946 jsonResponse["VLAN"]["Tagged"] = true;
1947
1948 nlohmann::json::array_t relatedInterfaces;
1949 nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1950 parentInterface["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001951 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1952 BMCWEB_REDFISH_MANAGER_URI_NAME,
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001953 extractParentInterfaceName(ifaceId));
1954 jsonResponse["Links"]["RelatedInterfaces"] =
1955 std::move(relatedInterfaces);
1956 }
1957 else
1958 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001959 jsonResponse["EthernetInterfaceType"] =
1960 ethernet_interface::EthernetDeviceType::Physical;
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001961 }
1962
Ed Tanousbf648f72021-06-03 15:00:14 -07001963 jsonResponse["NameServers"] = ethData.nameServers;
1964 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1965
1966 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1967 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1968 ipv4Array = nlohmann::json::array();
1969 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001970 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001971 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001972 std::string gatewayStr = ipv4Config.gateway;
1973 if (gatewayStr.empty())
1974 {
1975 gatewayStr = "0.0.0.0";
1976 }
Ed Tanous14766872022-03-15 10:44:42 -07001977 nlohmann::json::object_t ipv4;
1978 ipv4["AddressOrigin"] = ipv4Config.origin;
1979 ipv4["SubnetMask"] = ipv4Config.netmask;
1980 ipv4["Address"] = ipv4Config.address;
1981 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001982
Ed Tanousbf648f72021-06-03 15:00:14 -07001983 if (ipv4Config.origin == "Static")
1984 {
Ed Tanous14766872022-03-15 10:44:42 -07001985 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001986 }
Ed Tanous14766872022-03-15 10:44:42 -07001987
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001988 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001989 }
1990
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001991 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001992 if (ipv6GatewayStr.empty())
1993 {
1994 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1995 }
1996
1997 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1998
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001999 nlohmann::json::array_t ipv6StaticGatewayArray;
2000 for (const auto& ipv6GatewayConfig : ipv6GatewayData)
2001 {
2002 nlohmann::json::object_t ipv6Gateway;
2003 ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002004 ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
2005 }
2006 jsonResponse["IPv6StaticDefaultGateways"] =
2007 std::move(ipv6StaticGatewayArray);
2008
Ed Tanousbf648f72021-06-03 15:00:14 -07002009 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
2010 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
2011 ipv6Array = nlohmann::json::array();
2012 ipv6StaticArray = nlohmann::json::array();
2013 nlohmann::json& ipv6AddrPolicyTable =
2014 jsonResponse["IPv6AddressPolicyTable"];
2015 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08002016 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07002017 {
Ed Tanous14766872022-03-15 10:44:42 -07002018 nlohmann::json::object_t ipv6;
2019 ipv6["Address"] = ipv6Config.address;
2020 ipv6["PrefixLength"] = ipv6Config.prefixLength;
2021 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05002022
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002023 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07002024 if (ipv6Config.origin == "Static")
2025 {
Ed Tanous14766872022-03-15 10:44:42 -07002026 nlohmann::json::object_t ipv6Static;
2027 ipv6Static["Address"] = ipv6Config.address;
2028 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002029 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07002030 }
2031 }
2032}
2033
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002034inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2035 const std::string& ifaceId,
2036 const boost::system::error_code& ec,
2037 const sdbusplus::message_t& m)
2038{
2039 if (!ec)
2040 {
2041 return;
2042 }
2043 const sd_bus_error* dbusError = m.get_error();
2044 if (dbusError == nullptr)
2045 {
2046 messages::internalError(asyncResp->res);
2047 return;
2048 }
Ed Tanous62598e32023-07-17 17:06:25 -07002049 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002050
2051 if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
2052 dbusError->name)
2053 {
2054 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2055 ifaceId);
2056 return;
2057 }
2058 if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
2059 dbusError->name)
2060 {
2061 messages::resourceCannotBeDeleted(asyncResp->res);
2062 return;
2063 }
2064 messages::internalError(asyncResp->res);
2065}
2066
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002067inline void afterVlanCreate(
2068 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2069 const std::string& parentInterfaceUri, const std::string& vlanInterface,
2070 const boost::system::error_code& ec, const sdbusplus::message_t& m
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002071
2072)
2073{
2074 if (ec)
2075 {
2076 const sd_bus_error* dbusError = m.get_error();
2077 if (dbusError == nullptr)
2078 {
2079 messages::internalError(asyncResp->res);
2080 return;
2081 }
Ed Tanous62598e32023-07-17 17:06:25 -07002082 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002083
2084 if (std::string_view(
2085 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
2086 dbusError->name)
2087 {
2088 messages::propertyValueNotInList(
2089 asyncResp->res, parentInterfaceUri,
2090 "Links/RelatedInterfaces/0/@odata.id");
2091 return;
2092 }
2093 if (std::string_view(
2094 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
2095 dbusError->name)
2096 {
2097 messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
2098 "Id", vlanInterface);
2099 return;
2100 }
2101 messages::internalError(asyncResp->res);
2102 return;
2103 }
2104
Ed Tanous253f11b2024-05-16 09:38:31 -07002105 const boost::urls::url vlanInterfaceUri =
2106 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2107 BMCWEB_REDFISH_MANAGER_URI_NAME, vlanInterface);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002108 asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
2109}
2110
Ed Tanousbf648f72021-06-03 15:00:14 -07002111inline void requestEthernetInterfacesRoutes(App& app)
2112{
Ed Tanous253f11b2024-05-16 09:38:31 -07002113 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07002114 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002115 .methods(boost::beast::http::verb::get)(
2116 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002117 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2118 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002119 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2120 {
2121 return;
2122 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002123
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002124 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2125 {
2126 messages::resourceNotFound(asyncResp->res, "Manager",
2127 managerId);
2128 return;
2129 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002130
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002131 asyncResp->res.jsonValue["@odata.type"] =
2132 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2133 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2134 "/redfish/v1/Managers/{}/EthernetInterfaces",
2135 BMCWEB_REDFISH_MANAGER_URI_NAME);
2136 asyncResp->res.jsonValue["Name"] =
2137 "Ethernet Network Interface Collection";
2138 asyncResp->res.jsonValue["Description"] =
2139 "Collection of EthernetInterfaces for this Manager";
2140
2141 // Get eth interface list, and call the below callback for JSON
2142 // preparation
2143 getEthernetIfaceList(
2144 [asyncResp](const bool& success,
2145 const std::vector<std::string>& ifaceList) {
2146 if (!success)
2147 {
2148 messages::internalError(asyncResp->res);
2149 return;
2150 }
2151
2152 nlohmann::json& ifaceArray =
2153 asyncResp->res.jsonValue["Members"];
2154 ifaceArray = nlohmann::json::array();
2155 for (const std::string& ifaceItem : ifaceList)
2156 {
2157 nlohmann::json::object_t iface;
2158 iface["@odata.id"] = boost::urls::format(
2159 "/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2160 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceItem);
2161 ifaceArray.push_back(std::move(iface));
2162 }
2163
2164 asyncResp->res.jsonValue["Members@odata.count"] =
2165 ifaceArray.size();
2166 asyncResp->res.jsonValue["@odata.id"] =
2167 boost::urls::format(
2168 "/redfish/v1/Managers/{}/EthernetInterfaces",
Ed Tanous253f11b2024-05-16 09:38:31 -07002169 BMCWEB_REDFISH_MANAGER_URI_NAME);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002170 });
2171 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002172
Ed Tanous253f11b2024-05-16 09:38:31 -07002173 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002174 .privileges(redfish::privileges::postEthernetInterfaceCollection)
2175 .methods(boost::beast::http::verb::post)(
2176 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002177 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2178 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002179 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2180 {
2181 return;
2182 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002183
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002184 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2185 {
2186 messages::resourceNotFound(asyncResp->res, "Manager",
2187 managerId);
2188 return;
2189 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002190
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002191 bool vlanEnable = false;
2192 uint32_t vlanId = 0;
2193 std::vector<nlohmann::json::object_t> relatedInterfaces;
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002194
Patrick Williams504af5a2025-02-03 14:29:03 -05002195 if (!json_util::readJsonPatch( //
2196 req, asyncResp->res, //
Myung Baeafc474a2024-10-09 00:53:29 -07002197 "Links/RelatedInterfaces", relatedInterfaces, //
Patrick Williams504af5a2025-02-03 14:29:03 -05002198 "VLAN/VLANEnable", vlanEnable, //
2199 "VLAN/VLANId", vlanId //
Myung Baeafc474a2024-10-09 00:53:29 -07002200 ))
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002201 {
2202 return;
2203 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002204
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002205 if (relatedInterfaces.size() != 1)
2206 {
2207 messages::arraySizeTooLong(asyncResp->res,
2208 "Links/RelatedInterfaces",
2209 relatedInterfaces.size());
2210 return;
2211 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002212
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002213 std::string parentInterfaceUri;
2214 if (!json_util::readJsonObject(relatedInterfaces[0],
2215 asyncResp->res, "@odata.id",
2216 parentInterfaceUri))
2217 {
2218 messages::propertyMissing(
2219 asyncResp->res, "Links/RelatedInterfaces/0/@odata.id");
2220 return;
2221 }
2222 BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002223
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002224 boost::system::result<boost::urls::url_view> parsedUri =
2225 boost::urls::parse_relative_ref(parentInterfaceUri);
2226 if (!parsedUri)
2227 {
2228 messages::propertyValueFormatError(
2229 asyncResp->res, parentInterfaceUri,
2230 "Links/RelatedInterfaces/0/@odata.id");
2231 return;
2232 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002233
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002234 std::string parentInterface;
2235 if (!crow::utility::readUrlSegments(
2236 *parsedUri, "redfish", "v1", "Managers", "bmc",
2237 "EthernetInterfaces", std::ref(parentInterface)))
2238 {
2239 messages::propertyValueNotInList(
2240 asyncResp->res, parentInterfaceUri,
2241 "Links/RelatedInterfaces/0/@odata.id");
2242 return;
2243 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002244
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002245 if (!vlanEnable)
2246 {
2247 // In OpenBMC implementation, VLANEnable cannot be false on
2248 // create
2249 messages::propertyValueIncorrect(
2250 asyncResp->res, "VLAN/VLANEnable", "false");
2251 return;
2252 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002253
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002254 std::string vlanInterface =
2255 parentInterface + "_" + std::to_string(vlanId);
Ed Tanous177612a2025-02-14 15:16:09 -08002256 dbus::utility::async_method_call(
2257 asyncResp,
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002258 [asyncResp, parentInterfaceUri,
2259 vlanInterface](const boost::system::error_code& ec,
2260 const sdbusplus::message_t& m) {
2261 afterVlanCreate(asyncResp, parentInterfaceUri,
2262 vlanInterface, ec, m);
2263 },
2264 "xyz.openbmc_project.Network",
2265 "/xyz/openbmc_project/network",
2266 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2267 parentInterface, vlanId);
2268 });
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002269
Ed Tanous253f11b2024-05-16 09:38:31 -07002270 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002271 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002272 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002273 [&app](const crow::Request& req,
2274 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002275 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002276 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2277 {
2278 return;
2279 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002280
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002281 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2282 {
2283 messages::resourceNotFound(asyncResp->res, "Manager",
2284 managerId);
2285 return;
2286 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002287
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002288 getEthernetIfaceData(
2289 ifaceId,
2290 [asyncResp, ifaceId](
2291 const bool& success,
2292 const EthernetInterfaceData& ethData,
2293 const std::vector<IPv4AddressData>& ipv4Data,
2294 const std::vector<IPv6AddressData>& ipv6Data,
2295 const std::vector<StaticGatewayData>& ipv6GatewayData) {
2296 if (!success)
2297 {
2298 // TODO(Pawel)consider distinguish between non
2299 // existing object, and other errors
2300 messages::resourceNotFound(
2301 asyncResp->res, "EthernetInterface", ifaceId);
2302 return;
2303 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002304
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002305 asyncResp->res.jsonValue["@odata.type"] =
2306 "#EthernetInterface.v1_9_0.EthernetInterface";
2307 asyncResp->res.jsonValue["Name"] =
2308 "Manager Ethernet Interface";
2309 asyncResp->res.jsonValue["Description"] =
2310 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05302311
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002312 parseInterfaceData(asyncResp, ifaceId, ethData,
2313 ipv4Data, ipv6Data, ipv6GatewayData);
2314 });
2315 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002316
Ed Tanous253f11b2024-05-16 09:38:31 -07002317 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002318 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002319 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002320 [&app](const crow::Request& req,
2321 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002322 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002323 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2324 {
2325 return;
2326 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002327
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002328 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2329 {
2330 messages::resourceNotFound(asyncResp->res, "Manager",
2331 managerId);
2332 return;
2333 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002334
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002335 std::optional<std::string> hostname;
2336 std::optional<std::string> fqdn;
2337 std::optional<std::string> macAddress;
2338 std::optional<std::string> ipv6DefaultGateway;
2339 std::optional<std::vector<
2340 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2341 ipv4StaticAddresses;
2342 std::optional<std::vector<
2343 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2344 ipv6StaticAddresses;
2345 std::optional<std::vector<
2346 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2347 ipv6StaticDefaultGateways;
2348 std::optional<std::vector<std::string>> staticNameServers;
2349 std::optional<bool> ipv6AutoConfigEnabled;
2350 std::optional<bool> interfaceEnabled;
2351 std::optional<size_t> mtuSize;
2352 DHCPParameters v4dhcpParms;
2353 DHCPParameters v6dhcpParms;
Myung Baeafc474a2024-10-09 00:53:29 -07002354
Patrick Williams504af5a2025-02-03 14:29:03 -05002355 if (!json_util::readJsonPatch( //
2356 req, asyncResp->res, //
2357 "DHCPv4/DHCPEnabled", v4dhcpParms.dhcpv4Enabled, //
Myung Baeafc474a2024-10-09 00:53:29 -07002358 "DHCPv4/UseDNSServers", v4dhcpParms.useDnsServers, //
2359 "DHCPv4/UseDomainName", v4dhcpParms.useDomainName, //
2360 "DHCPv4/UseNTPServers", v4dhcpParms.useNtpServers, //
2361 "DHCPv6/OperatingMode",
Patrick Williams504af5a2025-02-03 14:29:03 -05002362 v6dhcpParms.dhcpv6OperatingMode, //
Myung Baeafc474a2024-10-09 00:53:29 -07002363 "DHCPv6/UseDNSServers", v6dhcpParms.useDnsServers, //
2364 "DHCPv6/UseDomainName", v6dhcpParms.useDomainName, //
2365 "DHCPv6/UseNTPServers", v6dhcpParms.useNtpServers, //
Patrick Williams504af5a2025-02-03 14:29:03 -05002366 "FQDN", fqdn, //
2367 "HostName", hostname, //
2368 "InterfaceEnabled", interfaceEnabled, //
2369 "IPv4StaticAddresses", ipv4StaticAddresses, //
2370 "IPv6DefaultGateway", ipv6DefaultGateway, //
2371 "IPv6StaticAddresses", ipv6StaticAddresses, //
Myung Baeafc474a2024-10-09 00:53:29 -07002372 "IPv6StaticDefaultGateways",
Patrick Williams504af5a2025-02-03 14:29:03 -05002373 ipv6StaticDefaultGateways, //
2374 "InterfaceEnabled", interfaceEnabled, //
2375 "MACAddress", macAddress, //
2376 "MTUSize", mtuSize, //
Myung Baeafc474a2024-10-09 00:53:29 -07002377 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
Patrick Williams504af5a2025-02-03 14:29:03 -05002378 ipv6AutoConfigEnabled, //
2379 "StaticNameServers", staticNameServers //
Myung Baeafc474a2024-10-09 00:53:29 -07002380 ))
2381 {
2382 return;
2383 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002384
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002385 // Get single eth interface data, and call the below callback
2386 // for JSON preparation
2387 getEthernetIfaceData(
2388 ifaceId,
2389 [asyncResp, ifaceId, hostname = std::move(hostname),
2390 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2391 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2392 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2393 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
2394 ipv6StaticDefaultGateway =
2395 std::move(ipv6StaticDefaultGateways),
2396 staticNameServers = std::move(staticNameServers), mtuSize,
2397 ipv6AutoConfigEnabled,
2398 v4dhcpParms = std::move(v4dhcpParms),
2399 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2400 const bool success,
2401 const EthernetInterfaceData& ethData,
2402 const std::vector<IPv4AddressData>& ipv4Data,
2403 const std::vector<IPv6AddressData>& ipv6Data,
2404 const std::vector<StaticGatewayData>&
2405 ipv6GatewayData) mutable {
2406 if (!success)
2407 {
2408 // ... otherwise return error
2409 // TODO(Pawel)consider distinguish between non
2410 // existing object, and other errors
2411 messages::resourceNotFound(
2412 asyncResp->res, "EthernetInterface", ifaceId);
2413 return;
2414 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002415
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002416 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2417 v6dhcpParms, asyncResp);
Tejas Patil35fb5312021-09-20 15:35:20 +05302418
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002419 if (hostname)
2420 {
2421 handleHostnamePatch(*hostname, asyncResp);
2422 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002423
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002424 if (ipv6AutoConfigEnabled)
2425 {
2426 handleSLAACAutoConfigPatch(
2427 ifaceId, *ipv6AutoConfigEnabled, asyncResp);
2428 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05002429
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002430 if (fqdn)
2431 {
2432 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2433 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002434
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002435 if (macAddress)
2436 {
2437 handleMACAddressPatch(ifaceId, *macAddress,
2438 asyncResp);
2439 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002440
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002441 if (ipv4StaticAddresses)
2442 {
2443 handleIPv4StaticPatch(ifaceId, *ipv4StaticAddresses,
2444 ethData, ipv4Data, asyncResp);
2445 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002446
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002447 if (staticNameServers)
2448 {
2449 handleStaticNameServersPatch(
2450 ifaceId, *staticNameServers, asyncResp);
2451 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002452
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002453 if (ipv6DefaultGateway)
2454 {
2455 messages::propertyNotWritable(asyncResp->res,
2456 "IPv6DefaultGateway");
2457 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002458
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002459 if (ipv6StaticAddresses)
2460 {
2461 handleIPv6StaticAddressesPatch(ifaceId,
2462 *ipv6StaticAddresses,
2463 ipv6Data, asyncResp);
2464 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002465
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002466 if (ipv6StaticDefaultGateway)
2467 {
2468 handleIPv6DefaultGateway(
2469 ifaceId, *ipv6StaticDefaultGateway,
2470 ipv6GatewayData, asyncResp);
2471 }
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002472
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002473 if (interfaceEnabled)
2474 {
2475 setDbusProperty(
2476 asyncResp, "InterfaceEnabled",
Ginu Georgee93abac2024-06-14 17:35:27 +05302477 "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08002478 sdbusplus::message::object_path(
2479 "/xyz/openbmc_project/network") /
2480 ifaceId,
2481 "xyz.openbmc_project.Network.EthernetInterface",
Ginu Georgee93abac2024-06-14 17:35:27 +05302482 "NICEnabled", *interfaceEnabled);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002483 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002484
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002485 if (mtuSize)
2486 {
2487 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2488 }
2489 });
2490 });
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002491
Ed Tanous253f11b2024-05-16 09:38:31 -07002492 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002493 .privileges(redfish::privileges::deleteEthernetInterface)
2494 .methods(boost::beast::http::verb::delete_)(
2495 [&app](const crow::Request& req,
2496 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002497 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002498 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2499 {
2500 return;
2501 }
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002502
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002503 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2504 {
2505 messages::resourceNotFound(asyncResp->res, "Manager",
2506 managerId);
2507 return;
2508 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002509
Ed Tanous177612a2025-02-14 15:16:09 -08002510 dbus::utility::async_method_call(
2511 asyncResp,
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002512 [asyncResp, ifaceId](const boost::system::error_code& ec,
2513 const sdbusplus::message_t& m) {
2514 afterDelete(asyncResp, ifaceId, ec, m);
2515 },
2516 "xyz.openbmc_project.Network",
2517 std::string("/xyz/openbmc_project/network/") + ifaceId,
2518 "xyz.openbmc_project.Object.Delete", "Delete");
2519 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002520}
2521
Ed Tanous1abe55e2018-09-05 08:30:59 -07002522} // namespace redfish