blob: cb7bfb9f760193cea6afd27ee7b0242ee128c041 [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "error_messages.hpp"
Ed Tanous2c5875a2023-05-15 09:56:06 -070022#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "query.hpp"
24#include "registries/privilege_registry.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070025#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070027
Sunitha Harishce73d5c2023-04-07 06:46:49 -050028#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070029#include <boost/url/format.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030
George Liu7a1dbc42022-12-07 16:03:22 +080031#include <array>
Ed Tanous3dfed532024-03-06 14:41:27 -080032#include <cstddef>
Sunitha Harishce73d5c2023-04-07 06:46:49 -050033#include <memory>
Ed Tanousa24526d2018-12-10 15:17:59 -080034#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070035#include <ranges>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053036#include <regex>
George Liu7a1dbc42022-12-07 16:03:22 +080037#include <string_view>
Ed Tanous3dfed532024-03-06 14:41:27 -080038#include <variant>
Ed Tanous77179532023-02-28 10:45:28 -080039#include <vector>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010040
Ed Tanous1abe55e2018-09-05 08:30:59 -070041namespace redfish
42{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010043
Ed Tanous4a0cb852018-10-15 07:55:04 -070044enum class LinkType
45{
46 Local,
47 Global
48};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010049
Johnathan Mantey743eb1c2024-04-03 12:05:57 -070050enum class IpVersion
51{
52 IpV4,
53 IpV6
54};
55
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010056/**
57 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010058 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070059struct IPv4AddressData
60{
61 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070062 std::string address;
63 std::string domain;
64 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 std::string netmask;
66 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080067 LinkType linktype{};
68 bool isActive{};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010069};
70
71/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050072 * Structure for keeping IPv6 data required by Redfish
73 */
74struct IPv6AddressData
75{
76 std::string id;
77 std::string address;
78 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080079 uint8_t prefixLength = 0;
Ravi Tejae48c0fc2019-04-16 08:37:20 -050080};
Sunitha Harishce73d5c2023-04-07 06:46:49 -050081
82/**
83 * Structure for keeping static route data required by Redfish
84 */
85struct StaticGatewayData
86{
87 std::string id;
88 std::string gateway;
89 size_t prefixLength = 0;
90 std::string protocol;
91};
92
Ravi Tejae48c0fc2019-04-16 08:37:20 -050093/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010094 * Structure for keeping basic single Ethernet Interface information
95 * available from DBus
96 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070097struct EthernetInterfaceData
98{
Ed Tanous4a0cb852018-10-15 07:55:04 -070099 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +0530100 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800101 bool autoNeg;
Jishnu CMe4588152023-05-11 00:04:40 -0500102 bool dnsv4Enabled;
103 bool dnsv6Enabled;
Ravi Teja91c441e2024-02-23 09:03:43 -0600104 bool domainv4Enabled;
105 bool domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -0500106 bool ntpv4Enabled;
107 bool ntpv6Enabled;
108 bool hostNamev4Enabled;
109 bool hostNamev6Enabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800110 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700111 bool nicEnabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -0500112 bool ipv6AcceptRa;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800113 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700114 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800115 std::string hostName;
116 std::string defaultGateway;
117 std::string ipv6DefaultGateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500118 std::string ipv6StaticDefaultGateway;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800119 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800120 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500121 std::vector<std::string> nameServers;
122 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800123 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100124};
125
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700126struct DHCPParameters
127{
128 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800129 std::optional<bool> useDnsServers;
130 std::optional<bool> useNtpServers;
131 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700132 std::optional<std::string> dhcpv6OperatingMode;
133};
134
Ed Tanous4a0cb852018-10-15 07:55:04 -0700135// Helper function that changes bits netmask notation (i.e. /24)
136// into full dot notation
137inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700139 uint32_t value = 0xffffffff << (32 - bits);
140 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
141 std::to_string((value >> 16) & 0xff) + "." +
142 std::to_string((value >> 8) & 0xff) + "." +
143 std::to_string(value & 0xff);
144 return netmask;
145}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100146
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800147inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700148 bool isIPv4)
149{
150 if (isIPv4)
151 {
152 return (
153 (inputDHCP ==
154 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
155 (inputDHCP ==
156 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
157 }
158 return ((inputDHCP ==
159 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
160 (inputDHCP ==
161 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
162}
163
Ed Tanous2c70f802020-09-28 14:29:23 -0700164inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700165{
166 if (isIPv4 && isIPv6)
167 {
168 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
169 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700170 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700171 {
172 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
173 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700174 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700175 {
176 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
177 }
178 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
179}
180
Ed Tanous4a0cb852018-10-15 07:55:04 -0700181inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500182 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700183 bool isIPv4)
184{
185 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700187 return "Static";
188 }
189 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
190 {
191 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700192 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700193 return "IPv4LinkLocal";
194 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700195 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700196 }
197 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
198 {
199 if (isIPv4)
200 {
201 return "DHCP";
202 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700203 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700204 }
205 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
206 {
207 return "SLAAC";
208 }
209 return "";
210}
211
Ed Tanous02cad962022-06-30 16:50:15 -0700212inline bool extractEthernetInterfaceData(
213 const std::string& ethifaceId,
214 const dbus::utility::ManagedObjectType& dbusData,
215 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700217 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700218 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 {
Ed Tanous02cad962022-06-30 16:50:15 -0700220 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700221 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000222 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700224 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700225 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500227 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500231 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800232 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 if (mac != nullptr)
234 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800235 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700236 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700238 }
239 }
240 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
241 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500242 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 {
244 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500246 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800247 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700248 if (id != nullptr)
249 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800250 ethData.vlanId = *id;
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 ==
256 "xyz.openbmc_project.Network.EthernetInterface")
257 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500258 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700259 {
260 if (propertyPair.first == "AutoNeg")
261 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700262 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800263 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700264 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700265 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800266 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700267 }
268 }
269 else if (propertyPair.first == "Speed")
270 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500271 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800272 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700273 if (speed != nullptr)
274 {
275 ethData.speed = *speed;
276 }
277 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530278 else if (propertyPair.first == "MTU")
279 {
Anthony3e7a8da2023-10-23 14:22:43 +0800280 const size_t* mtuSize =
281 std::get_if<size_t>(&propertyPair.second);
Tejas Patil35fb5312021-09-20 15:35:20 +0530282 if (mtuSize != nullptr)
283 {
284 ethData.mtuSize = *mtuSize;
285 }
286 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800287 else if (propertyPair.first == "LinkUp")
288 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500289 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800290 std::get_if<bool>(&propertyPair.second);
291 if (linkUp != nullptr)
292 {
293 ethData.linkUp = *linkUp;
294 }
295 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700296 else if (propertyPair.first == "NICEnabled")
297 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500298 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700299 std::get_if<bool>(&propertyPair.second);
300 if (nicEnabled != nullptr)
301 {
302 ethData.nicEnabled = *nicEnabled;
303 }
304 }
Ravi Tejab10d8db2022-05-24 09:04:12 -0500305 else if (propertyPair.first == "IPv6AcceptRA")
306 {
307 const bool* ipv6AcceptRa =
308 std::get_if<bool>(&propertyPair.second);
309 if (ipv6AcceptRa != nullptr)
310 {
311 ethData.ipv6AcceptRa = *ipv6AcceptRa;
312 }
313 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500314 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700315 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500316 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500317 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800318 &propertyPair.second);
319 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700320 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700321 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500322 }
323 }
324 else if (propertyPair.first == "StaticNameServers")
325 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500326 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500327 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500328 &propertyPair.second);
329 if (staticNameServers != nullptr)
330 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700331 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700332 }
333 }
manojkiraneda2a133282019-02-19 13:09:43 +0530334 else if (propertyPair.first == "DHCPEnabled")
335 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700336 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700337 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700338 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530339 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800340 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530341 }
342 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800343 else if (propertyPair.first == "DomainName")
344 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500345 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500346 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800347 &propertyPair.second);
348 if (domainNames != nullptr)
349 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700350 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800351 }
352 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500353 else if (propertyPair.first == "DefaultGateway")
354 {
355 const std::string* defaultGateway =
356 std::get_if<std::string>(&propertyPair.second);
357 if (defaultGateway != nullptr)
358 {
359 std::string defaultGatewayStr = *defaultGateway;
360 if (defaultGatewayStr.empty())
361 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800362 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500363 }
364 else
365 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800366 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500367 }
368 }
369 }
370 else if (propertyPair.first == "DefaultGateway6")
371 {
372 const std::string* defaultGateway6 =
373 std::get_if<std::string>(&propertyPair.second);
374 if (defaultGateway6 != nullptr)
375 {
376 std::string defaultGateway6Str =
377 *defaultGateway6;
378 if (defaultGateway6Str.empty())
379 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800380 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500381 "0:0:0:0:0:0:0:0";
382 }
383 else
384 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800385 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500386 defaultGateway6Str;
387 }
388 }
389 }
Ed Tanous029573d2019-02-01 10:57:49 -0800390 }
391 }
392 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700393
Jishnu CMe4588152023-05-11 00:04:40 -0500394 sdbusplus::message::object_path path(
395 "/xyz/openbmc_project/network");
396 sdbusplus::message::object_path dhcp4Path = path / ethifaceId /
397 "dhcp4";
398
399 if (sdbusplus::message::object_path(objpath.first) == dhcp4Path)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700400 {
401 if (ifacePair.first ==
402 "xyz.openbmc_project.Network.DHCPConfiguration")
403 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500404 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700405 {
406 if (propertyPair.first == "DNSEnabled")
407 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700408 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700409 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700410 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700411 {
Jishnu CMe4588152023-05-11 00:04:40 -0500412 ethData.dnsv4Enabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700413 }
414 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600415 else if (propertyPair.first == "DomainEnabled")
416 {
417 const bool* domainEnabled =
418 std::get_if<bool>(&propertyPair.second);
419 if (domainEnabled != nullptr)
420 {
421 ethData.domainv4Enabled = *domainEnabled;
422 }
423 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700424 else if (propertyPair.first == "NTPEnabled")
425 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700426 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700427 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700428 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700429 {
Jishnu CMe4588152023-05-11 00:04:40 -0500430 ethData.ntpv4Enabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700431 }
432 }
433 else if (propertyPair.first == "HostNameEnabled")
434 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700435 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700436 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700437 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700438 {
Jishnu CMe4588152023-05-11 00:04:40 -0500439 ethData.hostNamev4Enabled = *hostNameEnabled;
440 }
441 }
442 }
443 }
444 }
445
446 sdbusplus::message::object_path dhcp6Path = path / ethifaceId /
447 "dhcp6";
448
449 if (sdbusplus::message::object_path(objpath.first) == dhcp6Path)
450 {
451 if (ifacePair.first ==
452 "xyz.openbmc_project.Network.DHCPConfiguration")
453 {
454 for (const auto& propertyPair : ifacePair.second)
455 {
456 if (propertyPair.first == "DNSEnabled")
457 {
458 const bool* dnsEnabled =
459 std::get_if<bool>(&propertyPair.second);
460 if (dnsEnabled != nullptr)
461 {
462 ethData.dnsv6Enabled = *dnsEnabled;
463 }
464 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600465 if (propertyPair.first == "DomainEnabled")
466 {
467 const bool* domainEnabled =
468 std::get_if<bool>(&propertyPair.second);
469 if (domainEnabled != nullptr)
470 {
471 ethData.domainv6Enabled = *domainEnabled;
472 }
473 }
Jishnu CMe4588152023-05-11 00:04:40 -0500474 else if (propertyPair.first == "NTPEnabled")
475 {
476 const bool* ntpEnabled =
477 std::get_if<bool>(&propertyPair.second);
478 if (ntpEnabled != nullptr)
479 {
480 ethData.ntpv6Enabled = *ntpEnabled;
481 }
482 }
483 else if (propertyPair.first == "HostNameEnabled")
484 {
485 const bool* hostNameEnabled =
486 std::get_if<bool>(&propertyPair.second);
487 if (hostNameEnabled != nullptr)
488 {
489 ethData.hostNamev6Enabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700490 }
491 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700492 }
493 }
494 }
Ed Tanous029573d2019-02-01 10:57:49 -0800495 // System configuration shows up in the global namespace, so no need
496 // to check eth number
497 if (ifacePair.first ==
498 "xyz.openbmc_project.Network.SystemConfiguration")
499 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500500 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800501 {
502 if (propertyPair.first == "HostName")
503 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500504 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500505 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800506 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700507 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800508 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800509 }
510 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700511 }
512 }
513 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700514 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700515 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700516}
517
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500518// Helper function that extracts data for single ethernet ipv6 address
Ed Tanous77179532023-02-28 10:45:28 -0800519inline void extractIPV6Data(const std::string& ethifaceId,
520 const dbus::utility::ManagedObjectType& dbusData,
521 std::vector<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500522{
Patrick Williams89492a12023-05-10 07:51:34 -0500523 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
524 ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500525
526 // Since there might be several IPv6 configurations aligned with
527 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000528 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500529 {
530 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800531 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500532 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800533 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500534 {
535 if (interface.first == "xyz.openbmc_project.Network.IP")
536 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700537 auto type = std::ranges::find_if(interface.second,
538 [](const auto& property) {
Tony Lee353163e2022-11-23 11:06:10 +0800539 return property.first == "Type";
540 });
541 if (type == interface.second.end())
542 {
543 continue;
544 }
545
546 const std::string* typeStr =
547 std::get_if<std::string>(&type->second);
548
549 if (typeStr == nullptr ||
550 (*typeStr !=
551 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
552 {
553 continue;
554 }
555
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500556 // Instance IPv6AddressData structure, and set as
557 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800558 IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700559 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800560 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800561 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500562 {
563 if (property.first == "Address")
564 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500565 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500566 std::get_if<std::string>(&property.second);
567 if (address != nullptr)
568 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700569 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500570 }
571 }
572 else if (property.first == "Origin")
573 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500574 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500575 std::get_if<std::string>(&property.second);
576 if (origin != nullptr)
577 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700578 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500579 translateAddressOriginDbusToRedfish(*origin,
580 false);
581 }
582 }
583 else if (property.first == "PrefixLength")
584 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500585 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500586 std::get_if<uint8_t>(&property.second);
587 if (prefix != nullptr)
588 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700589 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500590 }
591 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600592 else if (property.first == "Type" ||
593 property.first == "Gateway")
594 {
595 // Type & Gateway is not used
596 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500597 else
598 {
Ed Tanous62598e32023-07-17 17:06:25 -0700599 BMCWEB_LOG_ERROR(
600 "Got extra property: {} on the {} object",
601 property.first, objpath.first.str);
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500602 }
603 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500604 }
605 }
606 }
607 }
608}
609
Ed Tanous4a0cb852018-10-15 07:55:04 -0700610// Helper function that extracts data for single ethernet ipv4 address
Ed Tanous77179532023-02-28 10:45:28 -0800611inline void extractIPData(const std::string& ethifaceId,
612 const dbus::utility::ManagedObjectType& dbusData,
613 std::vector<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700614{
Patrick Williams89492a12023-05-10 07:51:34 -0500615 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
616 ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700617
618 // Since there might be several IPv4 configurations aligned with
619 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000620 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700621 {
622 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800623 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700624 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800625 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700626 {
627 if (interface.first == "xyz.openbmc_project.Network.IP")
628 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700629 auto type = std::ranges::find_if(interface.second,
630 [](const auto& property) {
Tony Lee353163e2022-11-23 11:06:10 +0800631 return property.first == "Type";
632 });
633 if (type == interface.second.end())
634 {
635 continue;
636 }
637
638 const std::string* typeStr =
639 std::get_if<std::string>(&type->second);
640
641 if (typeStr == nullptr ||
642 (*typeStr !=
643 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
644 {
645 continue;
646 }
647
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 // Instance IPv4AddressData structure, and set as
649 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800650 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700651 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800652 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800653 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700654 {
655 if (property.first == "Address")
656 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500657 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800658 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700659 if (address != nullptr)
660 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700661 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 }
663 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700664 else if (property.first == "Origin")
665 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500666 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800667 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700668 if (origin != nullptr)
669 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700670 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700671 translateAddressOriginDbusToRedfish(*origin,
672 true);
673 }
674 }
675 else if (property.first == "PrefixLength")
676 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500677 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800678 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 if (mask != nullptr)
680 {
681 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700682 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700683 }
684 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600685 else if (property.first == "Type" ||
686 property.first == "Gateway")
687 {
688 // Type & Gateway is not used
689 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700690 else
691 {
Ed Tanous62598e32023-07-17 17:06:25 -0700692 BMCWEB_LOG_ERROR(
693 "Got extra property: {} on the {} object",
694 property.first, objpath.first.str);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700695 }
696 }
697 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700698 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700699 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700700 ? LinkType::Local
701 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700702 }
703 }
704 }
705 }
706}
707
708/**
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700709 * @brief Modifies the default gateway assigned to the NIC
710 *
711 * @param[in] ifaceId Id of network interface whose default gateway is to be
712 * changed
713 * @param[in] gateway The new gateway value. Assigning an empty string
714 * causes the gateway to be deleted
715 * @param[io] asyncResp Response object that will be returned to client
716 *
717 * @return None
718 */
719inline void updateIPv4DefaultGateway(
720 const std::string& ifaceId, const std::string& gateway,
721 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
722{
723 setDbusProperty(
724 asyncResp, "xyz.openbmc_project.Network",
725 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
726 ifaceId,
727 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
728 "Gateway", gateway);
729}
730
731/**
732 * @brief Deletes given static IP address for the interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700733 *
734 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700735 * @param[in] ipHash DBus Hash id of IP that should be deleted
736 * @param[io] asyncResp Response object that will be returned to client
737 *
738 * @return None
739 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600740inline void deleteIPAddress(const std::string& ifaceId,
741 const std::string& ipHash,
742 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700743{
744 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800745 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700746 if (ec)
747 {
748 messages::internalError(asyncResp->res);
749 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500750 },
Ed Tanous4a0cb852018-10-15 07:55:04 -0700751 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600752 "/xyz/openbmc_project/network/" + ifaceId + ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700753 "xyz.openbmc_project.Object.Delete", "Delete");
754}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700755
Ed Tanous4a0cb852018-10-15 07:55:04 -0700756/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700757 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700758 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700759 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
760 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
761 * @param[in] gateway IPv4 address of this interfaces gateway
762 * @param[in] address IPv4 address to assign to this interface
763 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700764 *
765 * @return None
766 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000767inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
768 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800769 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700770{
Patrick Williams5a39f772023-10-20 11:20:21 -0500771 auto createIpHandler = [asyncResp, ifaceId,
772 gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500773 if (ec)
774 {
775 messages::internalError(asyncResp->res);
776 return;
777 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500778 };
779
Ed Tanous4a0cb852018-10-15 07:55:04 -0700780 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500781 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700782 "/xyz/openbmc_project/network/" + ifaceId,
783 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700784 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700785 gateway);
786}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500787
788/**
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700789 * @brief Deletes the IP entry for this interface and creates a replacement
790 * static entry
Johnathan Mantey01784822019-06-18 12:44:21 -0700791 *
Johnathan Mantey743eb1c2024-04-03 12:05:57 -0700792 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
793 * @param[in] id The unique hash entry identifying the DBus entry
794 * @param[in] prefixLength Prefix syntax for the subnet mask
795 * @param[in] address Address to assign to this interface
796 * @param[in] numStaticAddrs Count of IPv4 static addresses
797 * @param[io] asyncResp Response object that will be returned to client
Johnathan Mantey01784822019-06-18 12:44:21 -0700798 *
799 * @return None
800 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600801
Ravi Teja9c5e5852023-02-26 21:33:52 -0600802inline void deleteAndCreateIPAddress(
803 IpVersion version, const std::string& ifaceId, const std::string& id,
804 uint8_t prefixLength, const std::string& address,
805 const std::string& gateway,
806 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700807{
808 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600809 [asyncResp, version, ifaceId, address, prefixLength,
810 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700811 if (ec)
812 {
813 messages::internalError(asyncResp->res);
814 }
Ravi Teja9c5e5852023-02-26 21:33:52 -0600815 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
816 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous002d39b2022-05-31 08:59:27 -0700817 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800818 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700819 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700820 {
821 messages::internalError(asyncResp->res);
822 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500823 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700824 "xyz.openbmc_project.Network",
825 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600826 "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
827 prefixLength, gateway);
Patrick Williams5a39f772023-10-20 11:20:21 -0500828 },
Johnathan Mantey01784822019-06-18 12:44:21 -0700829 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600830 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700831 "xyz.openbmc_project.Object.Delete", "Delete");
832}
833
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500834inline bool extractIPv6DefaultGatewayData(
835 const std::string& ethifaceId,
836 const dbus::utility::ManagedObjectType& dbusData,
837 std::vector<StaticGatewayData>& staticGatewayConfig)
838{
839 std::string staticGatewayPathStart("/xyz/openbmc_project/network/");
840 staticGatewayPathStart += ethifaceId;
841
842 for (const auto& objpath : dbusData)
843 {
844 if (!std::string_view(objpath.first.str)
845 .starts_with(staticGatewayPathStart))
846 {
847 continue;
848 }
849 for (const auto& interface : objpath.second)
850 {
851 if (interface.first != "xyz.openbmc_project.Network.StaticGateway")
852 {
853 continue;
854 }
855 StaticGatewayData& staticGateway =
856 staticGatewayConfig.emplace_back();
857 staticGateway.id = objpath.first.filename();
858
859 bool success = sdbusplus::unpackPropertiesNoThrow(
860 redfish::dbus_utils::UnpackErrorPrinter(), interface.second,
861 "Gateway", staticGateway.gateway, "PrefixLength",
862 staticGateway.prefixLength, "ProtocolType",
863 staticGateway.protocol);
864 if (!success)
865 {
866 return false;
867 }
868 }
869 }
870 return true;
871}
872
Johnathan Mantey01784822019-06-18 12:44:21 -0700873/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500874 * @brief Creates IPv6 with given data
875 *
876 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500877 * @param[in] prefixLength Prefix length that needs to be added
878 * @param[in] address IP address that needs to be added
879 * @param[io] asyncResp Response object that will be returned to client
880 *
881 * @return None
882 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500883inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
884 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800885 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500886{
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500887 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
888 path /= ifaceId;
889
Patrick Williams5a39f772023-10-20 11:20:21 -0500890 auto createIpHandler = [asyncResp,
891 address](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500892 if (ec)
893 {
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500894 if (ec == boost::system::errc::io_error)
895 {
896 messages::propertyValueFormatError(asyncResp->res, address,
897 "Address");
898 }
899 else
900 {
901 messages::internalError(asyncResp->res);
902 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500903 }
904 };
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500905 // Passing null for gateway, as per redfish spec IPv6StaticAddresses
906 // object does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500907 crow::connections::systemBus->async_method_call(
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500908 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500909 "xyz.openbmc_project.Network.IP.Create", "IP",
910 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
911 "");
912}
913
Ed Tanous4a0cb852018-10-15 07:55:04 -0700914/**
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500915 * @brief Deletes given IPv6 Static Gateway
916 *
917 * @param[in] ifaceId Id of interface whose IP should be deleted
918 * @param[in] ipHash DBus Hash id of IP that should be deleted
919 * @param[io] asyncResp Response object that will be returned to client
920 *
921 * @return None
922 */
923inline void
924 deleteIPv6Gateway(std::string_view gatewayId,
925 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
926{
927 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
928 path /= gatewayId;
929 crow::connections::systemBus->async_method_call(
930 [asyncResp](const boost::system::error_code& ec) {
931 if (ec)
932 {
933 messages::internalError(asyncResp->res);
934 }
935 },
936 "xyz.openbmc_project.Network", path,
937 "xyz.openbmc_project.Object.Delete", "Delete");
938}
939
940/**
941 * @brief Creates IPv6 static default gateway with given data
942 *
943 * @param[in] ifaceId Id of interface whose IP should be added
944 * @param[in] prefixLength Prefix length that needs to be added
945 * @param[in] gateway Gateway address that needs to be added
946 * @param[io] asyncResp Response object that will be returned to client
947 *
948 * @return None
949 */
950inline void createIPv6DefaultGateway(
951 std::string_view ifaceId, size_t prefixLength, std::string_view gateway,
952 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
953{
954 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
955 path /= ifaceId;
956 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
957 if (ec)
958 {
959 messages::internalError(asyncResp->res);
960 }
961 };
962 crow::connections::systemBus->async_method_call(
963 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
964 "xyz.openbmc_project.Network.StaticGateway.Create", "StaticGateway",
965 gateway, prefixLength, "xyz.openbmc_project.Network.IP.Protocol.IPv6");
966}
967
968/**
969 * @brief Deletes the IPv6 default gateway entry for this interface and
970 * creates a replacement IPv6 default gateway entry
971 *
972 * @param[in] ifaceId Id of interface upon which to create the IPv6
973 * entry
974 * @param[in] gateway IPv6 gateway to assign to this interface
975 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
976 * @param[io] asyncResp Response object that will be returned to client
977 *
978 * @return None
979 */
980inline void deleteAndCreateIPv6DefaultGateway(
981 std::string_view ifaceId, std::string_view gatewayId,
982 std::string_view gateway, size_t prefixLength,
983 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
984{
985 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
986 path /= gatewayId;
987 crow::connections::systemBus->async_method_call(
988 [asyncResp, ifaceId, gateway,
989 prefixLength](const boost::system::error_code& ec) {
990 if (ec)
991 {
992 messages::internalError(asyncResp->res);
993 return;
994 }
995 createIPv6DefaultGateway(ifaceId, prefixLength, gateway, asyncResp);
996 },
997 "xyz.openbmc_project.Network", path,
998 "xyz.openbmc_project.Object.Delete", "Delete");
999}
1000
1001/**
1002 * @brief Sets IPv6 default gateway with given data
1003 *
1004 * @param[in] ifaceId Id of interface whose gateway should be added
1005 * @param[in] input Contains address that needs to be added
1006 * @param[in] staticGatewayData Current static gateways in the system
1007 * @param[io] asyncResp Response object that will be returned to client
1008 *
1009 * @return None
1010 */
1011
1012inline void handleIPv6DefaultGateway(
Ed Tanous3dfed532024-03-06 14:41:27 -08001013 const std::string& ifaceId,
1014 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001015 const std::vector<StaticGatewayData>& staticGatewayData,
1016 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1017{
1018 size_t entryIdx = 1;
1019 std::vector<StaticGatewayData>::const_iterator staticGatewayEntry =
1020 staticGatewayData.begin();
1021
Ed Tanous3dfed532024-03-06 14:41:27 -08001022 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1023 input)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001024 {
1025 // find the next gateway entry
1026 while (staticGatewayEntry != staticGatewayData.end())
1027 {
1028 if (staticGatewayEntry->protocol ==
1029 "xyz.openbmc_project.Network.IP.Protocol.IPv6")
1030 {
1031 break;
1032 }
1033 staticGatewayEntry++;
1034 }
1035 std::string pathString = "IPv6StaticDefaultGateways/" +
1036 std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001037 nlohmann::json::object_t* obj =
1038 std::get_if<nlohmann::json::object_t>(&thisJson);
1039 if (obj == nullptr)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001040 {
1041 if (staticGatewayEntry == staticGatewayData.end())
1042 {
1043 messages::resourceCannotBeDeleted(asyncResp->res);
1044 return;
1045 }
1046 deleteIPv6Gateway(staticGatewayEntry->id, asyncResp);
1047 return;
1048 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001049 if (obj->empty())
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001050 {
1051 // Do nothing, but make sure the entry exists.
1052 if (staticGatewayEntry == staticGatewayData.end())
1053 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001054 messages::propertyValueFormatError(asyncResp->res, *obj,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001055 pathString);
1056 return;
1057 }
1058 }
1059 std::optional<std::string> address;
1060 std::optional<size_t> prefixLength;
1061
Ed Tanous3dfed532024-03-06 14:41:27 -08001062 if (!json_util::readJsonObject(*obj, asyncResp->res, "Address", address,
1063 "PrefixLength", prefixLength))
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001064 {
1065 return;
1066 }
1067 const std::string* addr = nullptr;
1068 size_t prefix = 0;
1069 if (address)
1070 {
1071 addr = &(*address);
1072 }
1073 else if (staticGatewayEntry != staticGatewayData.end())
1074 {
1075 addr = &(staticGatewayEntry->gateway);
1076 }
1077 else
1078 {
1079 messages::propertyMissing(asyncResp->res, pathString + "/Address");
1080 return;
1081 }
1082 if (prefixLength)
1083 {
1084 prefix = *prefixLength;
1085 }
1086 else if (staticGatewayEntry != staticGatewayData.end())
1087 {
1088 prefix = staticGatewayEntry->prefixLength;
1089 }
1090 else
1091 {
1092 messages::propertyMissing(asyncResp->res,
1093 pathString + "/PrefixLength");
1094 return;
1095 }
1096 if (staticGatewayEntry != staticGatewayData.end())
1097 {
1098 deleteAndCreateIPv6DefaultGateway(ifaceId, staticGatewayEntry->id,
1099 *addr, prefix, asyncResp);
1100 staticGatewayEntry++;
1101 }
1102 else
1103 {
1104 createIPv6DefaultGateway(ifaceId, prefix, *addr, asyncResp);
1105 }
1106 entryIdx++;
1107 }
1108}
1109
1110/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001111 * Function that retrieves all properties for given Ethernet Interface
1112 * Object
1113 * from EntityManager Network Manager
1114 * @param ethiface_id a eth interface id to query on DBus
1115 * @param callback a function that shall be called to convert Dbus output
1116 * into JSON
1117 */
1118template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +00001119void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001120 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001121{
George Liuf5892d02023-03-01 10:37:08 +08001122 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1123 dbus::utility::getManagedObjects(
1124 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001125 [ethifaceId{std::string{ethifaceId}},
Ed Tanous8cb2c022024-03-27 16:31:46 -07001126 callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001127 const boost::system::error_code& ec,
Ed Tanous3dfed532024-03-06 14:41:27 -08001128 const dbus::utility::ManagedObjectType& resp) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001129 EthernetInterfaceData ethData{};
Ed Tanous77179532023-02-28 10:45:28 -08001130 std::vector<IPv4AddressData> ipv4Data;
1131 std::vector<IPv6AddressData> ipv6Data;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001132 std::vector<StaticGatewayData> ipv6GatewayData;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001133
Ed Tanous8b242752023-06-27 17:17:13 -07001134 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001135 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001136 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 return;
1138 }
1139
1140 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
1141 if (!found)
1142 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001143 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001144 return;
1145 }
1146
1147 extractIPData(ethifaceId, resp, ipv4Data);
1148 // Fix global GW
1149 for (IPv4AddressData& ipv4 : ipv4Data)
1150 {
1151 if (((ipv4.linktype == LinkType::Global) &&
1152 (ipv4.gateway == "0.0.0.0")) ||
1153 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001154 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001155 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001156 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001157 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001158
Ed Tanous002d39b2022-05-31 08:59:27 -07001159 extractIPV6Data(ethifaceId, resp, ipv6Data);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001160 if (!extractIPv6DefaultGatewayData(ethifaceId, resp, ipv6GatewayData))
1161 {
1162 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1163 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001164 // Finally make a callback with useful data
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001165 callback(true, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Patrick Williams5a39f772023-10-20 11:20:21 -05001166 });
Ed Tanous271584a2019-07-09 16:24:22 -07001167}
Ed Tanous4a0cb852018-10-15 07:55:04 -07001168
1169/**
1170 * Function that retrieves all Ethernet Interfaces available through Network
1171 * Manager
1172 * @param callback a function that shall be called to convert Dbus output
1173 * into JSON.
1174 */
1175template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001176void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001177{
George Liuf5892d02023-03-01 10:37:08 +08001178 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1179 dbus::utility::getManagedObjects(
1180 "xyz.openbmc_project.Network", path,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001181 [callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001182 const boost::system::error_code& ec,
George Liuf5892d02023-03-01 10:37:08 +08001183 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001184 // Callback requires vector<string> to retrieve all available
1185 // ethernet interfaces
Ed Tanous77179532023-02-28 10:45:28 -08001186 std::vector<std::string> ifaceList;
Ed Tanous002d39b2022-05-31 08:59:27 -07001187 ifaceList.reserve(resp.size());
Ed Tanous8b242752023-06-27 17:17:13 -07001188 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001189 {
1190 callback(false, ifaceList);
1191 return;
1192 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001193
Ed Tanous002d39b2022-05-31 08:59:27 -07001194 // Iterate over all retrieved ObjectPaths.
1195 for (const auto& objpath : resp)
1196 {
1197 // And all interfaces available for certain ObjectPath.
1198 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001199 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001200 // If interface is
1201 // xyz.openbmc_project.Network.EthernetInterface, this is
1202 // what we're looking for.
1203 if (interface.first ==
1204 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -07001205 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 std::string ifaceId = objpath.first.filename();
1207 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001208 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001210 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001211 // and put it into output vector.
Ed Tanous77179532023-02-28 10:45:28 -08001212 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001213 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001214 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001215 }
Ed Tanous2c5875a2023-05-15 09:56:06 -07001216
Ed Tanous3544d2a2023-08-06 18:12:20 -07001217 std::ranges::sort(ifaceList, AlphanumLess<std::string>());
Ed Tanous2c5875a2023-05-15 09:56:06 -07001218
Ed Tanous002d39b2022-05-31 08:59:27 -07001219 // Finally make a callback with useful data
1220 callback(true, ifaceList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001221 });
Ed Tanous271584a2019-07-09 16:24:22 -07001222}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001223
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001224inline void
1225 handleHostnamePatch(const std::string& hostname,
1226 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001227{
Ed Tanousbf648f72021-06-03 15:00:14 -07001228 // SHOULD handle host names of up to 255 characters(RFC 1123)
1229 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001230 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001231 messages::propertyValueFormatError(asyncResp->res, hostname,
1232 "HostName");
1233 return;
1234 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001235 setDbusProperty(
1236 asyncResp, "xyz.openbmc_project.Network",
1237 sdbusplus::message::object_path("/xyz/openbmc_project/network/config"),
1238 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1239 "HostName", hostname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001240}
1241
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001242inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301243 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1244 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1245{
Ed Tanousd02aad32024-02-13 14:43:34 -08001246 sdbusplus::message::object_path objPath("/xyz/openbmc_project/network");
1247 objPath /= ifaceId;
1248 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", objPath,
1249 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1250 "MTUSize", mtuSize);
Tejas Patil35fb5312021-09-20 15:35:20 +05301251}
1252
1253inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001254 handleDomainnamePatch(const std::string& ifaceId,
1255 const std::string& domainname,
1256 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001257{
1258 std::vector<std::string> vectorDomainname = {domainname};
Ed Tanousd02aad32024-02-13 14:43:34 -08001259 setDbusProperty(
1260 asyncResp, "xyz.openbmc_project.Network",
1261 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1262 ifaceId,
1263 "xyz.openbmc_project.Network.EthernetInterface", "DomainName", "FQDN",
1264 vectorDomainname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001265}
1266
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001267inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001268{
1269 // A valid host name can never have the dotted-decimal form (RFC 1123)
Ed Tanous3544d2a2023-08-06 18:12:20 -07001270 if (std::ranges::all_of(hostname, ::isdigit))
Ed Tanousbf648f72021-06-03 15:00:14 -07001271 {
1272 return false;
1273 }
1274 // Each label(hostname/subdomains) within a valid FQDN
1275 // MUST handle host names of up to 63 characters (RFC 1123)
1276 // labels cannot start or end with hyphens (RFC 952)
1277 // labels can start with numbers (RFC 1123)
Ed Tanous4b242742023-05-11 09:51:51 -07001278 const static std::regex pattern(
Ed Tanousbf648f72021-06-03 15:00:14 -07001279 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1280
1281 return std::regex_match(hostname, pattern);
1282}
1283
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001284inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001285{
1286 // Can have multiple subdomains
1287 // Top Level Domain's min length is 2 character
Ed Tanous4b242742023-05-11 09:51:51 -07001288 const static std::regex pattern(
George Liu0fda0f12021-11-16 10:06:17 +08001289 "^([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 -07001290
1291 return std::regex_match(domainname, pattern);
1292}
1293
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001294inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1295 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001296{
1297 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1298 if (fqdn.length() > 255)
1299 {
1300 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1301 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001302 }
1303
Ed Tanousbf648f72021-06-03 15:00:14 -07001304 size_t pos = fqdn.find('.');
1305 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001306 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001307 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1308 return;
1309 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001310
Ed Tanousbf648f72021-06-03 15:00:14 -07001311 std::string hostname;
1312 std::string domainname;
1313 domainname = (fqdn).substr(pos + 1);
1314 hostname = (fqdn).substr(0, pos);
1315
1316 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1317 {
1318 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1319 return;
1320 }
1321
1322 handleHostnamePatch(hostname, asyncResp);
1323 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1324}
1325
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001326inline void
1327 handleMACAddressPatch(const std::string& ifaceId,
1328 const std::string& macAddress,
1329 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001330{
Ed Tanousd02aad32024-02-13 14:43:34 -08001331 setDbusProperty(
1332 asyncResp, "xyz.openbmc_project.Network",
1333 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1334 ifaceId,
1335 "xyz.openbmc_project.Network.MACAddress", "MACAddress", "MACAddress",
1336 macAddress);
Ed Tanousbf648f72021-06-03 15:00:14 -07001337}
1338
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001339inline void setDHCPEnabled(const std::string& ifaceId,
1340 const std::string& propertyName, const bool v4Value,
1341 const bool v6Value,
1342 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001343{
1344 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
Ed Tanousd02aad32024-02-13 14:43:34 -08001345 setDbusProperty(
1346 asyncResp, "xyz.openbmc_project.Network",
1347 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1348 ifaceId,
1349 "xyz.openbmc_project.Network.EthernetInterface", propertyName, "DHCPv4",
1350 dhcp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001351}
1352
Jishnu CMe4588152023-05-11 00:04:40 -05001353enum class NetworkType
1354{
1355 dhcp4,
1356 dhcp6
1357};
1358
1359inline void setDHCPConfig(const std::string& propertyName, const bool& value,
1360 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1361 const std::string& ethifaceId, NetworkType type)
Ed Tanousbf648f72021-06-03 15:00:14 -07001362{
Ed Tanous62598e32023-07-17 17:06:25 -07001363 BMCWEB_LOG_DEBUG("{} = {}", propertyName, value);
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001364 std::string redfishPropertyName;
Jishnu CMe4588152023-05-11 00:04:40 -05001365 sdbusplus::message::object_path path("/xyz/openbmc_project/network/");
1366 path /= ethifaceId;
1367
1368 if (type == NetworkType::dhcp4)
1369 {
1370 path /= "dhcp4";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001371 redfishPropertyName = "DHCPv4";
Jishnu CMe4588152023-05-11 00:04:40 -05001372 }
1373 else
1374 {
1375 path /= "dhcp6";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001376 redfishPropertyName = "DHCPv6";
Jishnu CMe4588152023-05-11 00:04:40 -05001377 }
1378
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001379 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1380 "xyz.openbmc_project.Network.DHCPConfiguration",
1381 propertyName, redfishPropertyName, value);
Ed Tanousbf648f72021-06-03 15:00:14 -07001382}
1383
Ravi Tejab10d8db2022-05-24 09:04:12 -05001384inline void handleSLAACAutoConfigPatch(
1385 const std::string& ifaceId, bool ipv6AutoConfigEnabled,
1386 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1387{
1388 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1389 path /= ifaceId;
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001390 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1391 "xyz.openbmc_project.Network.EthernetInterface",
1392 "IPv6AcceptRA",
1393 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
1394 ipv6AutoConfigEnabled);
Ravi Tejab10d8db2022-05-24 09:04:12 -05001395}
1396
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001397inline void handleDHCPPatch(const std::string& ifaceId,
1398 const EthernetInterfaceData& ethData,
1399 const DHCPParameters& v4dhcpParms,
1400 const DHCPParameters& v6dhcpParms,
1401 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001402{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001403 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1404 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001405
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001406 if (ipv4Active)
1407 {
1408 updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1409 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001410 bool nextv4DHCPState =
1411 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1412
1413 bool nextv6DHCPState{};
1414 if (v6dhcpParms.dhcpv6OperatingMode)
1415 {
Ravi Tejab10d8db2022-05-24 09:04:12 -05001416 if ((*v6dhcpParms.dhcpv6OperatingMode != "Enabled") &&
Ed Tanousbf648f72021-06-03 15:00:14 -07001417 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1418 {
1419 messages::propertyValueFormatError(asyncResp->res,
1420 *v6dhcpParms.dhcpv6OperatingMode,
1421 "OperatingMode");
1422 return;
1423 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05001424 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Enabled");
Ed Tanousbf648f72021-06-03 15:00:14 -07001425 }
1426 else
1427 {
1428 nextv6DHCPState = ipv6Active;
1429 }
1430
Jishnu CMe4588152023-05-11 00:04:40 -05001431 bool nextDNSv4 = ethData.dnsv4Enabled;
1432 bool nextDNSv6 = ethData.dnsv6Enabled;
1433 if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001434 {
Jishnu CMe4588152023-05-11 00:04:40 -05001435 nextDNSv4 = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001436 }
Jishnu CMe4588152023-05-11 00:04:40 -05001437 if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001438 {
Jishnu CMe4588152023-05-11 00:04:40 -05001439 nextDNSv6 = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001440 }
1441
Jishnu CMe4588152023-05-11 00:04:40 -05001442 bool nextNTPv4 = ethData.ntpv4Enabled;
1443 bool nextNTPv6 = ethData.ntpv6Enabled;
1444 if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001445 {
Jishnu CMe4588152023-05-11 00:04:40 -05001446 nextNTPv4 = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001447 }
Jishnu CMe4588152023-05-11 00:04:40 -05001448 if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001449 {
Jishnu CMe4588152023-05-11 00:04:40 -05001450 nextNTPv6 = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001451 }
1452
Ravi Teja91c441e2024-02-23 09:03:43 -06001453 bool nextUsev4Domain = ethData.domainv4Enabled;
1454 bool nextUsev6Domain = ethData.domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -05001455 if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001456 {
Jishnu CMe4588152023-05-11 00:04:40 -05001457 nextUsev4Domain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001458 }
Jishnu CMe4588152023-05-11 00:04:40 -05001459 if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001460 {
Jishnu CMe4588152023-05-11 00:04:40 -05001461 nextUsev6Domain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001462 }
1463
Ed Tanous62598e32023-07-17 17:06:25 -07001464 BMCWEB_LOG_DEBUG("set DHCPEnabled...");
Ed Tanousbf648f72021-06-03 15:00:14 -07001465 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1466 asyncResp);
Ed Tanous62598e32023-07-17 17:06:25 -07001467 BMCWEB_LOG_DEBUG("set DNSEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001468 setDHCPConfig("DNSEnabled", nextDNSv4, asyncResp, ifaceId,
1469 NetworkType::dhcp4);
Ed Tanous62598e32023-07-17 17:06:25 -07001470 BMCWEB_LOG_DEBUG("set NTPEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001471 setDHCPConfig("NTPEnabled", nextNTPv4, asyncResp, ifaceId,
1472 NetworkType::dhcp4);
Ravi Teja91c441e2024-02-23 09:03:43 -06001473 BMCWEB_LOG_DEBUG("set DomainEnabled...");
1474 setDHCPConfig("DomainEnabled", nextUsev4Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001475 NetworkType::dhcp4);
1476 BMCWEB_LOG_DEBUG("set DNSEnabled for dhcp6...");
1477 setDHCPConfig("DNSEnabled", nextDNSv6, asyncResp, ifaceId,
1478 NetworkType::dhcp6);
1479 BMCWEB_LOG_DEBUG("set NTPEnabled for dhcp6...");
1480 setDHCPConfig("NTPEnabled", nextNTPv6, asyncResp, ifaceId,
1481 NetworkType::dhcp6);
Ravi Teja91c441e2024-02-23 09:03:43 -06001482 BMCWEB_LOG_DEBUG("set DomainEnabled for dhcp6...");
1483 setDHCPConfig("DomainEnabled", nextUsev6Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001484 NetworkType::dhcp6);
Ed Tanousbf648f72021-06-03 15:00:14 -07001485}
1486
Ed Tanous77179532023-02-28 10:45:28 -08001487inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1488 const std::vector<IPv4AddressData>::const_iterator& head,
1489 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001490{
1491 return std::find_if(head, end, [](const IPv4AddressData& value) {
1492 return value.origin == "Static";
1493 });
1494}
1495
Ed Tanous77179532023-02-28 10:45:28 -08001496inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1497 const std::vector<IPv6AddressData>::const_iterator& head,
1498 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001499{
1500 return std::find_if(head, end, [](const IPv6AddressData& value) {
1501 return value.origin == "Static";
1502 });
1503}
1504
Ed Tanous3dfed532024-03-06 14:41:27 -08001505inline void handleIPv4StaticPatch(
1506 const std::string& ifaceId,
1507 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001508 const EthernetInterfaceData& ethData,
Ed Tanous3dfed532024-03-06 14:41:27 -08001509 const std::vector<IPv4AddressData>& ipv4Data,
1510 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001511{
Ed Tanousbf648f72021-06-03 15:00:14 -07001512 unsigned entryIdx = 1;
1513 // Find the first static IP address currently active on the NIC and
1514 // match it to the first JSON element in the IPv4StaticAddresses array.
1515 // Match each subsequent JSON element to the next static IP programmed
1516 // into the NIC.
Ed Tanous77179532023-02-28 10:45:28 -08001517 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001518 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1519
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001520 bool gatewayValueAssigned{};
Johnathan Mantey4a8f5d42024-05-24 08:00:46 -07001521 bool preserveGateway{};
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001522 std::string activePath{};
1523 std::string activeGateway{};
1524 if (!ethData.defaultGateway.empty() && ethData.defaultGateway != "0.0.0.0")
1525 {
1526 // The NIC is already configured with a default gateway. Use this if
1527 // the leading entry in the PATCH is '{}', which is preserving an active
1528 // static address.
1529 activeGateway = ethData.defaultGateway;
1530 activePath = "IPv4StaticAddresses/1";
1531 gatewayValueAssigned = true;
1532 }
1533
Ed Tanous3dfed532024-03-06 14:41:27 -08001534 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1535 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001536 {
Patrick Williams89492a12023-05-10 07:51:34 -05001537 std::string pathString = "IPv4StaticAddresses/" +
1538 std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001539 nlohmann::json::object_t* obj =
1540 std::get_if<nlohmann::json::object_t>(&thisJson);
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001541 if (obj == nullptr)
1542 {
1543 if (nicIpEntry != ipv4Data.cend())
1544 {
1545 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
1546 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1547 ipv4Data.cend());
Johnathan Mantey4a8f5d42024-05-24 08:00:46 -07001548 if (!preserveGateway && (nicIpEntry == ipv4Data.cend()))
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001549 {
1550 // All entries have been processed, and this last has
1551 // requested the IP address be deleted. No prior entry
1552 // performed an action that created or modified a
1553 // gateway. Deleting this IP address means the default
1554 // gateway entry has to be removed as well.
1555 updateIPv4DefaultGateway(ifaceId, "", asyncResp);
1556 }
1557 entryIdx++;
1558 continue;
1559 }
1560 // Received a DELETE action on an entry not assigned to the NIC
1561 messages::resourceCannotBeDeleted(asyncResp->res);
1562 return;
1563 }
1564
1565 // An Add/Modify action is requested
1566 if (!obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001567 {
1568 std::optional<std::string> address;
1569 std::optional<std::string> subnetMask;
1570 std::optional<std::string> gateway;
1571
Ed Tanous3dfed532024-03-06 14:41:27 -08001572 if (!json_util::readJsonObject(*obj, asyncResp->res, "Address",
1573 address, "SubnetMask", subnetMask,
1574 "Gateway", gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001575 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001576 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001577 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001578 return;
1579 }
1580
1581 // Find the address/subnet/gateway values. Any values that are
1582 // not explicitly provided are assumed to be unmodified from the
1583 // current state of the interface. Merge existing state into the
1584 // current request.
Ed Tanousbf648f72021-06-03 15:00:14 -07001585 if (address)
1586 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001587 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001588 {
1589 messages::propertyValueFormatError(asyncResp->res, *address,
1590 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001591 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001592 }
1593 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001594 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001595 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001596 address = (nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001597 }
1598 else
1599 {
1600 messages::propertyMissing(asyncResp->res,
1601 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001602 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001603 }
1604
Ed Tanouse01d0c32023-06-30 13:21:32 -07001605 uint8_t prefixLength = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001606 if (subnetMask)
1607 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001608 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1609 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001610 {
1611 messages::propertyValueFormatError(
1612 asyncResp->res, *subnetMask,
1613 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001614 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001615 }
1616 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001617 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001618 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001619 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1620 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001621 {
1622 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001623 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001624 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001625 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001626 }
1627 }
1628 else
1629 {
1630 messages::propertyMissing(asyncResp->res,
1631 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001632 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001633 }
1634
1635 if (gateway)
1636 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001637 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001638 {
1639 messages::propertyValueFormatError(asyncResp->res, *gateway,
1640 pathString + "/Gateway");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001641 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001642 }
1643 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001644 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001645 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001646 gateway = nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001647 }
1648 else
1649 {
1650 messages::propertyMissing(asyncResp->res,
1651 pathString + "/Gateway");
Ed Tanousbf648f72021-06-03 15:00:14 -07001652 return;
1653 }
1654
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001655 if (gatewayValueAssigned)
1656 {
1657 if (activeGateway != gateway)
1658 {
1659 // A NIC can only have a single active gateway value.
1660 // If any gateway in the array of static addresses
1661 // mismatch the PATCH is in error.
1662 std::string arg1 = pathString + "/Gateway";
1663 std::string arg2 = activePath + "/Gateway";
1664 messages::propertyValueConflict(asyncResp->res, arg1, arg2);
1665 return;
1666 }
1667 }
1668 else
1669 {
1670 // Capture the very first gateway value from the incoming
1671 // JSON record and use it at the default gateway.
1672 updateIPv4DefaultGateway(ifaceId, *gateway, asyncResp);
1673 activeGateway = *gateway;
1674 activePath = pathString;
1675 gatewayValueAssigned = true;
1676 }
1677
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001678 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001679 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001680 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
Ed Tanous77eb0152023-09-06 10:19:18 -07001681 nicIpEntry->id, prefixLength, *address,
1682 *gateway, asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001683 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1684 ipv4Data.cend());
Johnathan Mantey4a8f5d42024-05-24 08:00:46 -07001685 preserveGateway = true;
Ed Tanousbf648f72021-06-03 15:00:14 -07001686 }
1687 else
1688 {
1689 createIPv4(ifaceId, prefixLength, *gateway, *address,
1690 asyncResp);
Johnathan Mantey4a8f5d42024-05-24 08:00:46 -07001691 preserveGateway = true;
Ed Tanousbf648f72021-06-03 15:00:14 -07001692 }
1693 entryIdx++;
1694 }
1695 else
1696 {
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001697 // Received {}, do not modify this address
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001698 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001699 {
Patrick Williams89492a12023-05-10 07:51:34 -05001700 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1701 ipv4Data.cend());
Johnathan Mantey4a8f5d42024-05-24 08:00:46 -07001702 preserveGateway = true;
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001703 entryIdx++;
Ed Tanousbf648f72021-06-03 15:00:14 -07001704 }
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07001705 else
1706 {
1707 // Requested a DO NOT MODIFY action on an entry not assigned
1708 // to the NIC
1709 messages::propertyValueFormatError(asyncResp->res, *obj,
1710 pathString);
1711 return;
1712 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001713 }
1714 }
1715}
1716
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001717inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001718 const std::string& ifaceId,
1719 const std::vector<std::string>& updatedStaticNameServers,
1720 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1721{
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001722 setDbusProperty(
1723 asyncResp, "xyz.openbmc_project.Network",
1724 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1725 ifaceId,
George Liu9ae226f2023-06-21 17:56:46 +08001726 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001727 "StaticNameServers", updatedStaticNameServers);
Ed Tanousbf648f72021-06-03 15:00:14 -07001728}
1729
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001730inline void handleIPv6StaticAddressesPatch(
Ed Tanous3dfed532024-03-06 14:41:27 -08001731 const std::string& ifaceId,
1732 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Ed Tanous77179532023-02-28 10:45:28 -08001733 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001734 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1735{
Ed Tanousbf648f72021-06-03 15:00:14 -07001736 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001737 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001738 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanous3dfed532024-03-06 14:41:27 -08001739 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1740 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001741 {
Patrick Williams89492a12023-05-10 07:51:34 -05001742 std::string pathString = "IPv6StaticAddresses/" +
1743 std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001744 nlohmann::json::object_t* obj =
1745 std::get_if<nlohmann::json::object_t>(&thisJson);
1746 if (obj != nullptr && !obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001747 {
1748 std::optional<std::string> address;
1749 std::optional<uint8_t> prefixLength;
Ed Tanous3dfed532024-03-06 14:41:27 -08001750 nlohmann::json::object_t thisJsonCopy = *obj;
1751 if (!json_util::readJsonObject(thisJsonCopy, asyncResp->res,
1752 "Address", address, "PrefixLength",
1753 prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001754 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001755 messages::propertyValueFormatError(asyncResp->res, thisJsonCopy,
Ed Tanousf818b042022-06-27 13:17:35 -07001756 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001757 return;
1758 }
1759
Ed Tanousbf648f72021-06-03 15:00:14 -07001760 // Find the address and prefixLength values. Any values that are
1761 // not explicitly provided are assumed to be unmodified from the
1762 // current state of the interface. Merge existing state into the
1763 // current request.
Ed Tanousd547d8d2024-03-16 18:04:41 -07001764 if (!address)
Ed Tanousbf648f72021-06-03 15:00:14 -07001765 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001766 if (nicIpEntry == ipv6Data.end())
1767 {
1768 messages::propertyMissing(asyncResp->res,
1769 pathString + "/Address");
1770 return;
1771 }
1772 address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001773 }
1774
Ed Tanousd547d8d2024-03-16 18:04:41 -07001775 if (!prefixLength)
Ed Tanousbf648f72021-06-03 15:00:14 -07001776 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001777 if (nicIpEntry == ipv6Data.end())
1778 {
1779 messages::propertyMissing(asyncResp->res,
1780 pathString + "/PrefixLength");
1781 return;
1782 }
1783 prefixLength = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001784 }
1785
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001786 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001787 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001788 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
Ed Tanousd547d8d2024-03-16 18:04:41 -07001789 nicIpEntry->id, *prefixLength,
1790 *address, "", asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001791 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1792 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001793 }
1794 else
1795 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001796 createIPv6(ifaceId, *prefixLength, *address, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001797 }
1798 entryIdx++;
1799 }
1800 else
1801 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001802 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001803 {
1804 // Requesting a DELETE/DO NOT MODIFY action for an item
1805 // that isn't present on the eth(n) interface. Input JSON is
1806 // in error, so bail out.
Ed Tanous3dfed532024-03-06 14:41:27 -08001807 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001808 {
1809 messages::resourceCannotBeDeleted(asyncResp->res);
1810 return;
1811 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001812 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001813 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001814 return;
1815 }
1816
Ed Tanous3dfed532024-03-06 14:41:27 -08001817 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001818 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001819 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001820 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001821 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001822 {
Patrick Williams89492a12023-05-10 07:51:34 -05001823 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1824 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001825 }
1826 entryIdx++;
1827 }
1828 }
1829}
1830
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001831inline std::string extractParentInterfaceName(const std::string& ifaceId)
1832{
1833 std::size_t pos = ifaceId.find('_');
1834 return ifaceId.substr(0, pos);
1835}
1836
Ed Tanous77179532023-02-28 10:45:28 -08001837inline void
1838 parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1839 const std::string& ifaceId,
1840 const EthernetInterfaceData& ethData,
1841 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001842 const std::vector<IPv6AddressData>& ipv6Data,
1843 const std::vector<StaticGatewayData>& ipv6GatewayData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001844{
Ed Tanousbf648f72021-06-03 15:00:14 -07001845 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1846 jsonResponse["Id"] = ifaceId;
Ed Tanous253f11b2024-05-16 09:38:31 -07001847 jsonResponse["@odata.id"] =
1848 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
1849 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001850 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1851
Ed Tanousbf648f72021-06-03 15:00:14 -07001852 if (ethData.nicEnabled)
1853 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001854 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001855 jsonResponse["Status"]["State"] = "Enabled";
1856 }
1857 else
1858 {
1859 jsonResponse["LinkStatus"] = "NoLink";
1860 jsonResponse["Status"]["State"] = "Disabled";
1861 }
1862
Ed Tanousbf648f72021-06-03 15:00:14 -07001863 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301864 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001865 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001866 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001867 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
Jishnu CMe4588152023-05-11 00:04:40 -05001868 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1869 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
1870 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNamev4Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001871 jsonResponse["DHCPv6"]["OperatingMode"] =
Ravi Tejab10d8db2022-05-24 09:04:12 -05001872 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Enabled"
Ed Tanousbf648f72021-06-03 15:00:14 -07001873 : "Disabled";
Jishnu CMe4588152023-05-11 00:04:40 -05001874 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1875 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
1876 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNamev6Enabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -05001877 jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1878 ethData.ipv6AcceptRa;
Ed Tanousbf648f72021-06-03 15:00:14 -07001879
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001880 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001881 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001882 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001883
1884 // When domain name is empty then it means, that it is a network
1885 // without domain names, and the host name itself must be treated as
1886 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001887 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001888 if (!ethData.domainnames.empty())
1889 {
1890 fqdn += "." + ethData.domainnames[0];
1891 }
1892 jsonResponse["FQDN"] = fqdn;
1893 }
1894
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001895 if (ethData.vlanId)
1896 {
1897 jsonResponse["EthernetInterfaceType"] = "Virtual";
1898 jsonResponse["VLAN"]["VLANEnable"] = true;
1899 jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1900 jsonResponse["VLAN"]["Tagged"] = true;
1901
1902 nlohmann::json::array_t relatedInterfaces;
1903 nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1904 parentInterface["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001905 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1906 BMCWEB_REDFISH_MANAGER_URI_NAME,
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001907 extractParentInterfaceName(ifaceId));
1908 jsonResponse["Links"]["RelatedInterfaces"] =
1909 std::move(relatedInterfaces);
1910 }
1911 else
1912 {
1913 jsonResponse["EthernetInterfaceType"] = "Physical";
1914 }
1915
Ed Tanousbf648f72021-06-03 15:00:14 -07001916 jsonResponse["NameServers"] = ethData.nameServers;
1917 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1918
1919 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1920 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1921 ipv4Array = nlohmann::json::array();
1922 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001923 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001924 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001925 std::string gatewayStr = ipv4Config.gateway;
1926 if (gatewayStr.empty())
1927 {
1928 gatewayStr = "0.0.0.0";
1929 }
Ed Tanous14766872022-03-15 10:44:42 -07001930 nlohmann::json::object_t ipv4;
1931 ipv4["AddressOrigin"] = ipv4Config.origin;
1932 ipv4["SubnetMask"] = ipv4Config.netmask;
1933 ipv4["Address"] = ipv4Config.address;
1934 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001935
Ed Tanousbf648f72021-06-03 15:00:14 -07001936 if (ipv4Config.origin == "Static")
1937 {
Ed Tanous14766872022-03-15 10:44:42 -07001938 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001939 }
Ed Tanous14766872022-03-15 10:44:42 -07001940
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001941 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001942 }
1943
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001944 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001945 if (ipv6GatewayStr.empty())
1946 {
1947 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1948 }
1949
1950 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1951
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001952 nlohmann::json::array_t ipv6StaticGatewayArray;
1953 for (const auto& ipv6GatewayConfig : ipv6GatewayData)
1954 {
1955 nlohmann::json::object_t ipv6Gateway;
1956 ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
1957 ipv6Gateway["PrefixLength"] = ipv6GatewayConfig.prefixLength;
1958 ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
1959 }
1960 jsonResponse["IPv6StaticDefaultGateways"] =
1961 std::move(ipv6StaticGatewayArray);
1962
Ed Tanousbf648f72021-06-03 15:00:14 -07001963 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1964 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1965 ipv6Array = nlohmann::json::array();
1966 ipv6StaticArray = nlohmann::json::array();
1967 nlohmann::json& ipv6AddrPolicyTable =
1968 jsonResponse["IPv6AddressPolicyTable"];
1969 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001970 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001971 {
Ed Tanous14766872022-03-15 10:44:42 -07001972 nlohmann::json::object_t ipv6;
1973 ipv6["Address"] = ipv6Config.address;
1974 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1975 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05001976
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001977 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001978 if (ipv6Config.origin == "Static")
1979 {
Ed Tanous14766872022-03-15 10:44:42 -07001980 nlohmann::json::object_t ipv6Static;
1981 ipv6Static["Address"] = ipv6Config.address;
1982 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001983 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001984 }
1985 }
1986}
1987
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08001988inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1989 const std::string& ifaceId,
1990 const boost::system::error_code& ec,
1991 const sdbusplus::message_t& m)
1992{
1993 if (!ec)
1994 {
1995 return;
1996 }
1997 const sd_bus_error* dbusError = m.get_error();
1998 if (dbusError == nullptr)
1999 {
2000 messages::internalError(asyncResp->res);
2001 return;
2002 }
Ed Tanous62598e32023-07-17 17:06:25 -07002003 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002004
2005 if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
2006 dbusError->name)
2007 {
2008 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2009 ifaceId);
2010 return;
2011 }
2012 if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
2013 dbusError->name)
2014 {
2015 messages::resourceCannotBeDeleted(asyncResp->res);
2016 return;
2017 }
2018 messages::internalError(asyncResp->res);
2019}
2020
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002021inline void afterVlanCreate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2022 const std::string& parentInterfaceUri,
2023 const std::string& vlanInterface,
2024 const boost::system::error_code& ec,
2025 const sdbusplus::message_t& m
2026
2027)
2028{
2029 if (ec)
2030 {
2031 const sd_bus_error* dbusError = m.get_error();
2032 if (dbusError == nullptr)
2033 {
2034 messages::internalError(asyncResp->res);
2035 return;
2036 }
Ed Tanous62598e32023-07-17 17:06:25 -07002037 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002038
2039 if (std::string_view(
2040 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
2041 dbusError->name)
2042 {
2043 messages::propertyValueNotInList(
2044 asyncResp->res, parentInterfaceUri,
2045 "Links/RelatedInterfaces/0/@odata.id");
2046 return;
2047 }
2048 if (std::string_view(
2049 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
2050 dbusError->name)
2051 {
2052 messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
2053 "Id", vlanInterface);
2054 return;
2055 }
2056 messages::internalError(asyncResp->res);
2057 return;
2058 }
2059
Ed Tanous253f11b2024-05-16 09:38:31 -07002060 const boost::urls::url vlanInterfaceUri =
2061 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2062 BMCWEB_REDFISH_MANAGER_URI_NAME, vlanInterface);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002063 asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
2064}
2065
Ed Tanousbf648f72021-06-03 15:00:14 -07002066inline void requestEthernetInterfacesRoutes(App& app)
2067{
Ed Tanous253f11b2024-05-16 09:38:31 -07002068 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07002069 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002070 .methods(boost::beast::http::verb::get)(
2071 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002072 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2073 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002074 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002075 {
2076 return;
2077 }
2078
Ed Tanous253f11b2024-05-16 09:38:31 -07002079 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2080 {
2081 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2082 return;
2083 }
2084
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 asyncResp->res.jsonValue["@odata.type"] =
2086 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2087 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002088 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
2089 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002090 asyncResp->res.jsonValue["Name"] =
2091 "Ethernet Network Interface Collection";
2092 asyncResp->res.jsonValue["Description"] =
2093 "Collection of EthernetInterfaces for this Manager";
2094
2095 // Get eth interface list, and call the below callback for JSON
2096 // preparation
2097 getEthernetIfaceList(
Ed Tanous77179532023-02-28 10:45:28 -08002098 [asyncResp](const bool& success,
2099 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002100 if (!success)
2101 {
2102 messages::internalError(asyncResp->res);
2103 return;
2104 }
2105
2106 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
2107 ifaceArray = nlohmann::json::array();
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 for (const std::string& ifaceItem : ifaceList)
2109 {
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08002110 nlohmann::json::object_t iface;
2111 iface["@odata.id"] = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -07002112 "/redfish/v1/Managers/{}/EthernetInterfaces/{}",
2113 BMCWEB_REDFISH_MANAGER_URI_NAME, ifaceItem);
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08002114 ifaceArray.push_back(std::move(iface));
Ed Tanous002d39b2022-05-31 08:59:27 -07002115 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002116
Ed Tanous002d39b2022-05-31 08:59:27 -07002117 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
Ed Tanous253f11b2024-05-16 09:38:31 -07002118 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2119 "/redfish/v1/Managers/{}/EthernetInterfaces",
2120 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002121 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002122 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002123
Ed Tanous253f11b2024-05-16 09:38:31 -07002124 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/")
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002125 .privileges(redfish::privileges::postEthernetInterfaceCollection)
2126 .methods(boost::beast::http::verb::post)(
2127 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002128 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2129 const std::string& managerId) {
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002130 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2131 {
2132 return;
2133 }
2134
Ed Tanous253f11b2024-05-16 09:38:31 -07002135 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2136 {
2137 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2138 return;
2139 }
2140
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002141 bool vlanEnable = false;
2142 uint32_t vlanId = 0;
Ed Tanous3dfed532024-03-06 14:41:27 -08002143 std::vector<nlohmann::json::object_t> relatedInterfaces;
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002144
2145 if (!json_util::readJsonPatch(req, asyncResp->res, "VLAN/VLANEnable",
2146 vlanEnable, "VLAN/VLANId", vlanId,
2147 "Links/RelatedInterfaces",
2148 relatedInterfaces))
2149 {
2150 return;
2151 }
2152
2153 if (relatedInterfaces.size() != 1)
2154 {
2155 messages::arraySizeTooLong(asyncResp->res,
2156 "Links/RelatedInterfaces",
2157 relatedInterfaces.size());
2158 return;
2159 }
2160
2161 std::string parentInterfaceUri;
Ed Tanous3dfed532024-03-06 14:41:27 -08002162 if (!json_util::readJsonObject(relatedInterfaces[0], asyncResp->res,
2163 "@odata.id", parentInterfaceUri))
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002164 {
2165 messages::propertyMissing(asyncResp->res,
2166 "Links/RelatedInterfaces/0/@odata.id");
2167 return;
2168 }
Ed Tanous62598e32023-07-17 17:06:25 -07002169 BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002170
Ed Tanous6fd29552023-10-04 09:40:14 -07002171 boost::system::result<boost::urls::url_view> parsedUri =
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002172 boost::urls::parse_relative_ref(parentInterfaceUri);
2173 if (!parsedUri)
2174 {
2175 messages::propertyValueFormatError(
2176 asyncResp->res, parentInterfaceUri,
2177 "Links/RelatedInterfaces/0/@odata.id");
2178 return;
2179 }
2180
2181 std::string parentInterface;
2182 if (!crow::utility::readUrlSegments(
2183 *parsedUri, "redfish", "v1", "Managers", "bmc",
2184 "EthernetInterfaces", std::ref(parentInterface)))
2185 {
2186 messages::propertyValueNotInList(
2187 asyncResp->res, parentInterfaceUri,
2188 "Links/RelatedInterfaces/0/@odata.id");
2189 return;
2190 }
2191
2192 if (!vlanEnable)
2193 {
2194 // In OpenBMC implementation, VLANEnable cannot be false on
2195 // create
2196 messages::propertyValueIncorrect(asyncResp->res, "VLAN/VLANEnable",
2197 "false");
2198 return;
2199 }
2200
2201 std::string vlanInterface = parentInterface + "_" +
2202 std::to_string(vlanId);
2203 crow::connections::systemBus->async_method_call(
2204 [asyncResp, parentInterfaceUri,
2205 vlanInterface](const boost::system::error_code& ec,
2206 const sdbusplus::message_t& m) {
2207 afterVlanCreate(asyncResp, parentInterfaceUri, vlanInterface, ec,
2208 m);
Patrick Williams5a39f772023-10-20 11:20:21 -05002209 },
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002210 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
2211 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", parentInterface,
2212 vlanId);
Patrick Williams5a39f772023-10-20 11:20:21 -05002213 });
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002214
Ed Tanous253f11b2024-05-16 09:38:31 -07002215 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002216 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002217 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002218 [&app](const crow::Request& req,
2219 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002220 const std::string& managerId, const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002221 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002222 {
2223 return;
2224 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002225
2226 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2227 {
2228 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2229 return;
2230 }
2231
Ed Tanous002d39b2022-05-31 08:59:27 -07002232 getEthernetIfaceData(
2233 ifaceId,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002234 [asyncResp,
2235 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2236 const std::vector<IPv4AddressData>& ipv4Data,
2237 const std::vector<IPv6AddressData>& ipv6Data,
2238 const std::vector<StaticGatewayData>& ipv6GatewayData) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 if (!success)
2240 {
2241 // TODO(Pawel)consider distinguish between non
2242 // existing object, and other errors
2243 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2244 ifaceId);
2245 return;
2246 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002247
Ed Tanous002d39b2022-05-31 08:59:27 -07002248 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao93bbc952023-02-23 14:11:20 +08002249 "#EthernetInterface.v1_9_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07002250 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
2251 asyncResp->res.jsonValue["Description"] =
2252 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05302253
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002254 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data,
2255 ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07002256 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002257 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002258
Ed Tanous253f11b2024-05-16 09:38:31 -07002259 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002260 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002261 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002262 [&app](const crow::Request& req,
2263 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002264 const std::string& managerId, const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002265 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002266 {
2267 return;
2268 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002269
2270 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2271 {
2272 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2273 return;
2274 }
2275
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 std::optional<std::string> hostname;
2277 std::optional<std::string> fqdn;
2278 std::optional<std::string> macAddress;
2279 std::optional<std::string> ipv6DefaultGateway;
Ed Tanous3dfed532024-03-06 14:41:27 -08002280 std::optional<
2281 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2282 ipv4StaticAddresses;
2283 std::optional<
2284 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2285 ipv6StaticAddresses;
2286 std::optional<
2287 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2288 ipv6StaticDefaultGateways;
Ed Tanous002d39b2022-05-31 08:59:27 -07002289 std::optional<std::vector<std::string>> staticNameServers;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002290 std::optional<bool> ipv6AutoConfigEnabled;
Ed Tanous002d39b2022-05-31 08:59:27 -07002291 std::optional<bool> interfaceEnabled;
2292 std::optional<size_t> mtuSize;
2293 DHCPParameters v4dhcpParms;
2294 DHCPParameters v6dhcpParms;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002295 // clang-format off
Ed Tanous3dfed532024-03-06 14:41:27 -08002296 if (!json_util::readJsonPatch(req, asyncResp->res,
2297 "DHCPv4/DHCPEnabled", v4dhcpParms.dhcpv4Enabled,
2298 "DHCPv4/UseDNSServers", v4dhcpParms.useDnsServers,
2299 "DHCPv4/UseDomainName", v4dhcpParms.useDomainName,
2300 "DHCPv4/UseNTPServers", v4dhcpParms.useNtpServers,
2301 "DHCPv6/OperatingMode", v6dhcpParms.dhcpv6OperatingMode,
2302 "DHCPv6/UseDNSServers", v6dhcpParms.useDnsServers,
2303 "DHCPv6/UseDomainName", v6dhcpParms.useDomainName,
2304 "DHCPv6/UseNTPServers", v6dhcpParms.useNtpServers,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002305 "FQDN", fqdn,
2306 "HostName", hostname,
2307 "IPv4StaticAddresses", ipv4StaticAddresses,
2308 "IPv6DefaultGateway", ipv6DefaultGateway,
2309 "IPv6StaticAddresses", ipv6StaticAddresses,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002310 "IPv6StaticDefaultGateways", ipv6StaticDefaultGateways,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002311 "InterfaceEnabled", interfaceEnabled,
2312 "MACAddress", macAddress,
2313 "MTUSize", mtuSize,
2314 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled", ipv6AutoConfigEnabled,
2315 "StaticNameServers", staticNameServers
2316 )
2317 )
Ed Tanous002d39b2022-05-31 08:59:27 -07002318 {
2319 return;
2320 }
Ed Tanous5a8b6412024-03-27 11:30:54 -07002321 // clang-format on
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002322
Ed Tanous002d39b2022-05-31 08:59:27 -07002323 // Get single eth interface data, and call the below callback
2324 // for JSON preparation
2325 getEthernetIfaceData(
2326 ifaceId,
2327 [asyncResp, ifaceId, hostname = std::move(hostname),
2328 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2329 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2330 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2331 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002332 ipv6StaticDefaultGateway = std::move(ipv6StaticDefaultGateways),
Ed Tanous3dfed532024-03-06 14:41:27 -08002333 staticNameServers = std::move(staticNameServers), mtuSize,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002334 ipv6AutoConfigEnabled, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07002335 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
Ed Tanous3dfed532024-03-06 14:41:27 -08002336 const bool success, const EthernetInterfaceData& ethData,
Ed Tanous77179532023-02-28 10:45:28 -08002337 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002338 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanous3dfed532024-03-06 14:41:27 -08002339 const std::vector<StaticGatewayData>& ipv6GatewayData) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07002340 if (!success)
2341 {
2342 // ... otherwise return error
2343 // TODO(Pawel)consider distinguish between non
2344 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002345 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002346 ifaceId);
2347 return;
2348 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002349
Ed Tanous3dfed532024-03-06 14:41:27 -08002350 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
2351 asyncResp);
Tejas Patil35fb5312021-09-20 15:35:20 +05302352
Ed Tanous002d39b2022-05-31 08:59:27 -07002353 if (hostname)
2354 {
2355 handleHostnamePatch(*hostname, asyncResp);
2356 }
2357
Ravi Tejab10d8db2022-05-24 09:04:12 -05002358 if (ipv6AutoConfigEnabled)
2359 {
2360 handleSLAACAutoConfigPatch(ifaceId, *ipv6AutoConfigEnabled,
2361 asyncResp);
2362 }
2363
Ed Tanous002d39b2022-05-31 08:59:27 -07002364 if (fqdn)
2365 {
2366 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2367 }
2368
2369 if (macAddress)
2370 {
2371 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2372 }
2373
2374 if (ipv4StaticAddresses)
2375 {
Johnathan Mantey743eb1c2024-04-03 12:05:57 -07002376 handleIPv4StaticPatch(ifaceId, *ipv4StaticAddresses, ethData,
2377 ipv4Data, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002378 }
2379
2380 if (staticNameServers)
2381 {
2382 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2383 asyncResp);
2384 }
2385
2386 if (ipv6DefaultGateway)
2387 {
2388 messages::propertyNotWritable(asyncResp->res,
2389 "IPv6DefaultGateway");
2390 }
2391
2392 if (ipv6StaticAddresses)
2393 {
Ed Tanousddd70dc2023-03-01 16:00:27 -08002394 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
2395 ipv6Data, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002396 }
2397
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002398 if (ipv6StaticDefaultGateway)
2399 {
2400 handleIPv6DefaultGateway(ifaceId, *ipv6StaticDefaultGateway,
2401 ipv6GatewayData, asyncResp);
2402 }
2403
Ed Tanous002d39b2022-05-31 08:59:27 -07002404 if (interfaceEnabled)
2405 {
Ed Tanousd02aad32024-02-13 14:43:34 -08002406 setDbusProperty(asyncResp, "xyz.openbmc_project.Network",
2407 sdbusplus::message::object_path(
2408 "/xyz/openbmc_project/network") /
2409 ifaceId,
2410 "xyz.openbmc_project.Network.EthernetInterface",
2411 "NICEnabled", "InterfaceEnabled",
2412 *interfaceEnabled);
Ed Tanous002d39b2022-05-31 08:59:27 -07002413 }
2414
2415 if (mtuSize)
2416 {
2417 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2418 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002419 });
Ed Tanous5a8b6412024-03-27 11:30:54 -07002420 });
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002421
Ed Tanous253f11b2024-05-16 09:38:31 -07002422 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/EthernetInterfaces/<str>/")
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002423 .privileges(redfish::privileges::deleteEthernetInterface)
2424 .methods(boost::beast::http::verb::delete_)(
2425 [&app](const crow::Request& req,
2426 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous253f11b2024-05-16 09:38:31 -07002427 const std::string& managerId, const std::string& ifaceId) {
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002428 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2429 {
2430 return;
2431 }
2432
Ed Tanous253f11b2024-05-16 09:38:31 -07002433 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2434 {
2435 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2436 return;
2437 }
2438
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002439 crow::connections::systemBus->async_method_call(
2440 [asyncResp, ifaceId](const boost::system::error_code& ec,
2441 const sdbusplus::message_t& m) {
2442 afterDelete(asyncResp, ifaceId, ec, m);
Ed Tanous5a8b6412024-03-27 11:30:54 -07002443 },
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002444 "xyz.openbmc_project.Network",
2445 std::string("/xyz/openbmc_project/network/") + ifaceId,
2446 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous5a8b6412024-03-27 11:30:54 -07002447 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002448}
2449
Ed Tanous1abe55e2018-09-05 08:30:59 -07002450} // namespace redfish