blob: 1e547e409da1374d6939ef92d11c6d7bfff8ef8c [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"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080010#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080011#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012#include "error_messages.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070013#include "generated/enums/ethernet_interface.hpp"
14#include "generated/enums/resource.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080015#include "http_request.hpp"
16#include "http_response.hpp"
Ed Tanous2c5875a2023-05-15 09:56:06 -070017#include "human_sort.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080018#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080019#include "query.hpp"
20#include "registries/privilege_registry.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080021#include "utility.hpp"
22#include "utils/dbus_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070023#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070025
Ed Tanousd7857202025-01-28 15:32:26 -080026#include <systemd/sd-bus.h>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027
Ed Tanousd7857202025-01-28 15:32:26 -080028#include <boost/beast/http/verb.hpp>
29#include <boost/system/error_code.hpp>
30#include <boost/system/result.hpp>
31#include <boost/url/format.hpp>
32#include <boost/url/parse.hpp>
33#include <boost/url/url.hpp>
34#include <boost/url/url_view.hpp>
35#include <sdbusplus/message.hpp>
36#include <sdbusplus/message/native_types.hpp>
37#include <sdbusplus/unpack_properties.hpp>
38
39#include <algorithm>
40#include <cctype>
Ed Tanous3dfed532024-03-06 14:41:27 -080041#include <cstddef>
Ed Tanousd7857202025-01-28 15:32:26 -080042#include <cstdint>
43#include <format>
44#include <functional>
Sunitha Harishce73d5c2023-04-07 06:46:49 -050045#include <memory>
Ed Tanousa24526d2018-12-10 15:17:59 -080046#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070047#include <ranges>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053048#include <regex>
Ed Tanousd7857202025-01-28 15:32:26 -080049#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080050#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080051#include <utility>
Ed Tanous3dfed532024-03-06 14:41:27 -080052#include <variant>
Ed Tanous77179532023-02-28 10:45:28 -080053#include <vector>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010054
Ed Tanous1abe55e2018-09-05 08:30:59 -070055namespace redfish
56{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010057
Ed Tanous4a0cb852018-10-15 07:55:04 -070058enum class LinkType
59{
60 Local,
61 Global
62};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010063
Johnathan Mantey743eb1c2024-04-03 12:05:57 -070064enum class IpVersion
65{
66 IpV4,
67 IpV6
68};
69
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010070/**
71 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010072 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070073struct IPv4AddressData
74{
75 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070076 std::string address;
77 std::string domain;
78 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 std::string netmask;
80 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080081 LinkType linktype{};
82 bool isActive{};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010083};
84
85/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050086 * Structure for keeping IPv6 data required by Redfish
87 */
88struct IPv6AddressData
89{
90 std::string id;
91 std::string address;
92 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080093 uint8_t prefixLength = 0;
Ravi Tejae48c0fc2019-04-16 08:37:20 -050094};
Sunitha Harishce73d5c2023-04-07 06:46:49 -050095
96/**
97 * Structure for keeping static route data required by Redfish
98 */
99struct StaticGatewayData
100{
101 std::string id;
102 std::string gateway;
103 size_t prefixLength = 0;
104 std::string protocol;
105};
106
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500107/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100108 * Structure for keeping basic single Ethernet Interface information
109 * available from DBus
110 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111struct EthernetInterfaceData
112{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700113 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +0530114 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800115 bool autoNeg;
Jishnu CMe4588152023-05-11 00:04:40 -0500116 bool dnsv4Enabled;
117 bool dnsv6Enabled;
Ravi Teja91c441e2024-02-23 09:03:43 -0600118 bool domainv4Enabled;
119 bool domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -0500120 bool ntpv4Enabled;
121 bool ntpv6Enabled;
122 bool hostNamev4Enabled;
123 bool hostNamev6Enabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800124 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700125 bool nicEnabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -0500126 bool ipv6AcceptRa;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800127 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700128 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800129 std::string hostName;
130 std::string defaultGateway;
131 std::string ipv6DefaultGateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500132 std::string ipv6StaticDefaultGateway;
Asmitha Karunanithi4652c642024-07-30 11:35:53 -0500133 std::optional<std::string> macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800134 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500135 std::vector<std::string> nameServers;
136 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800137 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100138};
139
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700140struct DHCPParameters
141{
142 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800143 std::optional<bool> useDnsServers;
144 std::optional<bool> useNtpServers;
145 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700146 std::optional<std::string> dhcpv6OperatingMode;
147};
148
Ed Tanous4a0cb852018-10-15 07:55:04 -0700149// Helper function that changes bits netmask notation (i.e. /24)
150// into full dot notation
151inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700153 uint32_t value = 0xffffffff << (32 - bits);
154 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
155 std::to_string((value >> 16) & 0xff) + "." +
156 std::to_string((value >> 8) & 0xff) + "." +
157 std::to_string(value & 0xff);
158 return netmask;
159}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100160
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800161inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700162 bool isIPv4)
163{
164 if (isIPv4)
165 {
166 return (
167 (inputDHCP ==
168 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
169 (inputDHCP ==
Asmitha Karunanithi6e78b682024-12-13 04:45:31 -0600170 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both") ||
171 (inputDHCP ==
172 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4v6stateless"));
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700173 }
174 return ((inputDHCP ==
175 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
176 (inputDHCP ==
177 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
178}
179
Ed Tanous2c70f802020-09-28 14:29:23 -0700180inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700181{
182 if (isIPv4 && isIPv6)
183 {
184 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
185 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700186 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700187 {
188 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
189 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700190 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700191 {
192 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
193 }
194 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
195}
196
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400197inline std::string translateAddressOriginDbusToRedfish(
198 const std::string& inputOrigin, bool isIPv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700199{
200 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700202 return "Static";
203 }
204 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
205 {
206 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700208 return "IPv4LinkLocal";
209 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700210 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 }
212 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
213 {
214 if (isIPv4)
215 {
216 return "DHCP";
217 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700218 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 }
220 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
221 {
222 return "SLAAC";
223 }
224 return "";
225}
226
Ed Tanous02cad962022-06-30 16:50:15 -0700227inline bool extractEthernetInterfaceData(
228 const std::string& ethifaceId,
229 const dbus::utility::ManagedObjectType& dbusData,
230 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700231{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700232 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700233 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700234 {
Ed Tanous02cad962022-06-30 16:50:15 -0700235 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700236 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000237 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700239 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700240 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500242 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700244 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500246 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800247 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700248 if (mac != nullptr)
249 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800250 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700251 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700253 }
254 }
255 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
256 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500257 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700258 {
259 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700260 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500261 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800262 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700263 if (id != nullptr)
264 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800265 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700266 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700267 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700268 }
269 }
270 else if (ifacePair.first ==
271 "xyz.openbmc_project.Network.EthernetInterface")
272 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500273 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700274 {
275 if (propertyPair.first == "AutoNeg")
276 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700277 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800278 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700279 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700280 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800281 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700282 }
283 }
284 else if (propertyPair.first == "Speed")
285 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500286 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800287 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700288 if (speed != nullptr)
289 {
290 ethData.speed = *speed;
291 }
292 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530293 else if (propertyPair.first == "MTU")
294 {
Anthony3e7a8da2023-10-23 14:22:43 +0800295 const size_t* mtuSize =
296 std::get_if<size_t>(&propertyPair.second);
Tejas Patil35fb5312021-09-20 15:35:20 +0530297 if (mtuSize != nullptr)
298 {
299 ethData.mtuSize = *mtuSize;
300 }
301 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800302 else if (propertyPair.first == "LinkUp")
303 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500304 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800305 std::get_if<bool>(&propertyPair.second);
306 if (linkUp != nullptr)
307 {
308 ethData.linkUp = *linkUp;
309 }
310 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700311 else if (propertyPair.first == "NICEnabled")
312 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500313 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700314 std::get_if<bool>(&propertyPair.second);
315 if (nicEnabled != nullptr)
316 {
317 ethData.nicEnabled = *nicEnabled;
318 }
319 }
Ravi Tejab10d8db2022-05-24 09:04:12 -0500320 else if (propertyPair.first == "IPv6AcceptRA")
321 {
322 const bool* ipv6AcceptRa =
323 std::get_if<bool>(&propertyPair.second);
324 if (ipv6AcceptRa != nullptr)
325 {
326 ethData.ipv6AcceptRa = *ipv6AcceptRa;
327 }
328 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500329 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700330 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500331 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500332 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800333 &propertyPair.second);
334 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700335 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700336 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500337 }
338 }
339 else if (propertyPair.first == "StaticNameServers")
340 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500341 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500342 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500343 &propertyPair.second);
344 if (staticNameServers != nullptr)
345 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700346 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700347 }
348 }
manojkiraneda2a133282019-02-19 13:09:43 +0530349 else if (propertyPair.first == "DHCPEnabled")
350 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700351 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700352 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700353 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530354 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800355 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530356 }
357 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800358 else if (propertyPair.first == "DomainName")
359 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500360 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500361 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800362 &propertyPair.second);
363 if (domainNames != nullptr)
364 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700365 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800366 }
367 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500368 else if (propertyPair.first == "DefaultGateway")
369 {
370 const std::string* defaultGateway =
371 std::get_if<std::string>(&propertyPair.second);
372 if (defaultGateway != nullptr)
373 {
374 std::string defaultGatewayStr = *defaultGateway;
375 if (defaultGatewayStr.empty())
376 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800377 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500378 }
379 else
380 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800381 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500382 }
383 }
384 }
385 else if (propertyPair.first == "DefaultGateway6")
386 {
387 const std::string* defaultGateway6 =
388 std::get_if<std::string>(&propertyPair.second);
389 if (defaultGateway6 != nullptr)
390 {
391 std::string defaultGateway6Str =
392 *defaultGateway6;
393 if (defaultGateway6Str.empty())
394 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800395 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500396 "0:0:0:0:0:0:0:0";
397 }
398 else
399 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800400 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500401 defaultGateway6Str;
402 }
403 }
404 }
Ed Tanous029573d2019-02-01 10:57:49 -0800405 }
406 }
407 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700408
Jishnu CMe4588152023-05-11 00:04:40 -0500409 sdbusplus::message::object_path path(
410 "/xyz/openbmc_project/network");
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400411 sdbusplus::message::object_path dhcp4Path =
412 path / ethifaceId / "dhcp4";
Jishnu CMe4588152023-05-11 00:04:40 -0500413
414 if (sdbusplus::message::object_path(objpath.first) == dhcp4Path)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700415 {
416 if (ifacePair.first ==
417 "xyz.openbmc_project.Network.DHCPConfiguration")
418 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500419 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700420 {
421 if (propertyPair.first == "DNSEnabled")
422 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700423 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700424 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700425 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700426 {
Jishnu CMe4588152023-05-11 00:04:40 -0500427 ethData.dnsv4Enabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700428 }
429 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600430 else if (propertyPair.first == "DomainEnabled")
431 {
432 const bool* domainEnabled =
433 std::get_if<bool>(&propertyPair.second);
434 if (domainEnabled != nullptr)
435 {
436 ethData.domainv4Enabled = *domainEnabled;
437 }
438 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700439 else if (propertyPair.first == "NTPEnabled")
440 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700441 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700442 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700443 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700444 {
Jishnu CMe4588152023-05-11 00:04:40 -0500445 ethData.ntpv4Enabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700446 }
447 }
448 else if (propertyPair.first == "HostNameEnabled")
449 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700450 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700451 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700452 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700453 {
Jishnu CMe4588152023-05-11 00:04:40 -0500454 ethData.hostNamev4Enabled = *hostNameEnabled;
455 }
456 }
457 }
458 }
459 }
460
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400461 sdbusplus::message::object_path dhcp6Path =
462 path / ethifaceId / "dhcp6";
Jishnu CMe4588152023-05-11 00:04:40 -0500463
464 if (sdbusplus::message::object_path(objpath.first) == dhcp6Path)
465 {
466 if (ifacePair.first ==
467 "xyz.openbmc_project.Network.DHCPConfiguration")
468 {
469 for (const auto& propertyPair : ifacePair.second)
470 {
471 if (propertyPair.first == "DNSEnabled")
472 {
473 const bool* dnsEnabled =
474 std::get_if<bool>(&propertyPair.second);
475 if (dnsEnabled != nullptr)
476 {
477 ethData.dnsv6Enabled = *dnsEnabled;
478 }
479 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600480 if (propertyPair.first == "DomainEnabled")
481 {
482 const bool* domainEnabled =
483 std::get_if<bool>(&propertyPair.second);
484 if (domainEnabled != nullptr)
485 {
486 ethData.domainv6Enabled = *domainEnabled;
487 }
488 }
Jishnu CMe4588152023-05-11 00:04:40 -0500489 else if (propertyPair.first == "NTPEnabled")
490 {
491 const bool* ntpEnabled =
492 std::get_if<bool>(&propertyPair.second);
493 if (ntpEnabled != nullptr)
494 {
495 ethData.ntpv6Enabled = *ntpEnabled;
496 }
497 }
498 else if (propertyPair.first == "HostNameEnabled")
499 {
500 const bool* hostNameEnabled =
501 std::get_if<bool>(&propertyPair.second);
502 if (hostNameEnabled != nullptr)
503 {
504 ethData.hostNamev6Enabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700505 }
506 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700507 }
508 }
509 }
Ed Tanous029573d2019-02-01 10:57:49 -0800510 // System configuration shows up in the global namespace, so no need
511 // to check eth number
512 if (ifacePair.first ==
513 "xyz.openbmc_project.Network.SystemConfiguration")
514 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500515 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800516 {
517 if (propertyPair.first == "HostName")
518 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500519 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500520 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800521 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700522 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800523 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800524 }
525 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 }
527 }
528 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700530 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700531}
532
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500533// Helper function that extracts data for single ethernet ipv6 address
Ed Tanous77179532023-02-28 10:45:28 -0800534inline void extractIPV6Data(const std::string& ethifaceId,
535 const dbus::utility::ManagedObjectType& dbusData,
536 std::vector<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500537{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400538 const std::string ipPathStart =
539 "/xyz/openbmc_project/network/" + ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500540
541 // Since there might be several IPv6 configurations aligned with
542 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000543 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500544 {
545 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800546 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500547 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800548 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500549 {
550 if (interface.first == "xyz.openbmc_project.Network.IP")
551 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400552 auto type = std::ranges::find_if(
553 interface.second, [](const auto& property) {
554 return property.first == "Type";
555 });
Tony Lee353163e2022-11-23 11:06:10 +0800556 if (type == interface.second.end())
557 {
558 continue;
559 }
560
561 const std::string* typeStr =
562 std::get_if<std::string>(&type->second);
563
564 if (typeStr == nullptr ||
565 (*typeStr !=
566 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
567 {
568 continue;
569 }
570
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500571 // Instance IPv6AddressData structure, and set as
572 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800573 IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700574 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800575 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800576 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500577 {
578 if (property.first == "Address")
579 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500580 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500581 std::get_if<std::string>(&property.second);
582 if (address != nullptr)
583 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700584 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500585 }
586 }
587 else if (property.first == "Origin")
588 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500589 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500590 std::get_if<std::string>(&property.second);
591 if (origin != nullptr)
592 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700593 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500594 translateAddressOriginDbusToRedfish(*origin,
595 false);
596 }
597 }
598 else if (property.first == "PrefixLength")
599 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500600 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500601 std::get_if<uint8_t>(&property.second);
602 if (prefix != nullptr)
603 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700604 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500605 }
606 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600607 else if (property.first == "Type" ||
608 property.first == "Gateway")
609 {
610 // Type & Gateway is not used
611 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500612 else
613 {
Ed Tanous62598e32023-07-17 17:06:25 -0700614 BMCWEB_LOG_ERROR(
615 "Got extra property: {} on the {} object",
616 property.first, objpath.first.str);
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500617 }
618 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500619 }
620 }
621 }
622 }
623}
624
Ed Tanous4a0cb852018-10-15 07:55:04 -0700625// Helper function that extracts data for single ethernet ipv4 address
Ed Tanous77179532023-02-28 10:45:28 -0800626inline void extractIPData(const std::string& ethifaceId,
627 const dbus::utility::ManagedObjectType& dbusData,
628 std::vector<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700629{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400630 const std::string ipPathStart =
631 "/xyz/openbmc_project/network/" + ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632
633 // Since there might be several IPv4 configurations aligned with
634 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000635 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700636 {
637 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800638 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700639 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800640 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700641 {
642 if (interface.first == "xyz.openbmc_project.Network.IP")
643 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400644 auto type = std::ranges::find_if(
645 interface.second, [](const auto& property) {
646 return property.first == "Type";
647 });
Tony Lee353163e2022-11-23 11:06:10 +0800648 if (type == interface.second.end())
649 {
650 continue;
651 }
652
653 const std::string* typeStr =
654 std::get_if<std::string>(&type->second);
655
656 if (typeStr == nullptr ||
657 (*typeStr !=
658 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
659 {
660 continue;
661 }
662
Ed Tanous4a0cb852018-10-15 07:55:04 -0700663 // Instance IPv4AddressData structure, and set as
664 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800665 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700666 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800667 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800668 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700669 {
670 if (property.first == "Address")
671 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500672 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800673 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700674 if (address != nullptr)
675 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700676 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700677 }
678 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 else if (property.first == "Origin")
680 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500681 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800682 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700683 if (origin != nullptr)
684 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700685 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700686 translateAddressOriginDbusToRedfish(*origin,
687 true);
688 }
689 }
690 else if (property.first == "PrefixLength")
691 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500692 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800693 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700694 if (mask != nullptr)
695 {
696 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700697 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700698 }
699 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600700 else if (property.first == "Type" ||
701 property.first == "Gateway")
702 {
703 // Type & Gateway is not used
704 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700705 else
706 {
Ed Tanous62598e32023-07-17 17:06:25 -0700707 BMCWEB_LOG_ERROR(
708 "Got extra property: {} on the {} object",
709 property.first, objpath.first.str);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700710 }
711 }
712 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700713 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700714 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700715 ? LinkType::Local
716 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700717 }
718 }
719 }
720 }
721}
722
723/**
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700724 * @brief Modifies the default gateway assigned to the NIC
725 *
726 * @param[in] ifaceId Id of network interface whose default gateway is to be
727 * changed
728 * @param[in] gateway The new gateway value. Assigning an empty string
729 * causes the gateway to be deleted
730 * @param[io] asyncResp Response object that will be returned to client
731 *
732 * @return None
733 */
734inline void updateIPv4DefaultGateway(
735 const std::string& ifaceId, const std::string& gateway,
736 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
737{
738 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +0530739 asyncResp, "Gateway", "xyz.openbmc_project.Network",
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700740 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
741 ifaceId,
742 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ginu Georgee93abac2024-06-14 17:35:27 +0530743 gateway);
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700744}
745
746/**
747 * @brief Deletes given static IP address for the interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700748 *
749 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700750 * @param[in] ipHash DBus Hash id of IP that should be deleted
751 * @param[io] asyncResp Response object that will be returned to client
752 *
753 * @return None
754 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600755inline void deleteIPAddress(const std::string& ifaceId,
756 const std::string& ipHash,
757 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700758{
759 crow::connections::systemBus->async_method_call(
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 Tanous4a0cb852018-10-15 07:55:04 -0700795 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500796 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{
823 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600824 [asyncResp, version, ifaceId, address, prefixLength,
825 gateway](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400826 if (ec)
Johnathan Mantey01784822019-06-18 12:44:21 -0700827 {
828 messages::internalError(asyncResp->res);
829 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400830 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
831 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
832 crow::connections::systemBus->async_method_call(
833 [asyncResp](const boost::system::error_code& ec2) {
834 if (ec2)
835 {
836 messages::internalError(asyncResp->res);
837 }
838 },
839 "xyz.openbmc_project.Network",
840 "/xyz/openbmc_project/network/" + ifaceId,
841 "xyz.openbmc_project.Network.IP.Create", "IP", protocol,
842 address, prefixLength, gateway);
Patrick Williams5a39f772023-10-20 11:20:21 -0500843 },
Johnathan Mantey01784822019-06-18 12:44:21 -0700844 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600845 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700846 "xyz.openbmc_project.Object.Delete", "Delete");
847}
848
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500849inline bool extractIPv6DefaultGatewayData(
850 const std::string& ethifaceId,
851 const dbus::utility::ManagedObjectType& dbusData,
852 std::vector<StaticGatewayData>& staticGatewayConfig)
853{
854 std::string staticGatewayPathStart("/xyz/openbmc_project/network/");
855 staticGatewayPathStart += ethifaceId;
856
857 for (const auto& objpath : dbusData)
858 {
859 if (!std::string_view(objpath.first.str)
860 .starts_with(staticGatewayPathStart))
861 {
862 continue;
863 }
864 for (const auto& interface : objpath.second)
865 {
866 if (interface.first != "xyz.openbmc_project.Network.StaticGateway")
867 {
868 continue;
869 }
870 StaticGatewayData& staticGateway =
871 staticGatewayConfig.emplace_back();
872 staticGateway.id = objpath.first.filename();
873
874 bool success = sdbusplus::unpackPropertiesNoThrow(
875 redfish::dbus_utils::UnpackErrorPrinter(), interface.second,
Ravi Tejaab0d4392024-09-03 12:27:40 -0500876 "Gateway", staticGateway.gateway, "ProtocolType",
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500877 staticGateway.protocol);
878 if (!success)
879 {
880 return false;
881 }
882 }
883 }
884 return true;
885}
886
Johnathan Mantey01784822019-06-18 12:44:21 -0700887/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500888 * @brief Creates IPv6 with given data
889 *
890 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500891 * @param[in] prefixLength Prefix length that needs to be added
892 * @param[in] address IP address that needs to be added
893 * @param[io] asyncResp Response object that will be returned to client
894 *
895 * @return None
896 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500897inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
898 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800899 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500900{
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500901 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
902 path /= ifaceId;
903
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400904 auto createIpHandler =
905 [asyncResp, address](const boost::system::error_code& ec) {
906 if (ec)
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500907 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400908 if (ec == boost::system::errc::io_error)
909 {
910 messages::propertyValueFormatError(asyncResp->res, address,
911 "Address");
912 }
913 else
914 {
915 messages::internalError(asyncResp->res);
916 }
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500917 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400918 };
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500919 // Passing null for gateway, as per redfish spec IPv6StaticAddresses
920 // object does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500921 crow::connections::systemBus->async_method_call(
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500922 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500923 "xyz.openbmc_project.Network.IP.Create", "IP",
924 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
925 "");
926}
927
Ed Tanous4a0cb852018-10-15 07:55:04 -0700928/**
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500929 * @brief Deletes given IPv6 Static Gateway
930 *
931 * @param[in] ifaceId Id of interface whose IP should be deleted
932 * @param[in] ipHash DBus Hash id of IP that should be deleted
933 * @param[io] asyncResp Response object that will be returned to client
934 *
935 * @return None
936 */
Patrick Williams504af5a2025-02-03 14:29:03 -0500937inline void deleteIPv6Gateway(
938 std::string_view ifaceId, std::string_view gatewayId,
939 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500940{
941 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
Ravi Teja739b27b2024-08-27 21:03:53 -0500942 path /= ifaceId;
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500943 path /= gatewayId;
944 crow::connections::systemBus->async_method_call(
945 [asyncResp](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400946 if (ec)
947 {
948 messages::internalError(asyncResp->res);
949 }
950 },
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500951 "xyz.openbmc_project.Network", path,
952 "xyz.openbmc_project.Object.Delete", "Delete");
953}
954
955/**
956 * @brief Creates IPv6 static default gateway with given data
957 *
958 * @param[in] ifaceId Id of interface whose IP should be added
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500959 * @param[in] gateway Gateway address that needs to be added
960 * @param[io] asyncResp Response object that will be returned to client
961 *
962 * @return None
963 */
964inline void createIPv6DefaultGateway(
Asmitha Karunanithicf91c8c2025-01-24 04:39:15 -0600965 std::string_view ifaceId, const std::string& gateway,
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500966 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
967{
968 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
969 path /= ifaceId;
970 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
971 if (ec)
972 {
973 messages::internalError(asyncResp->res);
974 }
975 };
976 crow::connections::systemBus->async_method_call(
977 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
978 "xyz.openbmc_project.Network.StaticGateway.Create", "StaticGateway",
Ravi Tejaab0d4392024-09-03 12:27:40 -0500979 gateway, "xyz.openbmc_project.Network.IP.Protocol.IPv6");
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500980}
981
982/**
983 * @brief Deletes the IPv6 default gateway entry for this interface and
984 * creates a replacement IPv6 default gateway entry
985 *
986 * @param[in] ifaceId Id of interface upon which to create the IPv6
987 * entry
988 * @param[in] gateway IPv6 gateway to assign to this interface
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500989 * @param[io] asyncResp Response object that will be returned to client
990 *
991 * @return None
992 */
993inline void deleteAndCreateIPv6DefaultGateway(
994 std::string_view ifaceId, std::string_view gatewayId,
Asmitha Karunanithicf91c8c2025-01-24 04:39:15 -0600995 const std::string& gateway,
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500996 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
997{
998 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
Ravi Teja739b27b2024-08-27 21:03:53 -0500999 path /= ifaceId;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001000 path /= gatewayId;
1001 crow::connections::systemBus->async_method_call(
Ravi Tejaab0d4392024-09-03 12:27:40 -05001002 [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001003 if (ec)
1004 {
1005 messages::internalError(asyncResp->res);
1006 return;
1007 }
Ravi Tejaab0d4392024-09-03 12:27:40 -05001008 createIPv6DefaultGateway(ifaceId, gateway, asyncResp);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001009 },
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001010 "xyz.openbmc_project.Network", path,
1011 "xyz.openbmc_project.Object.Delete", "Delete");
1012}
1013
1014/**
1015 * @brief Sets IPv6 default gateway with given data
1016 *
1017 * @param[in] ifaceId Id of interface whose gateway should be added
1018 * @param[in] input Contains address that needs to be added
1019 * @param[in] staticGatewayData Current static gateways in the system
1020 * @param[io] asyncResp Response object that will be returned to client
1021 *
1022 * @return None
1023 */
1024
1025inline void handleIPv6DefaultGateway(
Ed Tanous3dfed532024-03-06 14:41:27 -08001026 const std::string& ifaceId,
1027 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001028 const std::vector<StaticGatewayData>& staticGatewayData,
1029 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1030{
1031 size_t entryIdx = 1;
1032 std::vector<StaticGatewayData>::const_iterator staticGatewayEntry =
1033 staticGatewayData.begin();
1034
Ed Tanous3dfed532024-03-06 14:41:27 -08001035 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1036 input)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001037 {
1038 // find the next gateway entry
1039 while (staticGatewayEntry != staticGatewayData.end())
1040 {
1041 if (staticGatewayEntry->protocol ==
1042 "xyz.openbmc_project.Network.IP.Protocol.IPv6")
1043 {
1044 break;
1045 }
1046 staticGatewayEntry++;
1047 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001048 std::string pathString =
1049 "IPv6StaticDefaultGateways/" + std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001050 nlohmann::json::object_t* obj =
1051 std::get_if<nlohmann::json::object_t>(&thisJson);
1052 if (obj == nullptr)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001053 {
1054 if (staticGatewayEntry == staticGatewayData.end())
1055 {
1056 messages::resourceCannotBeDeleted(asyncResp->res);
1057 return;
1058 }
Ravi Teja739b27b2024-08-27 21:03:53 -05001059 deleteIPv6Gateway(ifaceId, staticGatewayEntry->id, asyncResp);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001060 return;
1061 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001062 if (obj->empty())
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001063 {
1064 // Do nothing, but make sure the entry exists.
1065 if (staticGatewayEntry == staticGatewayData.end())
1066 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001067 messages::propertyValueFormatError(asyncResp->res, *obj,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001068 pathString);
1069 return;
1070 }
1071 }
1072 std::optional<std::string> address;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001073
Ravi Tejaab0d4392024-09-03 12:27:40 -05001074 if (!json_util::readJsonObject(*obj, asyncResp->res, "Address",
1075 address))
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001076 {
1077 return;
1078 }
1079 const std::string* addr = nullptr;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001080 if (address)
1081 {
1082 addr = &(*address);
1083 }
1084 else if (staticGatewayEntry != staticGatewayData.end())
1085 {
1086 addr = &(staticGatewayEntry->gateway);
1087 }
1088 else
1089 {
1090 messages::propertyMissing(asyncResp->res, pathString + "/Address");
1091 return;
1092 }
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001093 if (staticGatewayEntry != staticGatewayData.end())
1094 {
1095 deleteAndCreateIPv6DefaultGateway(ifaceId, staticGatewayEntry->id,
Ravi Tejaab0d4392024-09-03 12:27:40 -05001096 *addr, asyncResp);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001097 staticGatewayEntry++;
1098 }
1099 else
1100 {
Ravi Tejaab0d4392024-09-03 12:27:40 -05001101 createIPv6DefaultGateway(ifaceId, *addr, asyncResp);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001102 }
1103 entryIdx++;
1104 }
1105}
1106
1107/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001108 * Function that retrieves all properties for given Ethernet Interface
1109 * Object
1110 * from EntityManager Network Manager
1111 * @param ethiface_id a eth interface id to query on DBus
1112 * @param callback a function that shall be called to convert Dbus output
1113 * into JSON
1114 */
1115template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +00001116void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001117 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001118{
George Liuf5892d02023-03-01 10:37:08 +08001119 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1120 dbus::utility::getManagedObjects(
1121 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001122 [ethifaceId{std::string{ethifaceId}},
Ed Tanous8cb2c022024-03-27 16:31:46 -07001123 callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001124 const boost::system::error_code& ec,
Ed Tanous3dfed532024-03-06 14:41:27 -08001125 const dbus::utility::ManagedObjectType& resp) mutable {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001126 EthernetInterfaceData ethData{};
1127 std::vector<IPv4AddressData> ipv4Data;
1128 std::vector<IPv6AddressData> ipv6Data;
1129 std::vector<StaticGatewayData> ipv6GatewayData;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001130
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001131 if (ec)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001132 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001133 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1134 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001135 }
1136
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001137 bool found =
1138 extractEthernetInterfaceData(ethifaceId, resp, ethData);
1139 if (!found)
1140 {
1141 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1142 return;
1143 }
1144
1145 extractIPData(ethifaceId, resp, ipv4Data);
1146 // Fix global GW
1147 for (IPv4AddressData& ipv4 : ipv4Data)
1148 {
1149 if (((ipv4.linktype == LinkType::Global) &&
1150 (ipv4.gateway == "0.0.0.0")) ||
1151 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
1152 {
1153 ipv4.gateway = ethData.defaultGateway;
1154 }
1155 }
1156
1157 extractIPV6Data(ethifaceId, resp, ipv6Data);
1158 if (!extractIPv6DefaultGatewayData(ethifaceId, resp,
1159 ipv6GatewayData))
1160 {
1161 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1162 }
1163 // Finally make a callback with useful data
1164 callback(true, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1165 });
Ed Tanous271584a2019-07-09 16:24:22 -07001166}
Ed Tanous4a0cb852018-10-15 07:55:04 -07001167
1168/**
1169 * Function that retrieves all Ethernet Interfaces available through Network
1170 * Manager
1171 * @param callback a function that shall be called to convert Dbus output
1172 * into JSON.
1173 */
1174template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001175void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001176{
George Liuf5892d02023-03-01 10:37:08 +08001177 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1178 dbus::utility::getManagedObjects(
1179 "xyz.openbmc_project.Network", path,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001180 [callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001181 const boost::system::error_code& ec,
George Liuf5892d02023-03-01 10:37:08 +08001182 const dbus::utility::ManagedObjectType& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001183 // Callback requires vector<string> to retrieve all available
1184 // ethernet interfaces
1185 std::vector<std::string> ifaceList;
1186 ifaceList.reserve(resp.size());
1187 if (ec)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001188 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001189 callback(false, ifaceList);
1190 return;
1191 }
1192
1193 // Iterate over all retrieved ObjectPaths.
1194 for (const auto& objpath : resp)
1195 {
1196 // And all interfaces available for certain ObjectPath.
1197 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001198 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001199 // If interface is
1200 // xyz.openbmc_project.Network.EthernetInterface, this is
1201 // what we're looking for.
1202 if (interface.first ==
1203 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001204 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001205 std::string ifaceId = objpath.first.filename();
1206 if (ifaceId.empty())
1207 {
1208 continue;
1209 }
1210 // and put it into output vector.
1211 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001212 }
1213 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001214 }
Ed Tanous2c5875a2023-05-15 09:56:06 -07001215
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001216 std::ranges::sort(ifaceList, AlphanumLess<std::string>());
Ed Tanous2c5875a2023-05-15 09:56:06 -07001217
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001218 // Finally make a callback with useful data
1219 callback(true, ifaceList);
1220 });
Ed Tanous271584a2019-07-09 16:24:22 -07001221}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001222
Patrick Williams504af5a2025-02-03 14:29:03 -05001223inline void handleHostnamePatch(
1224 const std::string& hostname,
1225 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001226{
Ed Tanousbf648f72021-06-03 15:00:14 -07001227 // SHOULD handle host names of up to 255 characters(RFC 1123)
1228 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001229 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001230 messages::propertyValueFormatError(asyncResp->res, hostname,
1231 "HostName");
1232 return;
1233 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001234 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301235 asyncResp, "HostName", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001236 sdbusplus::message::object_path("/xyz/openbmc_project/network/config"),
1237 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ginu Georgee93abac2024-06-14 17:35:27 +05301238 hostname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001239}
1240
Patrick Williams504af5a2025-02-03 14:29:03 -05001241inline void handleMTUSizePatch(
1242 const std::string& ifaceId, const size_t mtuSize,
1243 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Tejas Patil35fb5312021-09-20 15:35:20 +05301244{
Ed Tanousd02aad32024-02-13 14:43:34 -08001245 sdbusplus::message::object_path objPath("/xyz/openbmc_project/network");
1246 objPath /= ifaceId;
Ginu Georgee93abac2024-06-14 17:35:27 +05301247 setDbusProperty(asyncResp, "MTUSize", "xyz.openbmc_project.Network",
1248 objPath, "xyz.openbmc_project.Network.EthernetInterface",
1249 "MTU", mtuSize);
Tejas Patil35fb5312021-09-20 15:35:20 +05301250}
1251
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001252inline void handleDomainnamePatch(
1253 const std::string& ifaceId, const std::string& domainname,
1254 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001255{
1256 std::vector<std::string> vectorDomainname = {domainname};
Ed Tanousd02aad32024-02-13 14:43:34 -08001257 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301258 asyncResp, "FQDN", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001259 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1260 ifaceId,
Ginu Georgee93abac2024-06-14 17:35:27 +05301261 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanousd02aad32024-02-13 14:43:34 -08001262 vectorDomainname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001263}
1264
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001265inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001266{
1267 // A valid host name can never have the dotted-decimal form (RFC 1123)
Ed Tanous3544d2a2023-08-06 18:12:20 -07001268 if (std::ranges::all_of(hostname, ::isdigit))
Ed Tanousbf648f72021-06-03 15:00:14 -07001269 {
1270 return false;
1271 }
1272 // Each label(hostname/subdomains) within a valid FQDN
1273 // MUST handle host names of up to 63 characters (RFC 1123)
1274 // labels cannot start or end with hyphens (RFC 952)
1275 // labels can start with numbers (RFC 1123)
Ed Tanous4b242742023-05-11 09:51:51 -07001276 const static std::regex pattern(
Ed Tanousbf648f72021-06-03 15:00:14 -07001277 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1278
1279 return std::regex_match(hostname, pattern);
1280}
1281
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001282inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001283{
1284 // Can have multiple subdomains
1285 // Top Level Domain's min length is 2 character
Ed Tanous4b242742023-05-11 09:51:51 -07001286 const static std::regex pattern(
George Liu0fda0f12021-11-16 10:06:17 +08001287 "^([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 -07001288
1289 return std::regex_match(domainname, pattern);
1290}
1291
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001292inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1293 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001294{
1295 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1296 if (fqdn.length() > 255)
1297 {
1298 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1299 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001300 }
1301
Ed Tanousbf648f72021-06-03 15:00:14 -07001302 size_t pos = fqdn.find('.');
1303 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001304 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001305 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1306 return;
1307 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001308
Ed Tanousbf648f72021-06-03 15:00:14 -07001309 std::string hostname;
1310 std::string domainname;
1311 domainname = (fqdn).substr(pos + 1);
1312 hostname = (fqdn).substr(0, pos);
1313
1314 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1315 {
1316 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1317 return;
1318 }
1319
1320 handleHostnamePatch(hostname, asyncResp);
1321 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1322}
1323
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001324inline void handleMACAddressPatch(
1325 const std::string& ifaceId, const std::string& macAddress,
1326 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001327{
Ed Tanousd02aad32024-02-13 14:43:34 -08001328 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301329 asyncResp, "MACAddress", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001330 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1331 ifaceId,
Ginu Georgee93abac2024-06-14 17:35:27 +05301332 "xyz.openbmc_project.Network.MACAddress", "MACAddress", macAddress);
Ed Tanousbf648f72021-06-03 15:00:14 -07001333}
1334
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001335inline void setDHCPEnabled(const std::string& ifaceId,
1336 const std::string& propertyName, const bool v4Value,
1337 const bool v6Value,
1338 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001339{
1340 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
Ed Tanousd02aad32024-02-13 14:43:34 -08001341 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301342 asyncResp, "DHCPv4", "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08001343 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1344 ifaceId,
Ginu Georgee93abac2024-06-14 17:35:27 +05301345 "xyz.openbmc_project.Network.EthernetInterface", propertyName, dhcp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001346}
1347
Jishnu CMe4588152023-05-11 00:04:40 -05001348enum class NetworkType
1349{
1350 dhcp4,
1351 dhcp6
1352};
1353
1354inline void setDHCPConfig(const std::string& propertyName, const bool& value,
1355 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1356 const std::string& ethifaceId, NetworkType type)
Ed Tanousbf648f72021-06-03 15:00:14 -07001357{
Ed Tanous62598e32023-07-17 17:06:25 -07001358 BMCWEB_LOG_DEBUG("{} = {}", propertyName, value);
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001359 std::string redfishPropertyName;
Jishnu CMe4588152023-05-11 00:04:40 -05001360 sdbusplus::message::object_path path("/xyz/openbmc_project/network/");
1361 path /= ethifaceId;
1362
1363 if (type == NetworkType::dhcp4)
1364 {
1365 path /= "dhcp4";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001366 redfishPropertyName = "DHCPv4";
Jishnu CMe4588152023-05-11 00:04:40 -05001367 }
1368 else
1369 {
1370 path /= "dhcp6";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001371 redfishPropertyName = "DHCPv6";
Jishnu CMe4588152023-05-11 00:04:40 -05001372 }
1373
Ginu Georgee93abac2024-06-14 17:35:27 +05301374 setDbusProperty(
1375 asyncResp, redfishPropertyName, "xyz.openbmc_project.Network", path,
1376 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, value);
Ed Tanousbf648f72021-06-03 15:00:14 -07001377}
1378
Ravi Tejab10d8db2022-05-24 09:04:12 -05001379inline void handleSLAACAutoConfigPatch(
1380 const std::string& ifaceId, bool ipv6AutoConfigEnabled,
1381 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1382{
1383 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1384 path /= ifaceId;
Ginu Georgee93abac2024-06-14 17:35:27 +05301385 setDbusProperty(asyncResp,
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001386 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
Ginu Georgee93abac2024-06-14 17:35:27 +05301387 "xyz.openbmc_project.Network", path,
1388 "xyz.openbmc_project.Network.EthernetInterface",
1389 "IPv6AcceptRA", ipv6AutoConfigEnabled);
Ravi Tejab10d8db2022-05-24 09:04:12 -05001390}
1391
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001392inline void handleDHCPPatch(
1393 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1394 const DHCPParameters& v4dhcpParms, const DHCPParameters& v6dhcpParms,
1395 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001396{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001397 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1398 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001399
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001400 if (ipv4Active)
1401 {
1402 updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1403 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001404 bool nextv4DHCPState =
1405 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1406
1407 bool nextv6DHCPState{};
1408 if (v6dhcpParms.dhcpv6OperatingMode)
1409 {
Ravi Tejab10d8db2022-05-24 09:04:12 -05001410 if ((*v6dhcpParms.dhcpv6OperatingMode != "Enabled") &&
Ed Tanousbf648f72021-06-03 15:00:14 -07001411 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1412 {
1413 messages::propertyValueFormatError(asyncResp->res,
1414 *v6dhcpParms.dhcpv6OperatingMode,
1415 "OperatingMode");
1416 return;
1417 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05001418 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Enabled");
Ed Tanousbf648f72021-06-03 15:00:14 -07001419 }
1420 else
1421 {
1422 nextv6DHCPState = ipv6Active;
1423 }
1424
Jishnu CMe4588152023-05-11 00:04:40 -05001425 bool nextDNSv4 = ethData.dnsv4Enabled;
1426 bool nextDNSv6 = ethData.dnsv6Enabled;
1427 if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001428 {
Jishnu CMe4588152023-05-11 00:04:40 -05001429 nextDNSv4 = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001430 }
Jishnu CMe4588152023-05-11 00:04:40 -05001431 if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001432 {
Jishnu CMe4588152023-05-11 00:04:40 -05001433 nextDNSv6 = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001434 }
1435
Jishnu CMe4588152023-05-11 00:04:40 -05001436 bool nextNTPv4 = ethData.ntpv4Enabled;
1437 bool nextNTPv6 = ethData.ntpv6Enabled;
1438 if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001439 {
Jishnu CMe4588152023-05-11 00:04:40 -05001440 nextNTPv4 = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001441 }
Jishnu CMe4588152023-05-11 00:04:40 -05001442 if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001443 {
Jishnu CMe4588152023-05-11 00:04:40 -05001444 nextNTPv6 = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001445 }
1446
Ravi Teja91c441e2024-02-23 09:03:43 -06001447 bool nextUsev4Domain = ethData.domainv4Enabled;
1448 bool nextUsev6Domain = ethData.domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -05001449 if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001450 {
Jishnu CMe4588152023-05-11 00:04:40 -05001451 nextUsev4Domain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001452 }
Jishnu CMe4588152023-05-11 00:04:40 -05001453 if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001454 {
Jishnu CMe4588152023-05-11 00:04:40 -05001455 nextUsev6Domain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001456 }
1457
Ed Tanous62598e32023-07-17 17:06:25 -07001458 BMCWEB_LOG_DEBUG("set DHCPEnabled...");
Ed Tanousbf648f72021-06-03 15:00:14 -07001459 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1460 asyncResp);
Ed Tanous62598e32023-07-17 17:06:25 -07001461 BMCWEB_LOG_DEBUG("set DNSEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001462 setDHCPConfig("DNSEnabled", nextDNSv4, asyncResp, ifaceId,
1463 NetworkType::dhcp4);
Ed Tanous62598e32023-07-17 17:06:25 -07001464 BMCWEB_LOG_DEBUG("set NTPEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001465 setDHCPConfig("NTPEnabled", nextNTPv4, asyncResp, ifaceId,
1466 NetworkType::dhcp4);
Ravi Teja91c441e2024-02-23 09:03:43 -06001467 BMCWEB_LOG_DEBUG("set DomainEnabled...");
1468 setDHCPConfig("DomainEnabled", nextUsev4Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001469 NetworkType::dhcp4);
1470 BMCWEB_LOG_DEBUG("set DNSEnabled for dhcp6...");
1471 setDHCPConfig("DNSEnabled", nextDNSv6, asyncResp, ifaceId,
1472 NetworkType::dhcp6);
1473 BMCWEB_LOG_DEBUG("set NTPEnabled for dhcp6...");
1474 setDHCPConfig("NTPEnabled", nextNTPv6, asyncResp, ifaceId,
1475 NetworkType::dhcp6);
Ravi Teja91c441e2024-02-23 09:03:43 -06001476 BMCWEB_LOG_DEBUG("set DomainEnabled for dhcp6...");
1477 setDHCPConfig("DomainEnabled", nextUsev6Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001478 NetworkType::dhcp6);
Ed Tanousbf648f72021-06-03 15:00:14 -07001479}
1480
Ed Tanous77179532023-02-28 10:45:28 -08001481inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1482 const std::vector<IPv4AddressData>::const_iterator& head,
1483 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001484{
1485 return std::find_if(head, end, [](const IPv4AddressData& value) {
1486 return value.origin == "Static";
1487 });
1488}
1489
Ed Tanous77179532023-02-28 10:45:28 -08001490inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1491 const std::vector<IPv6AddressData>::const_iterator& head,
1492 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001493{
1494 return std::find_if(head, end, [](const IPv6AddressData& value) {
1495 return value.origin == "Static";
1496 });
1497}
1498
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001499enum class AddrChange
Ed Tanousbf648f72021-06-03 15:00:14 -07001500{
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001501 Noop,
1502 Delete,
1503 Update,
1504};
1505
1506// Struct representing a dbus change
1507struct AddressPatch
1508{
1509 std::string address;
1510 std::string gateway;
1511 uint8_t prefixLength = 0;
1512 std::string existingDbusId;
1513 AddrChange operation = AddrChange::Noop;
1514};
1515
1516inline bool parseAddresses(
1517 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1518 const std::vector<IPv4AddressData>& ipv4Data, crow::Response& res,
1519 std::vector<AddressPatch>& addressesOut, std::string& gatewayOut)
1520{
Ed Tanous77179532023-02-28 10:45:28 -08001521 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001522 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1523
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001524 std::string lastGatewayPath;
1525 size_t entryIdx = 0;
Ed Tanous3dfed532024-03-06 14:41:27 -08001526 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1527 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001528 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001529 std::string pathString =
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001530 std::format("IPv4StaticAddresses/{}", entryIdx);
1531 AddressPatch& thisAddress = addressesOut.emplace_back();
Ed Tanous3dfed532024-03-06 14:41:27 -08001532 nlohmann::json::object_t* obj =
1533 std::get_if<nlohmann::json::object_t>(&thisJson);
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001534 if (nicIpEntry != ipv4Data.cend())
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001535 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001536 thisAddress.existingDbusId = nicIpEntry->id;
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001537 }
1538
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001539 if (obj == nullptr)
1540 {
1541 if (thisAddress.existingDbusId.empty())
1542 {
1543 // Received a DELETE action on an entry not assigned to the NIC
1544 messages::resourceCannotBeDeleted(res);
1545 return false;
1546 }
1547 thisAddress.operation = AddrChange::Delete;
1548 }
1549 else
Ed Tanousbf648f72021-06-03 15:00:14 -07001550 {
1551 std::optional<std::string> address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001552 std::optional<std::string> gateway;
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001553 std::optional<std::string> subnetMask;
1554 if (!obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001555 {
Patrick Williams504af5a2025-02-03 14:29:03 -05001556 if (!json_util::readJsonObject( //
1557 *obj, res, //
1558 "Address", address, //
1559 "Gateway", gateway, //
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001560 "SubnetMask", subnetMask //
1561 ))
1562 {
1563 messages::propertyValueFormatError(res, *obj, pathString);
1564 return false;
1565 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001566 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001567 // Find the address/subnet/gateway values. Any values that are
1568 // not explicitly provided are assumed to be unmodified from the
1569 // current state of the interface. Merge existing state into the
1570 // current request.
Ed Tanousbf648f72021-06-03 15:00:14 -07001571 if (address)
1572 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001573 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001574 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001575 messages::propertyValueFormatError(res, *address,
Ed Tanousbf648f72021-06-03 15:00:14 -07001576 pathString + "/Address");
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001577 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001578 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001579 thisAddress.operation = AddrChange::Update;
1580 thisAddress.address = *address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001581 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001582 else if (thisAddress.existingDbusId.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001583 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001584 messages::propertyMissing(res, pathString + "/Address");
1585 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001586 }
1587 else
1588 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001589 thisAddress.address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001590 }
1591
1592 if (subnetMask)
1593 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001594 uint8_t prefixLength = 0;
Ed Tanous033f1e42022-08-15 09:47:37 -07001595 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1596 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001597 {
1598 messages::propertyValueFormatError(
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001599 res, *subnetMask, pathString + "/SubnetMask");
1600 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001601 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001602 thisAddress.prefixLength = prefixLength;
1603 thisAddress.operation = AddrChange::Update;
Ed Tanousbf648f72021-06-03 15:00:14 -07001604 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001605 else if (thisAddress.existingDbusId.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001606 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001607 messages::propertyMissing(res, pathString + "/SubnetMask");
1608 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001609 }
1610 else
1611 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001612 uint8_t prefixLength = 0;
1613 // Ignore return code. It came from internal, it's it's invalid
1614 // nothing we can do
1615 ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1616 &prefixLength);
Ed Tanousbf648f72021-06-03 15:00:14 -07001617
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001618 thisAddress.prefixLength = prefixLength;
1619 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001620 if (gateway)
1621 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001622 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001623 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001624 messages::propertyValueFormatError(res, *gateway,
Ed Tanousbf648f72021-06-03 15:00:14 -07001625 pathString + "/Gateway");
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001626 return false;
Ed Tanousbf648f72021-06-03 15:00:14 -07001627 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001628 thisAddress.operation = AddrChange::Update;
1629 thisAddress.gateway = *gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001630 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001631 else if (thisAddress.existingDbusId.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001632 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001633 // Default to null gateway
1634 gateway = "";
Ed Tanousbf648f72021-06-03 15:00:14 -07001635 }
1636 else
1637 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001638 thisAddress.gateway = nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001639 }
1640
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001641 // Changing gateway from existing
1642 if (!thisAddress.gateway.empty() &&
1643 thisAddress.gateway != "0.0.0.0")
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001644 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001645 if (!gatewayOut.empty() && gatewayOut != thisAddress.gateway)
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001646 {
1647 // A NIC can only have a single active gateway value.
1648 // If any gateway in the array of static addresses
1649 // mismatch the PATCH is in error.
1650 std::string arg1 = pathString + "/Gateway";
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001651 std::string arg2 = lastGatewayPath + "/Gateway";
1652 messages::propertyValueConflict(res, arg1, arg2);
1653 return false;
1654 }
1655 gatewayOut = thisAddress.gateway;
1656 lastGatewayPath = pathString;
1657 }
1658 }
1659 nicIpEntry++;
1660 nicIpEntry = getNextStaticIpEntry(nicIpEntry, ipv4Data.cend());
1661 entryIdx++;
1662 }
1663
1664 // Delete the remaining IPs
1665 while (nicIpEntry != ipv4Data.cend())
1666 {
1667 AddressPatch& thisAddress = addressesOut.emplace_back();
1668 thisAddress.operation = AddrChange::Delete;
1669 thisAddress.existingDbusId = nicIpEntry->id;
1670 nicIpEntry++;
1671 nicIpEntry = getNextStaticIpEntry(nicIpEntry, ipv4Data.cend());
1672 }
1673
1674 return true;
1675}
1676
1677inline void handleIPv4StaticPatch(
1678 const std::string& ifaceId,
1679 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1680 const EthernetInterfaceData& ethData,
1681 const std::vector<IPv4AddressData>& ipv4Data,
1682 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1683{
1684 std::vector<AddressPatch> addresses;
1685 std::string gatewayOut;
1686 if (!parseAddresses(input, ipv4Data, asyncResp->res, addresses, gatewayOut))
1687 {
1688 return;
1689 }
1690
1691 // If we're setting the gateway to something new, delete the
1692 // existing so we won't conflict
1693 if (!ethData.defaultGateway.empty() && ethData.defaultGateway != gatewayOut)
1694 {
1695 updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1696 }
1697
1698 for (const AddressPatch& address : addresses)
1699 {
1700 switch (address.operation)
1701 {
1702 case AddrChange::Delete:
1703 {
1704 BMCWEB_LOG_ERROR("Deleting id {} on interface {}",
1705 address.existingDbusId, ifaceId);
1706 deleteIPAddress(ifaceId, address.existingDbusId, asyncResp);
1707 }
1708 break;
1709 case AddrChange::Update:
1710 {
1711 // Update is a delete then a recreate
1712 // Only need to update if there is an existing ip at this index
1713 if (!address.existingDbusId.empty())
1714 {
1715 BMCWEB_LOG_ERROR("Deleting id {} on interface {}",
1716 address.existingDbusId, ifaceId);
1717 deleteAndCreateIPAddress(
1718 IpVersion::IpV4, ifaceId, address.existingDbusId,
1719 address.prefixLength, address.address, address.gateway,
1720 asyncResp);
1721 }
1722 else
1723 {
1724 // Otherwise, just create a new one
1725 BMCWEB_LOG_ERROR(
1726 "creating ip {} prefix {} gateway {} on interface {}",
1727 address.address, address.prefixLength, address.gateway,
1728 ifaceId);
1729 createIPv4(ifaceId, address.prefixLength, address.gateway,
1730 address.address, asyncResp);
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001731 }
1732 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001733 break;
1734 default:
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001735 {
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001736 // Leave alone
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001737 }
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001738 break;
1739 }
1740 }
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001741
Ed Tanous6e1a52f2024-11-15 19:44:16 -08001742 // now update to the new gateway.
1743 // Default gateway is already empty, so no need to update if we're clearing
1744 if (!gatewayOut.empty() && ethData.defaultGateway != gatewayOut)
1745 {
1746 updateIPv4DefaultGateway(ifaceId, gatewayOut, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001747 }
1748}
1749
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001750inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001751 const std::string& ifaceId,
1752 const std::vector<std::string>& updatedStaticNameServers,
1753 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1754{
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001755 setDbusProperty(
Ginu Georgee93abac2024-06-14 17:35:27 +05301756 asyncResp, "StaticNameServers", "xyz.openbmc_project.Network",
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001757 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1758 ifaceId,
George Liu9ae226f2023-06-21 17:56:46 +08001759 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ginu Georgee93abac2024-06-14 17:35:27 +05301760 updatedStaticNameServers);
Ed Tanousbf648f72021-06-03 15:00:14 -07001761}
1762
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001763inline void handleIPv6StaticAddressesPatch(
Ed Tanous3dfed532024-03-06 14:41:27 -08001764 const std::string& ifaceId,
1765 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Ed Tanous77179532023-02-28 10:45:28 -08001766 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001767 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1768{
Ed Tanousbf648f72021-06-03 15:00:14 -07001769 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001770 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001771 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanous3dfed532024-03-06 14:41:27 -08001772 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1773 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001774 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001775 std::string pathString =
1776 "IPv6StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001777 nlohmann::json::object_t* obj =
1778 std::get_if<nlohmann::json::object_t>(&thisJson);
1779 if (obj != nullptr && !obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001780 {
1781 std::optional<std::string> address;
1782 std::optional<uint8_t> prefixLength;
Ed Tanous3dfed532024-03-06 14:41:27 -08001783 nlohmann::json::object_t thisJsonCopy = *obj;
Patrick Williams504af5a2025-02-03 14:29:03 -05001784 if (!json_util::readJsonObject( //
Myung Baeafc474a2024-10-09 00:53:29 -07001785 thisJsonCopy, asyncResp->res, //
Patrick Williams504af5a2025-02-03 14:29:03 -05001786 "Address", address, //
1787 "PrefixLength", prefixLength //
Myung Baeafc474a2024-10-09 00:53:29 -07001788 ))
Ed Tanousbf648f72021-06-03 15:00:14 -07001789 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001790 messages::propertyValueFormatError(asyncResp->res, thisJsonCopy,
Ed Tanousf818b042022-06-27 13:17:35 -07001791 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001792 return;
1793 }
1794
Ed Tanousbf648f72021-06-03 15:00:14 -07001795 // Find the address and prefixLength values. Any values that are
1796 // not explicitly provided are assumed to be unmodified from the
1797 // current state of the interface. Merge existing state into the
1798 // current request.
Ed Tanousd547d8d2024-03-16 18:04:41 -07001799 if (!address)
Ed Tanousbf648f72021-06-03 15:00:14 -07001800 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001801 if (nicIpEntry == ipv6Data.end())
1802 {
1803 messages::propertyMissing(asyncResp->res,
1804 pathString + "/Address");
1805 return;
1806 }
1807 address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001808 }
1809
Ed Tanousd547d8d2024-03-16 18:04:41 -07001810 if (!prefixLength)
Ed Tanousbf648f72021-06-03 15:00:14 -07001811 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001812 if (nicIpEntry == ipv6Data.end())
1813 {
1814 messages::propertyMissing(asyncResp->res,
1815 pathString + "/PrefixLength");
1816 return;
1817 }
1818 prefixLength = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001819 }
1820
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001821 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001822 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001823 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
Ed Tanousd547d8d2024-03-16 18:04:41 -07001824 nicIpEntry->id, *prefixLength,
1825 *address, "", asyncResp);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001826 nicIpEntry =
1827 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001828 }
1829 else
1830 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001831 createIPv6(ifaceId, *prefixLength, *address, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001832 }
1833 entryIdx++;
1834 }
1835 else
1836 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001837 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001838 {
1839 // Requesting a DELETE/DO NOT MODIFY action for an item
1840 // that isn't present on the eth(n) interface. Input JSON is
1841 // in error, so bail out.
Ed Tanous3dfed532024-03-06 14:41:27 -08001842 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001843 {
1844 messages::resourceCannotBeDeleted(asyncResp->res);
1845 return;
1846 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001847 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001848 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001849 return;
1850 }
1851
Ed Tanous3dfed532024-03-06 14:41:27 -08001852 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001853 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001854 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001855 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001856 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001857 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001858 nicIpEntry =
1859 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001860 }
1861 entryIdx++;
1862 }
1863 }
1864}
1865
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001866inline std::string extractParentInterfaceName(const std::string& ifaceId)
1867{
1868 std::size_t pos = ifaceId.find('_');
1869 return ifaceId.substr(0, pos);
1870}
1871
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001872inline void parseInterfaceData(
1873 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1874 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1875 const std::vector<IPv4AddressData>& ipv4Data,
1876 const std::vector<IPv6AddressData>& ipv6Data,
1877 const std::vector<StaticGatewayData>& ipv6GatewayData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001878{
Ed Tanousbf648f72021-06-03 15:00:14 -07001879 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1880 jsonResponse["Id"] = ifaceId;
Ed Tanous253f11b2024-05-16 09:38:31 -07001881 jsonResponse["@odata.id"] =
1882 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
1883 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001884 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1885
Ed Tanousbf648f72021-06-03 15:00:14 -07001886 if (ethData.nicEnabled)
1887 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001888 jsonResponse["LinkStatus"] =
1889 ethData.linkUp ? ethernet_interface::LinkStatus::LinkUp
1890 : ethernet_interface::LinkStatus::LinkDown;
1891 jsonResponse["Status"]["State"] = resource::State::Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001892 }
1893 else
1894 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001895 jsonResponse["LinkStatus"] = ethernet_interface::LinkStatus::NoLink;
1896 jsonResponse["Status"]["State"] = resource::State::Disabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001897 }
1898
Ed Tanousbf648f72021-06-03 15:00:14 -07001899 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301900 jsonResponse["MTUSize"] = ethData.mtuSize;
Asmitha Karunanithi4652c642024-07-30 11:35:53 -05001901 if (ethData.macAddress)
1902 {
1903 jsonResponse["MACAddress"] = *ethData.macAddress;
1904 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001905 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001906 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
Jishnu CMe4588152023-05-11 00:04:40 -05001907 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1908 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
Ravi Tejade9ad762024-06-03 02:00:15 -05001909 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.domainv4Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001910 jsonResponse["DHCPv6"]["OperatingMode"] =
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001911 translateDhcpEnabledToBool(ethData.dhcpEnabled, false)
1912 ? "Enabled"
1913 : "Disabled";
Jishnu CMe4588152023-05-11 00:04:40 -05001914 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1915 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
Ravi Tejade9ad762024-06-03 02:00:15 -05001916 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.domainv6Enabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -05001917 jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1918 ethData.ipv6AcceptRa;
Ed Tanousbf648f72021-06-03 15:00:14 -07001919
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001920 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001921 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001922 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001923
1924 // When domain name is empty then it means, that it is a network
1925 // without domain names, and the host name itself must be treated as
1926 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001927 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001928 if (!ethData.domainnames.empty())
1929 {
1930 fqdn += "." + ethData.domainnames[0];
1931 }
1932 jsonResponse["FQDN"] = fqdn;
1933 }
1934
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001935 if (ethData.vlanId)
1936 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001937 jsonResponse["EthernetInterfaceType"] =
1938 ethernet_interface::EthernetDeviceType::Virtual;
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001939 jsonResponse["VLAN"]["VLANEnable"] = true;
1940 jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1941 jsonResponse["VLAN"]["Tagged"] = true;
1942
1943 nlohmann::json::array_t relatedInterfaces;
1944 nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1945 parentInterface["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001946 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1947 BMCWEB_REDFISH_MANAGER_URI_NAME,
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001948 extractParentInterfaceName(ifaceId));
1949 jsonResponse["Links"]["RelatedInterfaces"] =
1950 std::move(relatedInterfaces);
1951 }
1952 else
1953 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001954 jsonResponse["EthernetInterfaceType"] =
1955 ethernet_interface::EthernetDeviceType::Physical;
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001956 }
1957
Ed Tanousbf648f72021-06-03 15:00:14 -07001958 jsonResponse["NameServers"] = ethData.nameServers;
1959 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1960
1961 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1962 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1963 ipv4Array = nlohmann::json::array();
1964 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001965 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001966 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001967 std::string gatewayStr = ipv4Config.gateway;
1968 if (gatewayStr.empty())
1969 {
1970 gatewayStr = "0.0.0.0";
1971 }
Ed Tanous14766872022-03-15 10:44:42 -07001972 nlohmann::json::object_t ipv4;
1973 ipv4["AddressOrigin"] = ipv4Config.origin;
1974 ipv4["SubnetMask"] = ipv4Config.netmask;
1975 ipv4["Address"] = ipv4Config.address;
1976 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001977
Ed Tanousbf648f72021-06-03 15:00:14 -07001978 if (ipv4Config.origin == "Static")
1979 {
Ed Tanous14766872022-03-15 10:44:42 -07001980 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001981 }
Ed Tanous14766872022-03-15 10:44:42 -07001982
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001983 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001984 }
1985
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001986 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001987 if (ipv6GatewayStr.empty())
1988 {
1989 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1990 }
1991
1992 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1993
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001994 nlohmann::json::array_t ipv6StaticGatewayArray;
1995 for (const auto& ipv6GatewayConfig : ipv6GatewayData)
1996 {
1997 nlohmann::json::object_t ipv6Gateway;
1998 ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001999 ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
2000 }
2001 jsonResponse["IPv6StaticDefaultGateways"] =
2002 std::move(ipv6StaticGatewayArray);
2003
Ed Tanousbf648f72021-06-03 15:00:14 -07002004 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
2005 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
2006 ipv6Array = nlohmann::json::array();
2007 ipv6StaticArray = nlohmann::json::array();
2008 nlohmann::json& ipv6AddrPolicyTable =
2009 jsonResponse["IPv6AddressPolicyTable"];
2010 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08002011 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07002012 {
Ed Tanous14766872022-03-15 10:44:42 -07002013 nlohmann::json::object_t ipv6;
2014 ipv6["Address"] = ipv6Config.address;
2015 ipv6["PrefixLength"] = ipv6Config.prefixLength;
2016 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05002017
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002018 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07002019 if (ipv6Config.origin == "Static")
2020 {
Ed Tanous14766872022-03-15 10:44:42 -07002021 nlohmann::json::object_t ipv6Static;
2022 ipv6Static["Address"] = ipv6Config.address;
2023 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002024 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07002025 }
2026 }
2027}
2028
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002029inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2030 const std::string& ifaceId,
2031 const boost::system::error_code& ec,
2032 const sdbusplus::message_t& m)
2033{
2034 if (!ec)
2035 {
2036 return;
2037 }
2038 const sd_bus_error* dbusError = m.get_error();
2039 if (dbusError == nullptr)
2040 {
2041 messages::internalError(asyncResp->res);
2042 return;
2043 }
Ed Tanous62598e32023-07-17 17:06:25 -07002044 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002045
2046 if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
2047 dbusError->name)
2048 {
2049 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2050 ifaceId);
2051 return;
2052 }
2053 if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
2054 dbusError->name)
2055 {
2056 messages::resourceCannotBeDeleted(asyncResp->res);
2057 return;
2058 }
2059 messages::internalError(asyncResp->res);
2060}
2061
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002062inline void afterVlanCreate(
2063 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2064 const std::string& parentInterfaceUri, const std::string& vlanInterface,
2065 const boost::system::error_code& ec, const sdbusplus::message_t& m
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002066
2067)
2068{
2069 if (ec)
2070 {
2071 const sd_bus_error* dbusError = m.get_error();
2072 if (dbusError == nullptr)
2073 {
2074 messages::internalError(asyncResp->res);
2075 return;
2076 }
Ed Tanous62598e32023-07-17 17:06:25 -07002077 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002078
2079 if (std::string_view(
2080 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
2081 dbusError->name)
2082 {
2083 messages::propertyValueNotInList(
2084 asyncResp->res, parentInterfaceUri,
2085 "Links/RelatedInterfaces/0/@odata.id");
2086 return;
2087 }
2088 if (std::string_view(
2089 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
2090 dbusError->name)
2091 {
2092 messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
2093 "Id", vlanInterface);
2094 return;
2095 }
2096 messages::internalError(asyncResp->res);
2097 return;
2098 }
2099
Ed Tanous253f11b2024-05-16 09:38:31 -07002100 const boost::urls::url vlanInterfaceUri =
2101 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2102 BMCWEB_REDFISH_MANAGER_URI_NAME, vlanInterface);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002103 asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
2104}
2105
Ed Tanousbf648f72021-06-03 15:00:14 -07002106inline void requestEthernetInterfacesRoutes(App& app)
2107{
Ed Tanous253f11b2024-05-16 09:38:31 -07002108 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07002109 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002110 .methods(boost::beast::http::verb::get)(
2111 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002112 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2113 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002114 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2115 {
2116 return;
2117 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002118
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002119 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2120 {
2121 messages::resourceNotFound(asyncResp->res, "Manager",
2122 managerId);
2123 return;
2124 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002125
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002126 asyncResp->res.jsonValue["@odata.type"] =
2127 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2128 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2129 "/redfish/v1/Managers/{}/EthernetInterfaces",
2130 BMCWEB_REDFISH_MANAGER_URI_NAME);
2131 asyncResp->res.jsonValue["Name"] =
2132 "Ethernet Network Interface Collection";
2133 asyncResp->res.jsonValue["Description"] =
2134 "Collection of EthernetInterfaces for this Manager";
2135
2136 // Get eth interface list, and call the below callback for JSON
2137 // preparation
2138 getEthernetIfaceList(
2139 [asyncResp](const bool& success,
2140 const std::vector<std::string>& ifaceList) {
2141 if (!success)
2142 {
2143 messages::internalError(asyncResp->res);
2144 return;
2145 }
2146
2147 nlohmann::json& ifaceArray =
2148 asyncResp->res.jsonValue["Members"];
2149 ifaceArray = nlohmann::json::array();
2150 for (const std::string& ifaceItem : ifaceList)
2151 {
2152 nlohmann::json::object_t iface;
2153 iface["@odata.id"] = boost::urls::format(
2154 "/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2155 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceItem);
2156 ifaceArray.push_back(std::move(iface));
2157 }
2158
2159 asyncResp->res.jsonValue["Members@odata.count"] =
2160 ifaceArray.size();
2161 asyncResp->res.jsonValue["@odata.id"] =
2162 boost::urls::format(
2163 "/redfish/v1/Managers/{}/EthernetInterfaces",
Ed Tanous253f11b2024-05-16 09:38:31 -07002164 BMCWEB_REDFISH_MANAGER_URI_NAME);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002165 });
2166 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002167
Ed Tanous253f11b2024-05-16 09:38:31 -07002168 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002169 .privileges(redfish::privileges::postEthernetInterfaceCollection)
2170 .methods(boost::beast::http::verb::post)(
2171 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002172 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2173 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002174 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2175 {
2176 return;
2177 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002178
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002179 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2180 {
2181 messages::resourceNotFound(asyncResp->res, "Manager",
2182 managerId);
2183 return;
2184 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002185
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002186 bool vlanEnable = false;
2187 uint32_t vlanId = 0;
2188 std::vector<nlohmann::json::object_t> relatedInterfaces;
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002189
Patrick Williams504af5a2025-02-03 14:29:03 -05002190 if (!json_util::readJsonPatch( //
2191 req, asyncResp->res, //
Myung Baeafc474a2024-10-09 00:53:29 -07002192 "Links/RelatedInterfaces", relatedInterfaces, //
Patrick Williams504af5a2025-02-03 14:29:03 -05002193 "VLAN/VLANEnable", vlanEnable, //
2194 "VLAN/VLANId", vlanId //
Myung Baeafc474a2024-10-09 00:53:29 -07002195 ))
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002196 {
2197 return;
2198 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002199
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002200 if (relatedInterfaces.size() != 1)
2201 {
2202 messages::arraySizeTooLong(asyncResp->res,
2203 "Links/RelatedInterfaces",
2204 relatedInterfaces.size());
2205 return;
2206 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002207
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002208 std::string parentInterfaceUri;
2209 if (!json_util::readJsonObject(relatedInterfaces[0],
2210 asyncResp->res, "@odata.id",
2211 parentInterfaceUri))
2212 {
2213 messages::propertyMissing(
2214 asyncResp->res, "Links/RelatedInterfaces/0/@odata.id");
2215 return;
2216 }
2217 BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002218
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002219 boost::system::result<boost::urls::url_view> parsedUri =
2220 boost::urls::parse_relative_ref(parentInterfaceUri);
2221 if (!parsedUri)
2222 {
2223 messages::propertyValueFormatError(
2224 asyncResp->res, parentInterfaceUri,
2225 "Links/RelatedInterfaces/0/@odata.id");
2226 return;
2227 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002228
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002229 std::string parentInterface;
2230 if (!crow::utility::readUrlSegments(
2231 *parsedUri, "redfish", "v1", "Managers", "bmc",
2232 "EthernetInterfaces", std::ref(parentInterface)))
2233 {
2234 messages::propertyValueNotInList(
2235 asyncResp->res, parentInterfaceUri,
2236 "Links/RelatedInterfaces/0/@odata.id");
2237 return;
2238 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002239
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002240 if (!vlanEnable)
2241 {
2242 // In OpenBMC implementation, VLANEnable cannot be false on
2243 // create
2244 messages::propertyValueIncorrect(
2245 asyncResp->res, "VLAN/VLANEnable", "false");
2246 return;
2247 }
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002248
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002249 std::string vlanInterface =
2250 parentInterface + "_" + std::to_string(vlanId);
2251 crow::connections::systemBus->async_method_call(
2252 [asyncResp, parentInterfaceUri,
2253 vlanInterface](const boost::system::error_code& ec,
2254 const sdbusplus::message_t& m) {
2255 afterVlanCreate(asyncResp, parentInterfaceUri,
2256 vlanInterface, ec, m);
2257 },
2258 "xyz.openbmc_project.Network",
2259 "/xyz/openbmc_project/network",
2260 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2261 parentInterface, vlanId);
2262 });
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002263
Ed Tanous253f11b2024-05-16 09:38:31 -07002264 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002265 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002266 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002267 [&app](const crow::Request& req,
2268 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002269 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002270 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2271 {
2272 return;
2273 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002274
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002275 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2276 {
2277 messages::resourceNotFound(asyncResp->res, "Manager",
2278 managerId);
2279 return;
2280 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002281
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002282 getEthernetIfaceData(
2283 ifaceId,
2284 [asyncResp, ifaceId](
2285 const bool& success,
2286 const EthernetInterfaceData& ethData,
2287 const std::vector<IPv4AddressData>& ipv4Data,
2288 const std::vector<IPv6AddressData>& ipv6Data,
2289 const std::vector<StaticGatewayData>& ipv6GatewayData) {
2290 if (!success)
2291 {
2292 // TODO(Pawel)consider distinguish between non
2293 // existing object, and other errors
2294 messages::resourceNotFound(
2295 asyncResp->res, "EthernetInterface", ifaceId);
2296 return;
2297 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002298
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002299 asyncResp->res.jsonValue["@odata.type"] =
2300 "#EthernetInterface.v1_9_0.EthernetInterface";
2301 asyncResp->res.jsonValue["Name"] =
2302 "Manager Ethernet Interface";
2303 asyncResp->res.jsonValue["Description"] =
2304 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05302305
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002306 parseInterfaceData(asyncResp, ifaceId, ethData,
2307 ipv4Data, ipv6Data, ipv6GatewayData);
2308 });
2309 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002310
Ed Tanous253f11b2024-05-16 09:38:31 -07002311 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002312 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002313 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002314 [&app](const crow::Request& req,
2315 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002316 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002317 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2318 {
2319 return;
2320 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002321
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002322 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2323 {
2324 messages::resourceNotFound(asyncResp->res, "Manager",
2325 managerId);
2326 return;
2327 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002328
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002329 std::optional<std::string> hostname;
2330 std::optional<std::string> fqdn;
2331 std::optional<std::string> macAddress;
2332 std::optional<std::string> ipv6DefaultGateway;
2333 std::optional<std::vector<
2334 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2335 ipv4StaticAddresses;
2336 std::optional<std::vector<
2337 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2338 ipv6StaticAddresses;
2339 std::optional<std::vector<
2340 std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2341 ipv6StaticDefaultGateways;
2342 std::optional<std::vector<std::string>> staticNameServers;
2343 std::optional<bool> ipv6AutoConfigEnabled;
2344 std::optional<bool> interfaceEnabled;
2345 std::optional<size_t> mtuSize;
2346 DHCPParameters v4dhcpParms;
2347 DHCPParameters v6dhcpParms;
Myung Baeafc474a2024-10-09 00:53:29 -07002348
Patrick Williams504af5a2025-02-03 14:29:03 -05002349 if (!json_util::readJsonPatch( //
2350 req, asyncResp->res, //
2351 "DHCPv4/DHCPEnabled", v4dhcpParms.dhcpv4Enabled, //
Myung Baeafc474a2024-10-09 00:53:29 -07002352 "DHCPv4/UseDNSServers", v4dhcpParms.useDnsServers, //
2353 "DHCPv4/UseDomainName", v4dhcpParms.useDomainName, //
2354 "DHCPv4/UseNTPServers", v4dhcpParms.useNtpServers, //
2355 "DHCPv6/OperatingMode",
Patrick Williams504af5a2025-02-03 14:29:03 -05002356 v6dhcpParms.dhcpv6OperatingMode, //
Myung Baeafc474a2024-10-09 00:53:29 -07002357 "DHCPv6/UseDNSServers", v6dhcpParms.useDnsServers, //
2358 "DHCPv6/UseDomainName", v6dhcpParms.useDomainName, //
2359 "DHCPv6/UseNTPServers", v6dhcpParms.useNtpServers, //
Patrick Williams504af5a2025-02-03 14:29:03 -05002360 "FQDN", fqdn, //
2361 "HostName", hostname, //
2362 "InterfaceEnabled", interfaceEnabled, //
2363 "IPv4StaticAddresses", ipv4StaticAddresses, //
2364 "IPv6DefaultGateway", ipv6DefaultGateway, //
2365 "IPv6StaticAddresses", ipv6StaticAddresses, //
Myung Baeafc474a2024-10-09 00:53:29 -07002366 "IPv6StaticDefaultGateways",
Patrick Williams504af5a2025-02-03 14:29:03 -05002367 ipv6StaticDefaultGateways, //
2368 "InterfaceEnabled", interfaceEnabled, //
2369 "MACAddress", macAddress, //
2370 "MTUSize", mtuSize, //
Myung Baeafc474a2024-10-09 00:53:29 -07002371 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
Patrick Williams504af5a2025-02-03 14:29:03 -05002372 ipv6AutoConfigEnabled, //
2373 "StaticNameServers", staticNameServers //
Myung Baeafc474a2024-10-09 00:53:29 -07002374 ))
2375 {
2376 return;
2377 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002378
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002379 // Get single eth interface data, and call the below callback
2380 // for JSON preparation
2381 getEthernetIfaceData(
2382 ifaceId,
2383 [asyncResp, ifaceId, hostname = std::move(hostname),
2384 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2385 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2386 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2387 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
2388 ipv6StaticDefaultGateway =
2389 std::move(ipv6StaticDefaultGateways),
2390 staticNameServers = std::move(staticNameServers), mtuSize,
2391 ipv6AutoConfigEnabled,
2392 v4dhcpParms = std::move(v4dhcpParms),
2393 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2394 const bool success,
2395 const EthernetInterfaceData& ethData,
2396 const std::vector<IPv4AddressData>& ipv4Data,
2397 const std::vector<IPv6AddressData>& ipv6Data,
2398 const std::vector<StaticGatewayData>&
2399 ipv6GatewayData) mutable {
2400 if (!success)
2401 {
2402 // ... otherwise return error
2403 // TODO(Pawel)consider distinguish between non
2404 // existing object, and other errors
2405 messages::resourceNotFound(
2406 asyncResp->res, "EthernetInterface", ifaceId);
2407 return;
2408 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002409
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002410 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2411 v6dhcpParms, asyncResp);
Tejas Patil35fb5312021-09-20 15:35:20 +05302412
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002413 if (hostname)
2414 {
2415 handleHostnamePatch(*hostname, asyncResp);
2416 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002417
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002418 if (ipv6AutoConfigEnabled)
2419 {
2420 handleSLAACAutoConfigPatch(
2421 ifaceId, *ipv6AutoConfigEnabled, asyncResp);
2422 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05002423
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002424 if (fqdn)
2425 {
2426 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2427 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002428
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002429 if (macAddress)
2430 {
2431 handleMACAddressPatch(ifaceId, *macAddress,
2432 asyncResp);
2433 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002434
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002435 if (ipv4StaticAddresses)
2436 {
2437 handleIPv4StaticPatch(ifaceId, *ipv4StaticAddresses,
2438 ethData, ipv4Data, asyncResp);
2439 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002440
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002441 if (staticNameServers)
2442 {
2443 handleStaticNameServersPatch(
2444 ifaceId, *staticNameServers, asyncResp);
2445 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002446
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002447 if (ipv6DefaultGateway)
2448 {
2449 messages::propertyNotWritable(asyncResp->res,
2450 "IPv6DefaultGateway");
2451 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002452
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002453 if (ipv6StaticAddresses)
2454 {
2455 handleIPv6StaticAddressesPatch(ifaceId,
2456 *ipv6StaticAddresses,
2457 ipv6Data, asyncResp);
2458 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002459
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002460 if (ipv6StaticDefaultGateway)
2461 {
2462 handleIPv6DefaultGateway(
2463 ifaceId, *ipv6StaticDefaultGateway,
2464 ipv6GatewayData, asyncResp);
2465 }
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002466
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002467 if (interfaceEnabled)
2468 {
2469 setDbusProperty(
2470 asyncResp, "InterfaceEnabled",
Ginu Georgee93abac2024-06-14 17:35:27 +05302471 "xyz.openbmc_project.Network",
Ed Tanousd02aad32024-02-13 14:43:34 -08002472 sdbusplus::message::object_path(
2473 "/xyz/openbmc_project/network") /
2474 ifaceId,
2475 "xyz.openbmc_project.Network.EthernetInterface",
Ginu Georgee93abac2024-06-14 17:35:27 +05302476 "NICEnabled", *interfaceEnabled);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002477 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002478
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002479 if (mtuSize)
2480 {
2481 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2482 }
2483 });
2484 });
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002485
Ed Tanous253f11b2024-05-16 09:38:31 -07002486 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002487 .privileges(redfish::privileges::deleteEthernetInterface)
2488 .methods(boost::beast::http::verb::delete_)(
2489 [&app](const crow::Request& req,
2490 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002491 const std::string& managerId, const std::string& ifaceId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002492 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2493 {
2494 return;
2495 }
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002496
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002497 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2498 {
2499 messages::resourceNotFound(asyncResp->res, "Manager",
2500 managerId);
2501 return;
2502 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002503
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002504 crow::connections::systemBus->async_method_call(
2505 [asyncResp, ifaceId](const boost::system::error_code& ec,
2506 const sdbusplus::message_t& m) {
2507 afterDelete(asyncResp, ifaceId, ec, m);
2508 },
2509 "xyz.openbmc_project.Network",
2510 std::string("/xyz/openbmc_project/network/") + ifaceId,
2511 "xyz.openbmc_project.Object.Delete", "Delete");
2512 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002513}
2514
Ed Tanous1abe55e2018-09-05 08:30:59 -07002515} // namespace redfish