blob: 53e21c530c482d140662ed99f8099edc20e5990a [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 }
Ed Tanous948c0b12025-07-24 10:49:24 -07001664 if (nicIpEntry != ipv4Data.end())
1665 {
1666 nicIpEntry++;
1667 nicIpEntry = getNextStaticIpEntry(nicIpEntry, ipv4Data.cend());
1668 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001669 entryIdx++;
1670 }
1671
1672 // Delete the remaining IPs
1673 while (nicIpEntry != ipv4Data.cend())
1674 {
1675 AddressPatch& thisAddress = addressesOut.emplace_back();
1676 thisAddress.operation = AddrChange::Delete;
1677 thisAddress.existingDbusId = nicIpEntry->id;
1678 nicIpEntry++;
1679 nicIpEntry = getNextStaticIpEntry(nicIpEntry, ipv4Data.cend());
1680 }
1681
1682 return true;
1683}
1684
1685inline void handleIPv4StaticPatch(
1686 const std::string& ifaceId,
1687 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1688 const EthernetInterfaceData& ethData,
1689 const std::vector<IPv4AddressData>& ipv4Data,
1690 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1691{
1692 std::vector<AddressPatch> addresses;
1693 std::string gatewayOut;
1694 if (!parseAddresses(input, ipv4Data, asyncResp->res, addresses, gatewayOut))
1695 {
1696 return;
1697 }
1698
1699 // If we're setting the gateway to something new, delete the
1700 // existing so we won't conflict
1701 if (!ethData.defaultGateway.empty() && ethData.defaultGateway != gatewayOut)
1702 {
1703 updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1704 }
1705
1706 for (const AddressPatch& address : addresses)
1707 {
1708 switch (address.operation)
1709 {
1710 case AddrChange::Delete:
1711 {
1712 BMCWEB_LOG_ERROR("Deleting id {} on interface {}",
1713 address.existingDbusId, ifaceId);
1714 deleteIPAddress(ifaceId, address.existingDbusId, asyncResp);
1715 }
1716 break;
1717 case AddrChange::Update:
1718 {
1719 // Update is a delete then a recreate
1720 // Only need to update if there is an existing ip at this index
1721 if (!address.existingDbusId.empty())
1722 {
1723 BMCWEB_LOG_ERROR("Deleting id {} on interface {}",
1724 address.existingDbusId, ifaceId);
1725 deleteAndCreateIPAddress(
1726 IpVersion::IpV4, ifaceId, address.existingDbusId,
1727 address.prefixLength, address.address, address.gateway,
1728 asyncResp);
1729 }
1730 else
1731 {
1732 // Otherwise, just create a new one
1733 BMCWEB_LOG_ERROR(
1734 "creating ip {} prefix {} gateway {} on interface {}",
1735 address.address, address.prefixLength, address.gateway,
1736 ifaceId);
1737 createIPv4(ifaceId, address.prefixLength, address.gateway,
1738 address.address, asyncResp);
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001739 }
1740 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001741 break;
1742 default:
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001743 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001744 // Leave alone
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001745 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001746 break;
1747 }
1748 }
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001749
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001750 // now update to the new gateway.
1751 // Default gateway is already empty, so no need to update if we're clearing
1752 if (!gatewayOut.empty() && ethData.defaultGateway != gatewayOut)
1753 {
1754 updateIPv4DefaultGateway(ifaceId, gatewayOut, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001755 }
1756}
1757
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001758inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001759 const std::string& ifaceId,
1760 const std::vector<std::string>& updatedStaticNameServers,
1761 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1762{
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001763 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301764 asyncResp, "StaticNameServers", "xyz.openbmc_project.Network",
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001765 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1766 ifaceId,
George Liu9ae226f2023-06-21 17:56:46 +08001767 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ginu Georgee93abac2024-06-14 17:35:27 +05301768 updatedStaticNameServers);
Ed Tanousbf648f72021-06-03 15:00:14 -07001769}
1770
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001771inline void handleIPv6StaticAddressesPatch(
Ed Tanous3dfed532024-03-06 14:41:27 -08001772 const std::string& ifaceId,
1773 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Ed Tanous77179532023-02-28 10:45:28 -08001774 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001775 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1776{
Ed Tanousbf648f72021-06-03 15:00:14 -07001777 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001778 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001779 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanous3dfed532024-03-06 14:41:27 -08001780 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1781 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001782 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001783 std::string pathString =
1784 "IPv6StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001785 nlohmann::json::object_t* obj =
1786 std::get_if<nlohmann::json::object_t>(&thisJson);
1787 if (obj != nullptr && !obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001788 {
1789 std::optional<std::string> address;
1790 std::optional<uint8_t> prefixLength;
Ed Tanous3dfed532024-03-06 14:41:27 -08001791 nlohmann::json::object_t thisJsonCopy = *obj;
Patrick Williams504af5a2025-02-03 14:29:03 -05001792 if (!json_util::readJsonObject( //
Myung Baeafc474a2024-10-09 00:53:29 -07001793 thisJsonCopy, asyncResp->res, //
Patrick Williams504af5a2025-02-03 14:29:03 -05001794 "Address", address, //
1795 "PrefixLength", prefixLength //
Myung Baeafc474a2024-10-09 00:53:29 -07001796 ))
Ed Tanousbf648f72021-06-03 15:00:14 -07001797 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001798 messages::propertyValueFormatError(asyncResp->res, thisJsonCopy,
Ed Tanousf818b042022-06-27 13:17:35 -07001799 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001800 return;
1801 }
1802
Ed Tanousbf648f72021-06-03 15:00:14 -07001803 // Find the address and prefixLength values. Any values that are
1804 // not explicitly provided are assumed to be unmodified from the
1805 // current state of the interface. Merge existing state into the
1806 // current request.
Ed Tanousd547d8d2024-03-16 18:04:41 -07001807 if (!address)
Ed Tanousbf648f72021-06-03 15:00:14 -07001808 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001809 if (nicIpEntry == ipv6Data.end())
1810 {
1811 messages::propertyMissing(asyncResp->res,
1812 pathString + "/Address");
1813 return;
1814 }
1815 address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001816 }
1817
Ed Tanousd547d8d2024-03-16 18:04:41 -07001818 if (!prefixLength)
Ed Tanousbf648f72021-06-03 15:00:14 -07001819 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001820 if (nicIpEntry == ipv6Data.end())
1821 {
1822 messages::propertyMissing(asyncResp->res,
1823 pathString + "/PrefixLength");
1824 return;
1825 }
1826 prefixLength = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001827 }
1828
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001829 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001830 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001831 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
Ed Tanousd547d8d2024-03-16 18:04:41 -07001832 nicIpEntry->id, *prefixLength,
1833 *address, "", asyncResp);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001834 nicIpEntry =
1835 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001836 }
1837 else
1838 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001839 createIPv6(ifaceId, *prefixLength, *address, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001840 }
1841 entryIdx++;
1842 }
1843 else
1844 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001845 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001846 {
1847 // Requesting a DELETE/DO NOT MODIFY action for an item
1848 // that isn't present on the eth(n) interface. Input JSON is
1849 // in error, so bail out.
Ed Tanous3dfed532024-03-06 14:41:27 -08001850 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001851 {
1852 messages::resourceCannotBeDeleted(asyncResp->res);
1853 return;
1854 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001855 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001856 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001857 return;
1858 }
1859
Ed Tanous3dfed532024-03-06 14:41:27 -08001860 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001861 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001862 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001863 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001864 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001865 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001866 nicIpEntry =
1867 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001868 }
1869 entryIdx++;
1870 }
1871 }
1872}
1873
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001874inline std::string extractParentInterfaceName(const std::string& ifaceId)
1875{
1876 std::size_t pos = ifaceId.find('_');
1877 return ifaceId.substr(0, pos);
1878}
1879
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001880inline void parseInterfaceData(
1881 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1882 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1883 const std::vector<IPv4AddressData>& ipv4Data,
1884 const std::vector<IPv6AddressData>& ipv6Data,
1885 const std::vector<StaticGatewayData>& ipv6GatewayData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001886{
Ed Tanousbf648f72021-06-03 15:00:14 -07001887 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1888 jsonResponse["Id"] = ifaceId;
Ed Tanous253f11b2024-05-16 09:38:31 -07001889 jsonResponse["@odata.id"] =
1890 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
1891 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001892 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1893
Ed Tanousbf648f72021-06-03 15:00:14 -07001894 if (ethData.nicEnabled)
1895 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001896 jsonResponse["LinkStatus"] =
1897 ethData.linkUp ? ethernet_interface::LinkStatus::LinkUp
1898 : ethernet_interface::LinkStatus::LinkDown;
1899 jsonResponse["Status"]["State"] = resource::State::Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001900 }
1901 else
1902 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001903 jsonResponse["LinkStatus"] = ethernet_interface::LinkStatus::NoLink;
1904 jsonResponse["Status"]["State"] = resource::State::Disabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001905 }
1906
Ed Tanousbf648f72021-06-03 15:00:14 -07001907 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301908 jsonResponse["MTUSize"] = ethData.mtuSize;
Asmitha Karunanithi4652c642024-07-30 11:35:53 -05001909 if (ethData.macAddress)
1910 {
1911 jsonResponse["MACAddress"] = *ethData.macAddress;
1912 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001913 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001914 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
Jishnu CMe4588152023-05-11 00:04:40 -05001915 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1916 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
Ravi Tejade9ad762024-06-03 02:00:15 -05001917 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.domainv4Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001918 jsonResponse["DHCPv6"]["OperatingMode"] =
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001919 translateDhcpEnabledToBool(ethData.dhcpEnabled, false)
1920 ? "Enabled"
1921 : "Disabled";
Jishnu CMe4588152023-05-11 00:04:40 -05001922 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1923 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
Ravi Tejade9ad762024-06-03 02:00:15 -05001924 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.domainv6Enabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -05001925 jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1926 ethData.ipv6AcceptRa;
Ed Tanousbf648f72021-06-03 15:00:14 -07001927
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001928 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001929 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001930 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001931
1932 // When domain name is empty then it means, that it is a network
1933 // without domain names, and the host name itself must be treated as
1934 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001935 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001936 if (!ethData.domainnames.empty())
1937 {
1938 fqdn += "." + ethData.domainnames[0];
1939 }
1940 jsonResponse["FQDN"] = fqdn;
1941 }
1942
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001943 if (ethData.vlanId)
1944 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001945 jsonResponse["EthernetInterfaceType"] =
1946 ethernet_interface::EthernetDeviceType::Virtual;
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001947 jsonResponse["VLAN"]["VLANEnable"] = true;
1948 jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1949 jsonResponse["VLAN"]["Tagged"] = true;
1950
1951 nlohmann::json::array_t relatedInterfaces;
1952 nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1953 parentInterface["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001954 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1955 BMCWEB_REDFISH_MANAGER_URI_NAME,
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001956 extractParentInterfaceName(ifaceId));
1957 jsonResponse["Links"]["RelatedInterfaces"] =
1958 std::move(relatedInterfaces);
1959 }
1960 else
1961 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001962 jsonResponse["EthernetInterfaceType"] =
1963 ethernet_interface::EthernetDeviceType::Physical;
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001964 }
1965
Ed Tanousbf648f72021-06-03 15:00:14 -07001966 jsonResponse["NameServers"] = ethData.nameServers;
1967 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1968
1969 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1970 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1971 ipv4Array = nlohmann::json::array();
1972 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001973 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001974 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001975 std::string gatewayStr = ipv4Config.gateway;
1976 if (gatewayStr.empty())
1977 {
1978 gatewayStr = "0.0.0.0";
1979 }
Ed Tanous14766872022-03-15 10:44:42 -07001980 nlohmann::json::object_t ipv4;
1981 ipv4["AddressOrigin"] = ipv4Config.origin;
1982 ipv4["SubnetMask"] = ipv4Config.netmask;
1983 ipv4["Address"] = ipv4Config.address;
1984 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001985
Ed Tanousbf648f72021-06-03 15:00:14 -07001986 if (ipv4Config.origin == "Static")
1987 {
Ed Tanous14766872022-03-15 10:44:42 -07001988 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001989 }
Ed Tanous14766872022-03-15 10:44:42 -07001990
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001991 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001992 }
1993
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001994 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001995 if (ipv6GatewayStr.empty())
1996 {
1997 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1998 }
1999
2000 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
2001
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002002 nlohmann::json::array_t ipv6StaticGatewayArray;
2003 for (const auto& ipv6GatewayConfig : ipv6GatewayData)
2004 {
2005 nlohmann::json::object_t ipv6Gateway;
2006 ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002007 ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
2008 }
2009 jsonResponse["IPv6StaticDefaultGateways"] =
2010 std::move(ipv6StaticGatewayArray);
2011
Ed Tanousbf648f72021-06-03 15:00:14 -07002012 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
2013 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
2014 ipv6Array = nlohmann::json::array();
2015 ipv6StaticArray = nlohmann::json::array();
2016 nlohmann::json& ipv6AddrPolicyTable =
2017 jsonResponse["IPv6AddressPolicyTable"];
2018 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08002019 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07002020 {
Ed Tanous14766872022-03-15 10:44:42 -07002021 nlohmann::json::object_t ipv6;
2022 ipv6["Address"] = ipv6Config.address;
2023 ipv6["PrefixLength"] = ipv6Config.prefixLength;
2024 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05002025
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002026 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07002027 if (ipv6Config.origin == "Static")
2028 {
Ed Tanous14766872022-03-15 10:44:42 -07002029 nlohmann::json::object_t ipv6Static;
2030 ipv6Static["Address"] = ipv6Config.address;
2031 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002032 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07002033 }
2034 }
2035}
2036
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002037inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2038 const std::string& ifaceId,
2039 const boost::system::error_code& ec,
2040 const sdbusplus::message_t& m)
2041{
2042 if (!ec)
2043 {
2044 return;
2045 }
2046 const sd_bus_error* dbusError = m.get_error();
2047 if (dbusError == nullptr)
2048 {
2049 messages::internalError(asyncResp->res);
2050 return;
2051 }
Ed Tanous62598e32023-07-17 17:06:25 -07002052 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002053
2054 if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
2055 dbusError->name)
2056 {
2057 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2058 ifaceId);
2059 return;
2060 }
2061 if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
2062 dbusError->name)
2063 {
2064 messages::resourceCannotBeDeleted(asyncResp->res);
2065 return;
2066 }
2067 messages::internalError(asyncResp->res);
2068}
2069
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002070inline void afterVlanCreate(
2071 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2072 const std::string& parentInterfaceUri, const std::string& vlanInterface,
2073 const boost::system::error_code& ec, const sdbusplus::message_t& m
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002074
2075)
2076{
2077 if (ec)
2078 {
2079 const sd_bus_error* dbusError = m.get_error();
2080 if (dbusError == nullptr)
2081 {
2082 messages::internalError(asyncResp->res);
2083 return;
2084 }
Ed Tanous62598e32023-07-17 17:06:25 -07002085 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002086
2087 if (std::string_view(
2088 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
2089 dbusError->name)
2090 {
2091 messages::propertyValueNotInList(
2092 asyncResp->res, parentInterfaceUri,
2093 "Links/RelatedInterfaces/0/@odata.id");
2094 return;
2095 }
2096 if (std::string_view(
2097 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
2098 dbusError->name)
2099 {
2100 messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
2101 "Id", vlanInterface);
2102 return;
2103 }
2104 messages::internalError(asyncResp->res);
2105 return;
2106 }
2107
Ed Tanous253f11b2024-05-16 09:38:31 -07002108 const boost::urls::url vlanInterfaceUri =
2109 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2110 BMCWEB_REDFISH_MANAGER_URI_NAME, vlanInterface);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002111 asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
2112}
2113
Ed Tanousbf648f72021-06-03 15:00:14 -07002114inline void requestEthernetInterfacesRoutes(App& app)
2115{
Ed Tanous253f11b2024-05-16 09:38:31 -07002116 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07002117 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002118 .methods(boost::beast::http::verb::get)(
2119 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002120 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2121 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002122 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2123 {
2124 return;
2125 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002126
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002127 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2128 {
2129 messages::resourceNotFound(asyncResp->res, "Manager",
2130 managerId);
2131 return;
2132 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002133
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002134 asyncResp->res.jsonValue["@odata.type"] =
2135 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2136 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2137 "/redfish/v1/Managers/{}/EthernetInterfaces",
2138 BMCWEB_REDFISH_MANAGER_URI_NAME);
2139 asyncResp->res.jsonValue["Name"] =
2140 "Ethernet Network Interface Collection";
2141 asyncResp->res.jsonValue["Description"] =
2142 "Collection of EthernetInterfaces for this Manager";
2143
2144 // Get eth interface list, and call the below callback for JSON
2145 // preparation
2146 getEthernetIfaceList(
2147 [asyncResp](const bool& success,
2148 const std::vector<std::string>& ifaceList) {
2149 if (!success)
2150 {
2151 messages::internalError(asyncResp->res);
2152 return;
2153 }
2154
2155 nlohmann::json& ifaceArray =
2156 asyncResp->res.jsonValue["Members"];
2157 ifaceArray = nlohmann::json::array();
2158 for (const std::string& ifaceItem : ifaceList)
2159 {
2160 nlohmann::json::object_t iface;
2161 iface["@odata.id"] = boost::urls::format(
2162 "/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2163 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceItem);
2164 ifaceArray.push_back(std::move(iface));
2165 }
2166
2167 asyncResp->res.jsonValue["Members@odata.count"] =
2168 ifaceArray.size();
2169 asyncResp->res.jsonValue["@odata.id"] =
2170 boost::urls::format(
2171 "/redfish/v1/Managers/{}/EthernetInterfaces",
Ed Tanous253f11b2024-05-16 09:38:31 -07002172 BMCWEB_REDFISH_MANAGER_URI_NAME);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002173 });
2174 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002175
Ed Tanous253f11b2024-05-16 09:38:31 -07002176 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002177 .privileges(redfish::privileges::postEthernetInterfaceCollection)
2178 .methods(boost::beast::http::verb::post)(
2179 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002180 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2181 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002182 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2183 {
2184 return;
2185 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002186
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002187 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2188 {
2189 messages::resourceNotFound(asyncResp->res, "Manager",
2190 managerId);
2191 return;
2192 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002193
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002194 bool vlanEnable = false;
2195 uint32_t vlanId = 0;
2196 std::vector<nlohmann::json::object_t> relatedInterfaces;
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002197
Patrick Williams504af5a2025-02-03 14:29:03 -05002198 if (!json_util::readJsonPatch( //
2199 req, asyncResp->res, //
Myung Baeafc474a2024-10-09 00:53:29 -07002200 "Links/RelatedInterfaces", relatedInterfaces, //
Patrick Williams504af5a2025-02-03 14:29:03 -05002201 "VLAN/VLANEnable", vlanEnable, //
2202 "VLAN/VLANId", vlanId //
Myung Baeafc474a2024-10-09 00:53:29 -07002203 ))
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002204 {
2205 return;
2206 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002207
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002208 if (relatedInterfaces.size() != 1)
2209 {
2210 messages::arraySizeTooLong(asyncResp->res,
2211 "Links/RelatedInterfaces",
2212 relatedInterfaces.size());
2213 return;
2214 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002215
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002216 std::string parentInterfaceUri;
2217 if (!json_util::readJsonObject(relatedInterfaces[0],
2218 asyncResp->res, "@odata.id",
2219 parentInterfaceUri))
2220 {
2221 messages::propertyMissing(
2222 asyncResp->res, "Links/RelatedInterfaces/0/@odata.id");
2223 return;
2224 }
2225 BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002226
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002227 boost::system::result<boost::urls::url_view> parsedUri =
2228 boost::urls::parse_relative_ref(parentInterfaceUri);
2229 if (!parsedUri)
2230 {
2231 messages::propertyValueFormatError(
2232 asyncResp->res, parentInterfaceUri,
2233 "Links/RelatedInterfaces/0/@odata.id");
2234 return;
2235 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002236
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002237 std::string parentInterface;
2238 if (!crow::utility::readUrlSegments(
2239 *parsedUri, "redfish", "v1", "Managers", "bmc",
2240 "EthernetInterfaces", std::ref(parentInterface)))
2241 {
2242 messages::propertyValueNotInList(
2243 asyncResp->res, parentInterfaceUri,
2244 "Links/RelatedInterfaces/0/@odata.id");
2245 return;
2246 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002247
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002248 if (!vlanEnable)
2249 {
2250 // In OpenBMC implementation, VLANEnable cannot be false on
2251 // create
2252 messages::propertyValueIncorrect(
2253 asyncResp->res, "VLAN/VLANEnable", "false");
2254 return;
2255 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002256
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002257 std::string vlanInterface =
2258 parentInterface + "_" + std::to_string(vlanId);
Ed Tanous177612a2025-02-14 15:16:09 -08002259 dbus::utility::async_method_call(
2260 asyncResp,
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002261 [asyncResp, parentInterfaceUri,
2262 vlanInterface](const boost::system::error_code& ec,
2263 const sdbusplus::message_t& m) {
2264 afterVlanCreate(asyncResp, parentInterfaceUri,
2265 vlanInterface, ec, m);
2266 },
2267 "xyz.openbmc_project.Network",
2268 "/xyz/openbmc_project/network",
2269 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2270 parentInterface, vlanId);
2271 });
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002272
Ed Tanous253f11b2024-05-16 09:38:31 -07002273 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002274 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002275 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002276 [&app](const crow::Request& req,
2277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002278 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002279 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2280 {
2281 return;
2282 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002283
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002284 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2285 {
2286 messages::resourceNotFound(asyncResp->res, "Manager",
2287 managerId);
2288 return;
2289 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002290
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002291 getEthernetIfaceData(
2292 ifaceId,
2293 [asyncResp, ifaceId](
2294 const bool& success,
2295 const EthernetInterfaceData& ethData,
2296 const std::vector<IPv4AddressData>& ipv4Data,
2297 const std::vector<IPv6AddressData>& ipv6Data,
2298 const std::vector<StaticGatewayData>& ipv6GatewayData) {
2299 if (!success)
2300 {
2301 // TODO(Pawel)consider distinguish between non
2302 // existing object, and other errors
2303 messages::resourceNotFound(
2304 asyncResp->res, "EthernetInterface", ifaceId);
2305 return;
2306 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002307
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002308 asyncResp->res.jsonValue["@odata.type"] =
2309 "#EthernetInterface.v1_9_0.EthernetInterface";
2310 asyncResp->res.jsonValue["Name"] =
2311 "Manager Ethernet Interface";
2312 asyncResp->res.jsonValue["Description"] =
2313 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05302314
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002315 parseInterfaceData(asyncResp, ifaceId, ethData,
2316 ipv4Data, ipv6Data, ipv6GatewayData);
2317 });
2318 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002319
Ed Tanous253f11b2024-05-16 09:38:31 -07002320 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002321 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002322 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002323 [&app](const crow::Request& req,
2324 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002325 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002326 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2327 {
2328 return;
2329 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002330
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002331 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2332 {
2333 messages::resourceNotFound(asyncResp->res, "Manager",
2334 managerId);
2335 return;
2336 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002337
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002338 std::optional<std::string> hostname;
2339 std::optional<std::string> fqdn;
2340 std::optional<std::string> macAddress;
2341 std::optional<std::string> ipv6DefaultGateway;
2342 std::optional<std::vector<
2343 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2344 ipv4StaticAddresses;
2345 std::optional<std::vector<
2346 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2347 ipv6StaticAddresses;
2348 std::optional<std::vector<
2349 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2350 ipv6StaticDefaultGateways;
2351 std::optional<std::vector<std::string>> staticNameServers;
2352 std::optional<bool> ipv6AutoConfigEnabled;
2353 std::optional<bool> interfaceEnabled;
2354 std::optional<size_t> mtuSize;
2355 DHCPParameters v4dhcpParms;
2356 DHCPParameters v6dhcpParms;
Myung Baeafc474a2024-10-09 00:53:29 -07002357
Patrick Williams504af5a2025-02-03 14:29:03 -05002358 if (!json_util::readJsonPatch( //
2359 req, asyncResp->res, //
2360 "DHCPv4/DHCPEnabled", v4dhcpParms.dhcpv4Enabled, //
Myung Baeafc474a2024-10-09 00:53:29 -07002361 "DHCPv4/UseDNSServers", v4dhcpParms.useDnsServers, //
2362 "DHCPv4/UseDomainName", v4dhcpParms.useDomainName, //
2363 "DHCPv4/UseNTPServers", v4dhcpParms.useNtpServers, //
2364 "DHCPv6/OperatingMode",
Patrick Williams504af5a2025-02-03 14:29:03 -05002365 v6dhcpParms.dhcpv6OperatingMode, //
Myung Baeafc474a2024-10-09 00:53:29 -07002366 "DHCPv6/UseDNSServers", v6dhcpParms.useDnsServers, //
2367 "DHCPv6/UseDomainName", v6dhcpParms.useDomainName, //
2368 "DHCPv6/UseNTPServers", v6dhcpParms.useNtpServers, //
Patrick Williams504af5a2025-02-03 14:29:03 -05002369 "FQDN", fqdn, //
2370 "HostName", hostname, //
2371 "InterfaceEnabled", interfaceEnabled, //
2372 "IPv4StaticAddresses", ipv4StaticAddresses, //
2373 "IPv6DefaultGateway", ipv6DefaultGateway, //
2374 "IPv6StaticAddresses", ipv6StaticAddresses, //
Myung Baeafc474a2024-10-09 00:53:29 -07002375 "IPv6StaticDefaultGateways",
Patrick Williams504af5a2025-02-03 14:29:03 -05002376 ipv6StaticDefaultGateways, //
2377 "InterfaceEnabled", interfaceEnabled, //
2378 "MACAddress", macAddress, //
2379 "MTUSize", mtuSize, //
Myung Baeafc474a2024-10-09 00:53:29 -07002380 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
Patrick Williams504af5a2025-02-03 14:29:03 -05002381 ipv6AutoConfigEnabled, //
2382 "StaticNameServers", staticNameServers //
Myung Baeafc474a2024-10-09 00:53:29 -07002383 ))
2384 {
2385 return;
2386 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002387
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002388 // Get single eth interface data, and call the below callback
2389 // for JSON preparation
2390 getEthernetIfaceData(
2391 ifaceId,
2392 [asyncResp, ifaceId, hostname = std::move(hostname),
2393 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2394 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2395 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2396 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
2397 ipv6StaticDefaultGateway =
2398 std::move(ipv6StaticDefaultGateways),
2399 staticNameServers = std::move(staticNameServers), mtuSize,
2400 ipv6AutoConfigEnabled,
2401 v4dhcpParms = std::move(v4dhcpParms),
2402 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2403 const bool success,
2404 const EthernetInterfaceData& ethData,
2405 const std::vector<IPv4AddressData>& ipv4Data,
2406 const std::vector<IPv6AddressData>& ipv6Data,
2407 const std::vector<StaticGatewayData>&
2408 ipv6GatewayData) mutable {
2409 if (!success)
2410 {
2411 // ... otherwise return error
2412 // TODO(Pawel)consider distinguish between non
2413 // existing object, and other errors
2414 messages::resourceNotFound(
2415 asyncResp->res, "EthernetInterface", ifaceId);
2416 return;
2417 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002418
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002419 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2420 v6dhcpParms, asyncResp);
Tejas Patil35fb5312021-09-20 15:35:20 +05302421
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002422 if (hostname)
2423 {
2424 handleHostnamePatch(*hostname, asyncResp);
2425 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002426
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002427 if (ipv6AutoConfigEnabled)
2428 {
2429 handleSLAACAutoConfigPatch(
2430 ifaceId, *ipv6AutoConfigEnabled, asyncResp);
2431 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05002432
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002433 if (fqdn)
2434 {
2435 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2436 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002437
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002438 if (macAddress)
2439 {
2440 handleMACAddressPatch(ifaceId, *macAddress,
2441 asyncResp);
2442 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002443
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002444 if (ipv4StaticAddresses)
2445 {
2446 handleIPv4StaticPatch(ifaceId, *ipv4StaticAddresses,
2447 ethData, ipv4Data, asyncResp);
2448 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002449
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002450 if (staticNameServers)
2451 {
2452 handleStaticNameServersPatch(
2453 ifaceId, *staticNameServers, asyncResp);
2454 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002455
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002456 if (ipv6DefaultGateway)
2457 {
2458 messages::propertyNotWritable(asyncResp->res,
2459 "IPv6DefaultGateway");
2460 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002461
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002462 if (ipv6StaticAddresses)
2463 {
2464 handleIPv6StaticAddressesPatch(ifaceId,
2465 *ipv6StaticAddresses,
2466 ipv6Data, asyncResp);
2467 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002468
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002469 if (ipv6StaticDefaultGateway)
2470 {
2471 handleIPv6DefaultGateway(
2472 ifaceId, *ipv6StaticDefaultGateway,
2473 ipv6GatewayData, asyncResp);
2474 }
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002475
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002476 if (interfaceEnabled)
2477 {
2478 setDbusProperty(
2479 asyncResp, "InterfaceEnabled",
Ginu Georgee93abac2024-06-14 17:35:27 +05302480 "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08002481 sdbusplus::message::object_path(
2482 "/xyz/openbmc_project/network") /
2483 ifaceId,
2484 "xyz.openbmc_project.Network.EthernetInterface",
Ginu Georgee93abac2024-06-14 17:35:27 +05302485 "NICEnabled", *interfaceEnabled);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002486 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002487
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002488 if (mtuSize)
2489 {
2490 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2491 }
2492 });
2493 });
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002494
Ed Tanous253f11b2024-05-16 09:38:31 -07002495 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002496 .privileges(redfish::privileges::deleteEthernetInterface)
2497 .methods(boost::beast::http::verb::delete_)(
2498 [&app](const crow::Request& req,
2499 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002500 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002501 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2502 {
2503 return;
2504 }
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002505
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002506 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2507 {
2508 messages::resourceNotFound(asyncResp->res, "Manager",
2509 managerId);
2510 return;
2511 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002512
Ed Tanous177612a2025-02-14 15:16:09 -08002513 dbus::utility::async_method_call(
2514 asyncResp,
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002515 [asyncResp, ifaceId](const boost::system::error_code& ec,
2516 const sdbusplus::message_t& m) {
2517 afterDelete(asyncResp, ifaceId, ec, m);
2518 },
2519 "xyz.openbmc_project.Network",
2520 std::string("/xyz/openbmc_project/network/") + ifaceId,
2521 "xyz.openbmc_project.Object.Delete", "Delete");
2522 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002523}
2524
Ed Tanous1abe55e2018-09-05 08:30:59 -07002525} // namespace redfish