blob: dd3473db8a7cd9bb9c04dc3dac3e21f715e3478d [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
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "app.hpp"
21#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080022#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "error_messages.hpp"
24#include "health.hpp"
Ed Tanous2c5875a2023-05-15 09:56:06 -070025#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "query.hpp"
27#include "registries/privilege_registry.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070028#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070030
Sunitha Harishce73d5c2023-04-07 06:46:49 -050031#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070032#include <boost/url/format.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033
George Liu7a1dbc42022-12-07 16:03:22 +080034#include <array>
Sunitha Harishce73d5c2023-04-07 06:46:49 -050035#include <memory>
Ed Tanousa24526d2018-12-10 15:17:59 -080036#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070037#include <ranges>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053038#include <regex>
George Liu7a1dbc42022-12-07 16:03:22 +080039#include <string_view>
Ed Tanous77179532023-02-28 10:45:28 -080040#include <vector>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010041
Ed Tanous1abe55e2018-09-05 08:30:59 -070042namespace redfish
43{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010044
Ed Tanous4a0cb852018-10-15 07:55:04 -070045enum class LinkType
46{
47 Local,
48 Global
49};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010050
51/**
52 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010053 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070054struct IPv4AddressData
55{
56 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070057 std::string address;
58 std::string domain;
59 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 std::string netmask;
61 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080062 LinkType linktype{};
63 bool isActive{};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010064};
65
66/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050067 * Structure for keeping IPv6 data required by Redfish
68 */
69struct IPv6AddressData
70{
71 std::string id;
72 std::string address;
73 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080074 uint8_t prefixLength = 0;
Ravi Tejae48c0fc2019-04-16 08:37:20 -050075};
Sunitha Harishce73d5c2023-04-07 06:46:49 -050076
77/**
78 * Structure for keeping static route data required by Redfish
79 */
80struct StaticGatewayData
81{
82 std::string id;
83 std::string gateway;
84 size_t prefixLength = 0;
85 std::string protocol;
86};
87
Ravi Tejae48c0fc2019-04-16 08:37:20 -050088/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010089 * Structure for keeping basic single Ethernet Interface information
90 * available from DBus
91 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070092struct EthernetInterfaceData
93{
Ed Tanous4a0cb852018-10-15 07:55:04 -070094 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053095 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080096 bool autoNeg;
Jishnu CMe4588152023-05-11 00:04:40 -050097 bool dnsv4Enabled;
98 bool dnsv6Enabled;
Ravi Teja91c441e2024-02-23 09:03:43 -060099 bool domainv4Enabled;
100 bool domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -0500101 bool ntpv4Enabled;
102 bool ntpv6Enabled;
103 bool hostNamev4Enabled;
104 bool hostNamev6Enabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800105 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700106 bool nicEnabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -0500107 bool ipv6AcceptRa;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800108 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700109 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800110 std::string hostName;
111 std::string defaultGateway;
112 std::string ipv6DefaultGateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500113 std::string ipv6StaticDefaultGateway;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800114 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800115 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500116 std::vector<std::string> nameServers;
117 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800118 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100119};
120
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700121struct DHCPParameters
122{
123 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800124 std::optional<bool> useDnsServers;
125 std::optional<bool> useNtpServers;
126 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700127 std::optional<std::string> dhcpv6OperatingMode;
128};
129
Ed Tanous4a0cb852018-10-15 07:55:04 -0700130// Helper function that changes bits netmask notation (i.e. /24)
131// into full dot notation
132inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700134 uint32_t value = 0xffffffff << (32 - bits);
135 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
136 std::to_string((value >> 16) & 0xff) + "." +
137 std::to_string((value >> 8) & 0xff) + "." +
138 std::to_string(value & 0xff);
139 return netmask;
140}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100141
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800142inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700143 bool isIPv4)
144{
145 if (isIPv4)
146 {
147 return (
148 (inputDHCP ==
149 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
150 (inputDHCP ==
151 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
152 }
153 return ((inputDHCP ==
154 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
155 (inputDHCP ==
156 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
157}
158
Ed Tanous2c70f802020-09-28 14:29:23 -0700159inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700160{
161 if (isIPv4 && isIPv6)
162 {
163 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
164 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700165 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700166 {
167 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
168 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700169 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700170 {
171 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
172 }
173 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
174}
175
Ed Tanous4a0cb852018-10-15 07:55:04 -0700176inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500177 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 bool isIPv4)
179{
180 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700182 return "Static";
183 }
184 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
185 {
186 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700187 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700188 return "IPv4LinkLocal";
189 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700190 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700191 }
192 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
193 {
194 if (isIPv4)
195 {
196 return "DHCP";
197 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700198 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700199 }
200 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
201 {
202 return "SLAAC";
203 }
204 return "";
205}
206
Ed Tanous02cad962022-06-30 16:50:15 -0700207inline bool extractEthernetInterfaceData(
208 const std::string& ethifaceId,
209 const dbus::utility::ManagedObjectType& dbusData,
210 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700212 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700213 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700214 {
Ed Tanous02cad962022-06-30 16:50:15 -0700215 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000217 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700219 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700220 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500222 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500226 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800227 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700228 if (mac != nullptr)
229 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800230 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700231 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 }
234 }
235 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
236 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500237 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700238 {
239 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500241 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800242 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 if (id != nullptr)
244 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800245 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700246 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700248 }
249 }
250 else if (ifacePair.first ==
251 "xyz.openbmc_project.Network.EthernetInterface")
252 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500253 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700254 {
255 if (propertyPair.first == "AutoNeg")
256 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700257 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800258 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700259 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700260 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800261 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700262 }
263 }
264 else if (propertyPair.first == "Speed")
265 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500266 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800267 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700268 if (speed != nullptr)
269 {
270 ethData.speed = *speed;
271 }
272 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530273 else if (propertyPair.first == "MTU")
274 {
Anthony3e7a8da2023-10-23 14:22:43 +0800275 const size_t* mtuSize =
276 std::get_if<size_t>(&propertyPair.second);
Tejas Patil35fb5312021-09-20 15:35:20 +0530277 if (mtuSize != nullptr)
278 {
279 ethData.mtuSize = *mtuSize;
280 }
281 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800282 else if (propertyPair.first == "LinkUp")
283 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500284 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800285 std::get_if<bool>(&propertyPair.second);
286 if (linkUp != nullptr)
287 {
288 ethData.linkUp = *linkUp;
289 }
290 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700291 else if (propertyPair.first == "NICEnabled")
292 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500293 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700294 std::get_if<bool>(&propertyPair.second);
295 if (nicEnabled != nullptr)
296 {
297 ethData.nicEnabled = *nicEnabled;
298 }
299 }
Ravi Tejab10d8db2022-05-24 09:04:12 -0500300 else if (propertyPair.first == "IPv6AcceptRA")
301 {
302 const bool* ipv6AcceptRa =
303 std::get_if<bool>(&propertyPair.second);
304 if (ipv6AcceptRa != nullptr)
305 {
306 ethData.ipv6AcceptRa = *ipv6AcceptRa;
307 }
308 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500309 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700310 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500311 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500312 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800313 &propertyPair.second);
314 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700315 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700316 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500317 }
318 }
319 else if (propertyPair.first == "StaticNameServers")
320 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500321 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500322 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500323 &propertyPair.second);
324 if (staticNameServers != nullptr)
325 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700326 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700327 }
328 }
manojkiraneda2a133282019-02-19 13:09:43 +0530329 else if (propertyPair.first == "DHCPEnabled")
330 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700331 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700332 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700333 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530334 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800335 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530336 }
337 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800338 else if (propertyPair.first == "DomainName")
339 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500340 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500341 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800342 &propertyPair.second);
343 if (domainNames != nullptr)
344 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700345 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800346 }
347 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500348 else if (propertyPair.first == "DefaultGateway")
349 {
350 const std::string* defaultGateway =
351 std::get_if<std::string>(&propertyPair.second);
352 if (defaultGateway != nullptr)
353 {
354 std::string defaultGatewayStr = *defaultGateway;
355 if (defaultGatewayStr.empty())
356 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800357 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500358 }
359 else
360 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800361 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500362 }
363 }
364 }
365 else if (propertyPair.first == "DefaultGateway6")
366 {
367 const std::string* defaultGateway6 =
368 std::get_if<std::string>(&propertyPair.second);
369 if (defaultGateway6 != nullptr)
370 {
371 std::string defaultGateway6Str =
372 *defaultGateway6;
373 if (defaultGateway6Str.empty())
374 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800375 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500376 "0:0:0:0:0:0:0:0";
377 }
378 else
379 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800380 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500381 defaultGateway6Str;
382 }
383 }
384 }
Ed Tanous029573d2019-02-01 10:57:49 -0800385 }
386 }
387 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700388
Jishnu CMe4588152023-05-11 00:04:40 -0500389 sdbusplus::message::object_path path(
390 "/xyz/openbmc_project/network");
391 sdbusplus::message::object_path dhcp4Path = path / ethifaceId /
392 "dhcp4";
393
394 if (sdbusplus::message::object_path(objpath.first) == dhcp4Path)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700395 {
396 if (ifacePair.first ==
397 "xyz.openbmc_project.Network.DHCPConfiguration")
398 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500399 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700400 {
401 if (propertyPair.first == "DNSEnabled")
402 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700403 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700404 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700405 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700406 {
Jishnu CMe4588152023-05-11 00:04:40 -0500407 ethData.dnsv4Enabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700408 }
409 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600410 else if (propertyPair.first == "DomainEnabled")
411 {
412 const bool* domainEnabled =
413 std::get_if<bool>(&propertyPair.second);
414 if (domainEnabled != nullptr)
415 {
416 ethData.domainv4Enabled = *domainEnabled;
417 }
418 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700419 else if (propertyPair.first == "NTPEnabled")
420 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700421 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700422 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700423 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700424 {
Jishnu CMe4588152023-05-11 00:04:40 -0500425 ethData.ntpv4Enabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700426 }
427 }
428 else if (propertyPair.first == "HostNameEnabled")
429 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700430 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700431 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700432 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700433 {
Jishnu CMe4588152023-05-11 00:04:40 -0500434 ethData.hostNamev4Enabled = *hostNameEnabled;
435 }
436 }
437 }
438 }
439 }
440
441 sdbusplus::message::object_path dhcp6Path = path / ethifaceId /
442 "dhcp6";
443
444 if (sdbusplus::message::object_path(objpath.first) == dhcp6Path)
445 {
446 if (ifacePair.first ==
447 "xyz.openbmc_project.Network.DHCPConfiguration")
448 {
449 for (const auto& propertyPair : ifacePair.second)
450 {
451 if (propertyPair.first == "DNSEnabled")
452 {
453 const bool* dnsEnabled =
454 std::get_if<bool>(&propertyPair.second);
455 if (dnsEnabled != nullptr)
456 {
457 ethData.dnsv6Enabled = *dnsEnabled;
458 }
459 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600460 if (propertyPair.first == "DomainEnabled")
461 {
462 const bool* domainEnabled =
463 std::get_if<bool>(&propertyPair.second);
464 if (domainEnabled != nullptr)
465 {
466 ethData.domainv6Enabled = *domainEnabled;
467 }
468 }
Jishnu CMe4588152023-05-11 00:04:40 -0500469 else if (propertyPair.first == "NTPEnabled")
470 {
471 const bool* ntpEnabled =
472 std::get_if<bool>(&propertyPair.second);
473 if (ntpEnabled != nullptr)
474 {
475 ethData.ntpv6Enabled = *ntpEnabled;
476 }
477 }
478 else if (propertyPair.first == "HostNameEnabled")
479 {
480 const bool* hostNameEnabled =
481 std::get_if<bool>(&propertyPair.second);
482 if (hostNameEnabled != nullptr)
483 {
484 ethData.hostNamev6Enabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700485 }
486 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700487 }
488 }
489 }
Ed Tanous029573d2019-02-01 10:57:49 -0800490 // System configuration shows up in the global namespace, so no need
491 // to check eth number
492 if (ifacePair.first ==
493 "xyz.openbmc_project.Network.SystemConfiguration")
494 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500495 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800496 {
497 if (propertyPair.first == "HostName")
498 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500499 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500500 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800501 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700502 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800503 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800504 }
505 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700506 }
507 }
508 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700509 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700510 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700511}
512
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500513// Helper function that extracts data for single ethernet ipv6 address
Ed Tanous77179532023-02-28 10:45:28 -0800514inline void extractIPV6Data(const std::string& ethifaceId,
515 const dbus::utility::ManagedObjectType& dbusData,
516 std::vector<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500517{
Patrick Williams89492a12023-05-10 07:51:34 -0500518 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
519 ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500520
521 // Since there might be several IPv6 configurations aligned with
522 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000523 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500524 {
525 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800526 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500527 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800528 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500529 {
530 if (interface.first == "xyz.openbmc_project.Network.IP")
531 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700532 auto type = std::ranges::find_if(interface.second,
533 [](const auto& property) {
Tony Lee353163e2022-11-23 11:06:10 +0800534 return property.first == "Type";
535 });
536 if (type == interface.second.end())
537 {
538 continue;
539 }
540
541 const std::string* typeStr =
542 std::get_if<std::string>(&type->second);
543
544 if (typeStr == nullptr ||
545 (*typeStr !=
546 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
547 {
548 continue;
549 }
550
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500551 // Instance IPv6AddressData structure, and set as
552 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800553 IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700554 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800555 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800556 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500557 {
558 if (property.first == "Address")
559 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500560 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500561 std::get_if<std::string>(&property.second);
562 if (address != nullptr)
563 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700564 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500565 }
566 }
567 else if (property.first == "Origin")
568 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500569 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500570 std::get_if<std::string>(&property.second);
571 if (origin != nullptr)
572 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700573 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500574 translateAddressOriginDbusToRedfish(*origin,
575 false);
576 }
577 }
578 else if (property.first == "PrefixLength")
579 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500580 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500581 std::get_if<uint8_t>(&property.second);
582 if (prefix != nullptr)
583 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700584 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500585 }
586 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600587 else if (property.first == "Type" ||
588 property.first == "Gateway")
589 {
590 // Type & Gateway is not used
591 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500592 else
593 {
Ed Tanous62598e32023-07-17 17:06:25 -0700594 BMCWEB_LOG_ERROR(
595 "Got extra property: {} on the {} object",
596 property.first, objpath.first.str);
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500597 }
598 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500599 }
600 }
601 }
602 }
603}
604
Ed Tanous4a0cb852018-10-15 07:55:04 -0700605// Helper function that extracts data for single ethernet ipv4 address
Ed Tanous77179532023-02-28 10:45:28 -0800606inline void extractIPData(const std::string& ethifaceId,
607 const dbus::utility::ManagedObjectType& dbusData,
608 std::vector<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700609{
Patrick Williams89492a12023-05-10 07:51:34 -0500610 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
611 ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700612
613 // Since there might be several IPv4 configurations aligned with
614 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000615 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700616 {
617 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800618 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700619 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800620 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700621 {
622 if (interface.first == "xyz.openbmc_project.Network.IP")
623 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700624 auto type = std::ranges::find_if(interface.second,
625 [](const auto& property) {
Tony Lee353163e2022-11-23 11:06:10 +0800626 return property.first == "Type";
627 });
628 if (type == interface.second.end())
629 {
630 continue;
631 }
632
633 const std::string* typeStr =
634 std::get_if<std::string>(&type->second);
635
636 if (typeStr == nullptr ||
637 (*typeStr !=
638 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
639 {
640 continue;
641 }
642
Ed Tanous4a0cb852018-10-15 07:55:04 -0700643 // Instance IPv4AddressData structure, and set as
644 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800645 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700646 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800647 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800648 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700649 {
650 if (property.first == "Address")
651 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500652 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800653 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700654 if (address != nullptr)
655 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700656 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700657 }
658 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700659 else if (property.first == "Origin")
660 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500661 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800662 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700663 if (origin != nullptr)
664 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700665 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700666 translateAddressOriginDbusToRedfish(*origin,
667 true);
668 }
669 }
670 else if (property.first == "PrefixLength")
671 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500672 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800673 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700674 if (mask != nullptr)
675 {
676 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700677 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700678 }
679 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600680 else if (property.first == "Type" ||
681 property.first == "Gateway")
682 {
683 // Type & Gateway is not used
684 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700685 else
686 {
Ed Tanous62598e32023-07-17 17:06:25 -0700687 BMCWEB_LOG_ERROR(
688 "Got extra property: {} on the {} object",
689 property.first, objpath.first.str);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700690 }
691 }
692 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700693 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700694 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700695 ? LinkType::Local
696 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700697 }
698 }
699 }
700 }
701}
702
703/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700704 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700705 *
706 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700707 * @param[in] ipHash DBus Hash id of IP that should be deleted
708 * @param[io] asyncResp Response object that will be returned to client
709 *
710 * @return None
711 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600712inline void deleteIPAddress(const std::string& ifaceId,
713 const std::string& ipHash,
714 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700715{
716 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800717 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700718 if (ec)
719 {
720 messages::internalError(asyncResp->res);
721 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500722 },
Ed Tanous4a0cb852018-10-15 07:55:04 -0700723 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600724 "/xyz/openbmc_project/network/" + ifaceId + ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700725 "xyz.openbmc_project.Object.Delete", "Delete");
726}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700727
Gunnar Mills244b6d52021-04-12 15:44:23 -0500728inline void updateIPv4DefaultGateway(
729 const std::string& ifaceId, const std::string& gateway,
730 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500731{
Ed Tanousd02aad32024-02-13 14:43:34 -0800732 setDbusProperty(
733 asyncResp, "xyz.openbmc_project.Network",
734 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
735 ifaceId,
Ravi Teja9010ec22019-08-01 23:30:25 -0500736 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanousd02aad32024-02-13 14:43:34 -0800737 "Gateway", gateway);
Ravi Teja9010ec22019-08-01 23:30:25 -0500738}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700739/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700740 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700741 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700742 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
743 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
744 * @param[in] gateway IPv4 address of this interfaces gateway
745 * @param[in] address IPv4 address to assign to this interface
746 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700747 *
748 * @return None
749 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000750inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
751 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800752 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700753{
Patrick Williams5a39f772023-10-20 11:20:21 -0500754 auto createIpHandler = [asyncResp, ifaceId,
755 gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500756 if (ec)
757 {
758 messages::internalError(asyncResp->res);
759 return;
760 }
761 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
762 };
763
Ed Tanous4a0cb852018-10-15 07:55:04 -0700764 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500765 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700766 "/xyz/openbmc_project/network/" + ifaceId,
767 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700768 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700769 gateway);
770}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500771
772/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700773 * @brief Deletes the IPv6 entry for this interface and creates a replacement
774 * static IPv6 entry
775 *
776 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
777 * @param[in] id The unique hash entry identifying the DBus entry
778 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
779 * @param[in] address IPv6 address to assign to this interface
780 * @param[io] asyncResp Response object that will be returned to client
781 *
782 * @return None
783 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600784
785enum class IpVersion
786{
787 IpV4,
788 IpV6
789};
790
791inline void deleteAndCreateIPAddress(
792 IpVersion version, const std::string& ifaceId, const std::string& id,
793 uint8_t prefixLength, const std::string& address,
794 const std::string& gateway,
795 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700796{
797 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600798 [asyncResp, version, ifaceId, address, prefixLength,
799 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700800 if (ec)
801 {
802 messages::internalError(asyncResp->res);
803 }
Ravi Teja9c5e5852023-02-26 21:33:52 -0600804 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
805 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous002d39b2022-05-31 08:59:27 -0700806 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800807 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700808 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700809 {
810 messages::internalError(asyncResp->res);
811 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500812 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700813 "xyz.openbmc_project.Network",
814 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600815 "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
816 prefixLength, gateway);
Patrick Williams5a39f772023-10-20 11:20:21 -0500817 },
Johnathan Mantey01784822019-06-18 12:44:21 -0700818 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600819 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700820 "xyz.openbmc_project.Object.Delete", "Delete");
821}
822
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500823inline bool extractIPv6DefaultGatewayData(
824 const std::string& ethifaceId,
825 const dbus::utility::ManagedObjectType& dbusData,
826 std::vector<StaticGatewayData>& staticGatewayConfig)
827{
828 std::string staticGatewayPathStart("/xyz/openbmc_project/network/");
829 staticGatewayPathStart += ethifaceId;
830
831 for (const auto& objpath : dbusData)
832 {
833 if (!std::string_view(objpath.first.str)
834 .starts_with(staticGatewayPathStart))
835 {
836 continue;
837 }
838 for (const auto& interface : objpath.second)
839 {
840 if (interface.first != "xyz.openbmc_project.Network.StaticGateway")
841 {
842 continue;
843 }
844 StaticGatewayData& staticGateway =
845 staticGatewayConfig.emplace_back();
846 staticGateway.id = objpath.first.filename();
847
848 bool success = sdbusplus::unpackPropertiesNoThrow(
849 redfish::dbus_utils::UnpackErrorPrinter(), interface.second,
850 "Gateway", staticGateway.gateway, "PrefixLength",
851 staticGateway.prefixLength, "ProtocolType",
852 staticGateway.protocol);
853 if (!success)
854 {
855 return false;
856 }
857 }
858 }
859 return true;
860}
861
Johnathan Mantey01784822019-06-18 12:44:21 -0700862/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500863 * @brief Creates IPv6 with given data
864 *
865 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500866 * @param[in] prefixLength Prefix length that needs to be added
867 * @param[in] address IP address that needs to be added
868 * @param[io] asyncResp Response object that will be returned to client
869 *
870 * @return None
871 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500872inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
873 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800874 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500875{
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500876 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
877 path /= ifaceId;
878
Patrick Williams5a39f772023-10-20 11:20:21 -0500879 auto createIpHandler = [asyncResp,
880 address](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500881 if (ec)
882 {
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500883 if (ec == boost::system::errc::io_error)
884 {
885 messages::propertyValueFormatError(asyncResp->res, address,
886 "Address");
887 }
888 else
889 {
890 messages::internalError(asyncResp->res);
891 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500892 }
893 };
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500894 // Passing null for gateway, as per redfish spec IPv6StaticAddresses
895 // object does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500896 crow::connections::systemBus->async_method_call(
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500897 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500898 "xyz.openbmc_project.Network.IP.Create", "IP",
899 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
900 "");
901}
902
Ed Tanous4a0cb852018-10-15 07:55:04 -0700903/**
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500904 * @brief Deletes given IPv6 Static Gateway
905 *
906 * @param[in] ifaceId Id of interface whose IP should be deleted
907 * @param[in] ipHash DBus Hash id of IP that should be deleted
908 * @param[io] asyncResp Response object that will be returned to client
909 *
910 * @return None
911 */
912inline void
913 deleteIPv6Gateway(std::string_view gatewayId,
914 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
915{
916 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
917 path /= gatewayId;
918 crow::connections::systemBus->async_method_call(
919 [asyncResp](const boost::system::error_code& ec) {
920 if (ec)
921 {
922 messages::internalError(asyncResp->res);
923 }
924 },
925 "xyz.openbmc_project.Network", path,
926 "xyz.openbmc_project.Object.Delete", "Delete");
927}
928
929/**
930 * @brief Creates IPv6 static default gateway with given data
931 *
932 * @param[in] ifaceId Id of interface whose IP should be added
933 * @param[in] prefixLength Prefix length that needs to be added
934 * @param[in] gateway Gateway address that needs to be added
935 * @param[io] asyncResp Response object that will be returned to client
936 *
937 * @return None
938 */
939inline void createIPv6DefaultGateway(
940 std::string_view ifaceId, size_t prefixLength, std::string_view gateway,
941 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
942{
943 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
944 path /= ifaceId;
945 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
946 if (ec)
947 {
948 messages::internalError(asyncResp->res);
949 }
950 };
951 crow::connections::systemBus->async_method_call(
952 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
953 "xyz.openbmc_project.Network.StaticGateway.Create", "StaticGateway",
954 gateway, prefixLength, "xyz.openbmc_project.Network.IP.Protocol.IPv6");
955}
956
957/**
958 * @brief Deletes the IPv6 default gateway entry for this interface and
959 * creates a replacement IPv6 default gateway entry
960 *
961 * @param[in] ifaceId Id of interface upon which to create the IPv6
962 * entry
963 * @param[in] gateway IPv6 gateway to assign to this interface
964 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
965 * @param[io] asyncResp Response object that will be returned to client
966 *
967 * @return None
968 */
969inline void deleteAndCreateIPv6DefaultGateway(
970 std::string_view ifaceId, std::string_view gatewayId,
971 std::string_view gateway, size_t prefixLength,
972 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
973{
974 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
975 path /= gatewayId;
976 crow::connections::systemBus->async_method_call(
977 [asyncResp, ifaceId, gateway,
978 prefixLength](const boost::system::error_code& ec) {
979 if (ec)
980 {
981 messages::internalError(asyncResp->res);
982 return;
983 }
984 createIPv6DefaultGateway(ifaceId, prefixLength, gateway, asyncResp);
985 },
986 "xyz.openbmc_project.Network", path,
987 "xyz.openbmc_project.Object.Delete", "Delete");
988}
989
990/**
991 * @brief Sets IPv6 default gateway with given data
992 *
993 * @param[in] ifaceId Id of interface whose gateway should be added
994 * @param[in] input Contains address that needs to be added
995 * @param[in] staticGatewayData Current static gateways in the system
996 * @param[io] asyncResp Response object that will be returned to client
997 *
998 * @return None
999 */
1000
1001inline void handleIPv6DefaultGateway(
1002 const std::string& ifaceId, const nlohmann::json::array_t& input,
1003 const std::vector<StaticGatewayData>& staticGatewayData,
1004 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1005{
1006 size_t entryIdx = 1;
1007 std::vector<StaticGatewayData>::const_iterator staticGatewayEntry =
1008 staticGatewayData.begin();
1009
1010 for (const nlohmann::json& thisJson : input)
1011 {
1012 // find the next gateway entry
1013 while (staticGatewayEntry != staticGatewayData.end())
1014 {
1015 if (staticGatewayEntry->protocol ==
1016 "xyz.openbmc_project.Network.IP.Protocol.IPv6")
1017 {
1018 break;
1019 }
1020 staticGatewayEntry++;
1021 }
1022 std::string pathString = "IPv6StaticDefaultGateways/" +
1023 std::to_string(entryIdx);
1024 if (thisJson.is_null())
1025 {
1026 if (staticGatewayEntry == staticGatewayData.end())
1027 {
1028 messages::resourceCannotBeDeleted(asyncResp->res);
1029 return;
1030 }
1031 deleteIPv6Gateway(staticGatewayEntry->id, asyncResp);
1032 return;
1033 }
1034 if (thisJson.is_object() && thisJson.empty())
1035 {
1036 // Do nothing, but make sure the entry exists.
1037 if (staticGatewayEntry == staticGatewayData.end())
1038 {
1039 messages::propertyValueFormatError(asyncResp->res, thisJson,
1040 pathString);
1041 return;
1042 }
1043 }
1044 std::optional<std::string> address;
1045 std::optional<size_t> prefixLength;
1046
1047 nlohmann::json thisJsonCopy = thisJson;
1048 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1049 address, "PrefixLength", prefixLength))
1050 {
1051 return;
1052 }
1053 const std::string* addr = nullptr;
1054 size_t prefix = 0;
1055 if (address)
1056 {
1057 addr = &(*address);
1058 }
1059 else if (staticGatewayEntry != staticGatewayData.end())
1060 {
1061 addr = &(staticGatewayEntry->gateway);
1062 }
1063 else
1064 {
1065 messages::propertyMissing(asyncResp->res, pathString + "/Address");
1066 return;
1067 }
1068 if (prefixLength)
1069 {
1070 prefix = *prefixLength;
1071 }
1072 else if (staticGatewayEntry != staticGatewayData.end())
1073 {
1074 prefix = staticGatewayEntry->prefixLength;
1075 }
1076 else
1077 {
1078 messages::propertyMissing(asyncResp->res,
1079 pathString + "/PrefixLength");
1080 return;
1081 }
1082 if (staticGatewayEntry != staticGatewayData.end())
1083 {
1084 deleteAndCreateIPv6DefaultGateway(ifaceId, staticGatewayEntry->id,
1085 *addr, prefix, asyncResp);
1086 staticGatewayEntry++;
1087 }
1088 else
1089 {
1090 createIPv6DefaultGateway(ifaceId, prefix, *addr, asyncResp);
1091 }
1092 entryIdx++;
1093 }
1094}
1095
1096/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001097 * Function that retrieves all properties for given Ethernet Interface
1098 * Object
1099 * from EntityManager Network Manager
1100 * @param ethiface_id a eth interface id to query on DBus
1101 * @param callback a function that shall be called to convert Dbus output
1102 * into JSON
1103 */
1104template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +00001105void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001106 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001107{
George Liuf5892d02023-03-01 10:37:08 +08001108 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1109 dbus::utility::getManagedObjects(
1110 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001111 [ethifaceId{std::string{ethifaceId}},
1112 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous8b242752023-06-27 17:17:13 -07001113 const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001114 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001115 EthernetInterfaceData ethData{};
Ed Tanous77179532023-02-28 10:45:28 -08001116 std::vector<IPv4AddressData> ipv4Data;
1117 std::vector<IPv6AddressData> ipv6Data;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001118 std::vector<StaticGatewayData> ipv6GatewayData;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001119
Ed Tanous8b242752023-06-27 17:17:13 -07001120 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001122 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 return;
1124 }
1125
1126 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
1127 if (!found)
1128 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001129 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 return;
1131 }
1132
1133 extractIPData(ethifaceId, resp, ipv4Data);
1134 // Fix global GW
1135 for (IPv4AddressData& ipv4 : ipv4Data)
1136 {
1137 if (((ipv4.linktype == LinkType::Global) &&
1138 (ipv4.gateway == "0.0.0.0")) ||
1139 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001140 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001141 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001142 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001143 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001144
Ed Tanous002d39b2022-05-31 08:59:27 -07001145 extractIPV6Data(ethifaceId, resp, ipv6Data);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001146 if (!extractIPv6DefaultGatewayData(ethifaceId, resp, ipv6GatewayData))
1147 {
1148 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1149 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001150 // Finally make a callback with useful data
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001151 callback(true, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Patrick Williams5a39f772023-10-20 11:20:21 -05001152 });
Ed Tanous271584a2019-07-09 16:24:22 -07001153}
Ed Tanous4a0cb852018-10-15 07:55:04 -07001154
1155/**
1156 * Function that retrieves all Ethernet Interfaces available through Network
1157 * Manager
1158 * @param callback a function that shall be called to convert Dbus output
1159 * into JSON.
1160 */
1161template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001162void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001163{
George Liuf5892d02023-03-01 10:37:08 +08001164 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1165 dbus::utility::getManagedObjects(
1166 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001167 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous8b242752023-06-27 17:17:13 -07001168 const boost::system::error_code& ec,
George Liuf5892d02023-03-01 10:37:08 +08001169 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001170 // Callback requires vector<string> to retrieve all available
1171 // ethernet interfaces
Ed Tanous77179532023-02-28 10:45:28 -08001172 std::vector<std::string> ifaceList;
Ed Tanous002d39b2022-05-31 08:59:27 -07001173 ifaceList.reserve(resp.size());
Ed Tanous8b242752023-06-27 17:17:13 -07001174 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001175 {
1176 callback(false, ifaceList);
1177 return;
1178 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001179
Ed Tanous002d39b2022-05-31 08:59:27 -07001180 // Iterate over all retrieved ObjectPaths.
1181 for (const auto& objpath : resp)
1182 {
1183 // And all interfaces available for certain ObjectPath.
1184 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001185 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 // If interface is
1187 // xyz.openbmc_project.Network.EthernetInterface, this is
1188 // what we're looking for.
1189 if (interface.first ==
1190 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -07001191 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 std::string ifaceId = objpath.first.filename();
1193 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001194 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001196 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001197 // and put it into output vector.
Ed Tanous77179532023-02-28 10:45:28 -08001198 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001199 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001200 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001201 }
Ed Tanous2c5875a2023-05-15 09:56:06 -07001202
Ed Tanous3544d2a2023-08-06 18:12:20 -07001203 std::ranges::sort(ifaceList, AlphanumLess<std::string>());
Ed Tanous2c5875a2023-05-15 09:56:06 -07001204
Ed Tanous002d39b2022-05-31 08:59:27 -07001205 // Finally make a callback with useful data
1206 callback(true, ifaceList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001207 });
Ed Tanous271584a2019-07-09 16:24:22 -07001208}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001209
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001210inline void
1211 handleHostnamePatch(const std::string& hostname,
1212 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001213{
Ed Tanousbf648f72021-06-03 15:00:14 -07001214 // SHOULD handle host names of up to 255 characters(RFC 1123)
1215 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001216 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001217 messages::propertyValueFormatError(asyncResp->res, hostname,
1218 "HostName");
1219 return;
1220 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001221 setDbusProperty(
1222 asyncResp, "xyz.openbmc_project.Network",
1223 sdbusplus::message::object_path("/xyz/openbmc_project/network/config"),
1224 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1225 "HostName", hostname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001226}
1227
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001228inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301229 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1230 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1231{
Ed Tanousd02aad32024-02-13 14:43:34 -08001232 sdbusplus::message::object_path objPath("/xyz/openbmc_project/network");
1233 objPath /= ifaceId;
1234 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", objPath,
1235 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1236 "MTUSize", mtuSize);
Tejas Patil35fb5312021-09-20 15:35:20 +05301237}
1238
1239inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001240 handleDomainnamePatch(const std::string& ifaceId,
1241 const std::string& domainname,
1242 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001243{
1244 std::vector<std::string> vectorDomainname = {domainname};
Ed Tanousd02aad32024-02-13 14:43:34 -08001245 setDbusProperty(
1246 asyncResp, "xyz.openbmc_project.Network",
1247 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1248 ifaceId,
1249 "xyz.openbmc_project.Network.EthernetInterface", "DomainName", "FQDN",
1250 vectorDomainname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001251}
1252
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001253inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001254{
1255 // A valid host name can never have the dotted-decimal form (RFC 1123)
Ed Tanous3544d2a2023-08-06 18:12:20 -07001256 if (std::ranges::all_of(hostname, ::isdigit))
Ed Tanousbf648f72021-06-03 15:00:14 -07001257 {
1258 return false;
1259 }
1260 // Each label(hostname/subdomains) within a valid FQDN
1261 // MUST handle host names of up to 63 characters (RFC 1123)
1262 // labels cannot start or end with hyphens (RFC 952)
1263 // labels can start with numbers (RFC 1123)
Ed Tanous4b242742023-05-11 09:51:51 -07001264 const static std::regex pattern(
Ed Tanousbf648f72021-06-03 15:00:14 -07001265 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1266
1267 return std::regex_match(hostname, pattern);
1268}
1269
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001270inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001271{
1272 // Can have multiple subdomains
1273 // Top Level Domain's min length is 2 character
Ed Tanous4b242742023-05-11 09:51:51 -07001274 const static std::regex pattern(
George Liu0fda0f12021-11-16 10:06:17 +08001275 "^([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 -07001276
1277 return std::regex_match(domainname, pattern);
1278}
1279
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001280inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1281 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001282{
1283 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1284 if (fqdn.length() > 255)
1285 {
1286 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1287 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001288 }
1289
Ed Tanousbf648f72021-06-03 15:00:14 -07001290 size_t pos = fqdn.find('.');
1291 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001292 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001293 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1294 return;
1295 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001296
Ed Tanousbf648f72021-06-03 15:00:14 -07001297 std::string hostname;
1298 std::string domainname;
1299 domainname = (fqdn).substr(pos + 1);
1300 hostname = (fqdn).substr(0, pos);
1301
1302 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1303 {
1304 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1305 return;
1306 }
1307
1308 handleHostnamePatch(hostname, asyncResp);
1309 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1310}
1311
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001312inline void
1313 handleMACAddressPatch(const std::string& ifaceId,
1314 const std::string& macAddress,
1315 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001316{
Ed Tanousd02aad32024-02-13 14:43:34 -08001317 setDbusProperty(
1318 asyncResp, "xyz.openbmc_project.Network",
1319 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1320 ifaceId,
1321 "xyz.openbmc_project.Network.MACAddress", "MACAddress", "MACAddress",
1322 macAddress);
Ed Tanousbf648f72021-06-03 15:00:14 -07001323}
1324
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001325inline void setDHCPEnabled(const std::string& ifaceId,
1326 const std::string& propertyName, const bool v4Value,
1327 const bool v6Value,
1328 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001329{
1330 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
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.EthernetInterface", propertyName, "DHCPv4",
1336 dhcp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001337}
1338
Jishnu CMe4588152023-05-11 00:04:40 -05001339enum class NetworkType
1340{
1341 dhcp4,
1342 dhcp6
1343};
1344
1345inline void setDHCPConfig(const std::string& propertyName, const bool& value,
1346 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1347 const std::string& ethifaceId, NetworkType type)
Ed Tanousbf648f72021-06-03 15:00:14 -07001348{
Ed Tanous62598e32023-07-17 17:06:25 -07001349 BMCWEB_LOG_DEBUG("{} = {}", propertyName, value);
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001350 std::string redfishPropertyName;
Jishnu CMe4588152023-05-11 00:04:40 -05001351 sdbusplus::message::object_path path("/xyz/openbmc_project/network/");
1352 path /= ethifaceId;
1353
1354 if (type == NetworkType::dhcp4)
1355 {
1356 path /= "dhcp4";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001357 redfishPropertyName = "DHCPv4";
Jishnu CMe4588152023-05-11 00:04:40 -05001358 }
1359 else
1360 {
1361 path /= "dhcp6";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001362 redfishPropertyName = "DHCPv6";
Jishnu CMe4588152023-05-11 00:04:40 -05001363 }
1364
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001365 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1366 "xyz.openbmc_project.Network.DHCPConfiguration",
1367 propertyName, redfishPropertyName, value);
Ed Tanousbf648f72021-06-03 15:00:14 -07001368}
1369
Ravi Tejab10d8db2022-05-24 09:04:12 -05001370inline void handleSLAACAutoConfigPatch(
1371 const std::string& ifaceId, bool ipv6AutoConfigEnabled,
1372 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1373{
1374 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1375 path /= ifaceId;
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001376 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1377 "xyz.openbmc_project.Network.EthernetInterface",
1378 "IPv6AcceptRA",
1379 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
1380 ipv6AutoConfigEnabled);
Ravi Tejab10d8db2022-05-24 09:04:12 -05001381}
1382
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001383inline void handleDHCPPatch(const std::string& ifaceId,
1384 const EthernetInterfaceData& ethData,
1385 const DHCPParameters& v4dhcpParms,
1386 const DHCPParameters& v6dhcpParms,
1387 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001388{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001389 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1390 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001391
1392 bool nextv4DHCPState =
1393 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1394
1395 bool nextv6DHCPState{};
1396 if (v6dhcpParms.dhcpv6OperatingMode)
1397 {
Ravi Tejab10d8db2022-05-24 09:04:12 -05001398 if ((*v6dhcpParms.dhcpv6OperatingMode != "Enabled") &&
Ed Tanousbf648f72021-06-03 15:00:14 -07001399 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1400 {
1401 messages::propertyValueFormatError(asyncResp->res,
1402 *v6dhcpParms.dhcpv6OperatingMode,
1403 "OperatingMode");
1404 return;
1405 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05001406 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Enabled");
Ed Tanousbf648f72021-06-03 15:00:14 -07001407 }
1408 else
1409 {
1410 nextv6DHCPState = ipv6Active;
1411 }
1412
Jishnu CMe4588152023-05-11 00:04:40 -05001413 bool nextDNSv4 = ethData.dnsv4Enabled;
1414 bool nextDNSv6 = ethData.dnsv6Enabled;
1415 if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001416 {
Jishnu CMe4588152023-05-11 00:04:40 -05001417 nextDNSv4 = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001418 }
Jishnu CMe4588152023-05-11 00:04:40 -05001419 if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001420 {
Jishnu CMe4588152023-05-11 00:04:40 -05001421 nextDNSv6 = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001422 }
1423
Jishnu CMe4588152023-05-11 00:04:40 -05001424 bool nextNTPv4 = ethData.ntpv4Enabled;
1425 bool nextNTPv6 = ethData.ntpv6Enabled;
1426 if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001427 {
Jishnu CMe4588152023-05-11 00:04:40 -05001428 nextNTPv4 = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001429 }
Jishnu CMe4588152023-05-11 00:04:40 -05001430 if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001431 {
Jishnu CMe4588152023-05-11 00:04:40 -05001432 nextNTPv6 = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001433 }
1434
Ravi Teja91c441e2024-02-23 09:03:43 -06001435 bool nextUsev4Domain = ethData.domainv4Enabled;
1436 bool nextUsev6Domain = ethData.domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -05001437 if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001438 {
Jishnu CMe4588152023-05-11 00:04:40 -05001439 nextUsev4Domain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001440 }
Jishnu CMe4588152023-05-11 00:04:40 -05001441 if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001442 {
Jishnu CMe4588152023-05-11 00:04:40 -05001443 nextUsev6Domain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001444 }
1445
Ed Tanous62598e32023-07-17 17:06:25 -07001446 BMCWEB_LOG_DEBUG("set DHCPEnabled...");
Ed Tanousbf648f72021-06-03 15:00:14 -07001447 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1448 asyncResp);
Ed Tanous62598e32023-07-17 17:06:25 -07001449 BMCWEB_LOG_DEBUG("set DNSEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001450 setDHCPConfig("DNSEnabled", nextDNSv4, asyncResp, ifaceId,
1451 NetworkType::dhcp4);
Ed Tanous62598e32023-07-17 17:06:25 -07001452 BMCWEB_LOG_DEBUG("set NTPEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001453 setDHCPConfig("NTPEnabled", nextNTPv4, asyncResp, ifaceId,
1454 NetworkType::dhcp4);
Ravi Teja91c441e2024-02-23 09:03:43 -06001455 BMCWEB_LOG_DEBUG("set DomainEnabled...");
1456 setDHCPConfig("DomainEnabled", nextUsev4Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001457 NetworkType::dhcp4);
1458 BMCWEB_LOG_DEBUG("set DNSEnabled for dhcp6...");
1459 setDHCPConfig("DNSEnabled", nextDNSv6, asyncResp, ifaceId,
1460 NetworkType::dhcp6);
1461 BMCWEB_LOG_DEBUG("set NTPEnabled for dhcp6...");
1462 setDHCPConfig("NTPEnabled", nextNTPv6, asyncResp, ifaceId,
1463 NetworkType::dhcp6);
Ravi Teja91c441e2024-02-23 09:03:43 -06001464 BMCWEB_LOG_DEBUG("set DomainEnabled for dhcp6...");
1465 setDHCPConfig("DomainEnabled", nextUsev6Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001466 NetworkType::dhcp6);
Ed Tanousbf648f72021-06-03 15:00:14 -07001467}
1468
Ed Tanous77179532023-02-28 10:45:28 -08001469inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1470 const std::vector<IPv4AddressData>::const_iterator& head,
1471 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001472{
1473 return std::find_if(head, end, [](const IPv4AddressData& value) {
1474 return value.origin == "Static";
1475 });
1476}
1477
Ed Tanous77179532023-02-28 10:45:28 -08001478inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1479 const std::vector<IPv6AddressData>::const_iterator& head,
1480 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001481{
1482 return std::find_if(head, end, [](const IPv6AddressData& value) {
1483 return value.origin == "Static";
1484 });
1485}
1486
Ed Tanous77179532023-02-28 10:45:28 -08001487inline void
Ed Tanousddd70dc2023-03-01 16:00:27 -08001488 handleIPv4StaticPatch(const std::string& ifaceId,
1489 nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001490 const std::vector<IPv4AddressData>& ipv4Data,
1491 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001492{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001493 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001494 {
Ed Tanous2e8c4bd2022-06-27 12:59:12 -07001495 messages::propertyValueTypeError(asyncResp->res, input,
1496 "IPv4StaticAddresses");
Ed Tanousbf648f72021-06-03 15:00:14 -07001497 return;
1498 }
1499
1500 unsigned entryIdx = 1;
1501 // Find the first static IP address currently active on the NIC and
1502 // match it to the first JSON element in the IPv4StaticAddresses array.
1503 // Match each subsequent JSON element to the next static IP programmed
1504 // into the NIC.
Ed Tanous77179532023-02-28 10:45:28 -08001505 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001506 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1507
1508 for (nlohmann::json& thisJson : input)
1509 {
Patrick Williams89492a12023-05-10 07:51:34 -05001510 std::string pathString = "IPv4StaticAddresses/" +
1511 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001512
1513 if (!thisJson.is_null() && !thisJson.empty())
1514 {
1515 std::optional<std::string> address;
1516 std::optional<std::string> subnetMask;
1517 std::optional<std::string> gateway;
1518
1519 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1520 address, "SubnetMask", subnetMask,
1521 "Gateway", gateway))
1522 {
Ed Tanousf818b042022-06-27 13:17:35 -07001523 messages::propertyValueFormatError(asyncResp->res, thisJson,
1524 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001525 return;
1526 }
1527
1528 // Find the address/subnet/gateway values. Any values that are
1529 // not explicitly provided are assumed to be unmodified from the
1530 // current state of the interface. Merge existing state into the
1531 // current request.
Ed Tanousbf648f72021-06-03 15:00:14 -07001532 if (address)
1533 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001534 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001535 {
1536 messages::propertyValueFormatError(asyncResp->res, *address,
1537 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001538 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001539 }
1540 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001541 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001542 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001543 address = (nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001544 }
1545 else
1546 {
1547 messages::propertyMissing(asyncResp->res,
1548 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001549 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001550 }
1551
Ed Tanouse01d0c32023-06-30 13:21:32 -07001552 uint8_t prefixLength = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001553 if (subnetMask)
1554 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001555 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1556 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001557 {
1558 messages::propertyValueFormatError(
1559 asyncResp->res, *subnetMask,
1560 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001561 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001562 }
1563 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001564 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001565 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001566 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1567 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001568 {
1569 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001570 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001571 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001572 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001573 }
1574 }
1575 else
1576 {
1577 messages::propertyMissing(asyncResp->res,
1578 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001579 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001580 }
1581
1582 if (gateway)
1583 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001584 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001585 {
1586 messages::propertyValueFormatError(asyncResp->res, *gateway,
1587 pathString + "/Gateway");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001588 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001589 }
1590 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001591 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001592 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001593 gateway = nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001594 }
1595 else
1596 {
1597 messages::propertyMissing(asyncResp->res,
1598 pathString + "/Gateway");
Ed Tanousbf648f72021-06-03 15:00:14 -07001599 return;
1600 }
1601
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001602 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001603 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001604 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
Ed Tanous77eb0152023-09-06 10:19:18 -07001605 nicIpEntry->id, prefixLength, *address,
1606 *gateway, asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001607 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1608 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001609 }
1610 else
1611 {
1612 createIPv4(ifaceId, prefixLength, *gateway, *address,
1613 asyncResp);
1614 }
1615 entryIdx++;
1616 }
1617 else
1618 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001619 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001620 {
1621 // Requesting a DELETE/DO NOT MODIFY action for an item
1622 // that isn't present on the eth(n) interface. Input JSON is
1623 // in error, so bail out.
1624 if (thisJson.is_null())
1625 {
1626 messages::resourceCannotBeDeleted(asyncResp->res);
1627 return;
1628 }
Ed Tanousf818b042022-06-27 13:17:35 -07001629 messages::propertyValueFormatError(asyncResp->res, thisJson,
1630 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001631 return;
1632 }
1633
1634 if (thisJson.is_null())
1635 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001636 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001637 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001638 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001639 {
Patrick Williams89492a12023-05-10 07:51:34 -05001640 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1641 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001642 }
1643 entryIdx++;
1644 }
1645 }
1646}
1647
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001648inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001649 const std::string& ifaceId,
1650 const std::vector<std::string>& updatedStaticNameServers,
1651 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1652{
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001653 setDbusProperty(
1654 asyncResp, "xyz.openbmc_project.Network",
1655 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1656 ifaceId,
George Liu9ae226f2023-06-21 17:56:46 +08001657 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001658 "StaticNameServers", updatedStaticNameServers);
Ed Tanousbf648f72021-06-03 15:00:14 -07001659}
1660
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001661inline void handleIPv6StaticAddressesPatch(
Ed Tanousddd70dc2023-03-01 16:00:27 -08001662 const std::string& ifaceId, const nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001663 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001664 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1665{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001666 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001667 {
Ed Tanous2e8c4bd2022-06-27 12:59:12 -07001668 messages::propertyValueTypeError(asyncResp->res, input,
1669 "IPv6StaticAddresses");
Ed Tanousbf648f72021-06-03 15:00:14 -07001670 return;
1671 }
1672 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001673 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001674 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1675 for (const nlohmann::json& thisJson : input)
1676 {
Patrick Williams89492a12023-05-10 07:51:34 -05001677 std::string pathString = "IPv6StaticAddresses/" +
1678 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001679
1680 if (!thisJson.is_null() && !thisJson.empty())
1681 {
1682 std::optional<std::string> address;
1683 std::optional<uint8_t> prefixLength;
1684 nlohmann::json thisJsonCopy = thisJson;
1685 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1686 address, "PrefixLength", prefixLength))
1687 {
Ed Tanousf818b042022-06-27 13:17:35 -07001688 messages::propertyValueFormatError(asyncResp->res, thisJson,
1689 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001690 return;
1691 }
1692
Ed Tanousbf648f72021-06-03 15:00:14 -07001693 // Find the address and prefixLength values. Any values that are
1694 // not explicitly provided are assumed to be unmodified from the
1695 // current state of the interface. Merge existing state into the
1696 // current request.
Ed Tanousd547d8d2024-03-16 18:04:41 -07001697 if (!address)
Ed Tanousbf648f72021-06-03 15:00:14 -07001698 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001699 if (nicIpEntry == ipv6Data.end())
1700 {
1701 messages::propertyMissing(asyncResp->res,
1702 pathString + "/Address");
1703 return;
1704 }
1705 address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001706 }
1707
Ed Tanousd547d8d2024-03-16 18:04:41 -07001708 if (!prefixLength)
Ed Tanousbf648f72021-06-03 15:00:14 -07001709 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001710 if (nicIpEntry == ipv6Data.end())
1711 {
1712 messages::propertyMissing(asyncResp->res,
1713 pathString + "/PrefixLength");
1714 return;
1715 }
1716 prefixLength = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001717 }
1718
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001719 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001720 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001721 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
Ed Tanousd547d8d2024-03-16 18:04:41 -07001722 nicIpEntry->id, *prefixLength,
1723 *address, "", asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001724 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1725 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001726 }
1727 else
1728 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001729 createIPv6(ifaceId, *prefixLength, *address, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001730 }
1731 entryIdx++;
1732 }
1733 else
1734 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001735 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001736 {
1737 // Requesting a DELETE/DO NOT MODIFY action for an item
1738 // that isn't present on the eth(n) interface. Input JSON is
1739 // in error, so bail out.
1740 if (thisJson.is_null())
1741 {
1742 messages::resourceCannotBeDeleted(asyncResp->res);
1743 return;
1744 }
Ed Tanousf818b042022-06-27 13:17:35 -07001745 messages::propertyValueFormatError(asyncResp->res, thisJson,
1746 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001747 return;
1748 }
1749
1750 if (thisJson.is_null())
1751 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001752 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001753 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001754 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001755 {
Patrick Williams89492a12023-05-10 07:51:34 -05001756 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1757 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001758 }
1759 entryIdx++;
1760 }
1761 }
1762}
1763
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001764inline std::string extractParentInterfaceName(const std::string& ifaceId)
1765{
1766 std::size_t pos = ifaceId.find('_');
1767 return ifaceId.substr(0, pos);
1768}
1769
Ed Tanous77179532023-02-28 10:45:28 -08001770inline void
1771 parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1772 const std::string& ifaceId,
1773 const EthernetInterfaceData& ethData,
1774 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001775 const std::vector<IPv6AddressData>& ipv6Data,
1776 const std::vector<StaticGatewayData>& ipv6GatewayData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001777{
Ed Tanousbf648f72021-06-03 15:00:14 -07001778 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1779 jsonResponse["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001780 jsonResponse["@odata.id"] = boost::urls::format(
1781 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001782 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1783
Willy Tu13451e32023-05-24 16:08:18 -07001784 if constexpr (bmcwebEnableHealthPopulate)
1785 {
1786 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1787 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1788 auto health = std::make_shared<HealthPopulate>(asyncResp);
1789 dbus::utility::getSubTreePaths(
1790 "/", 0, inventoryForEthernet,
1791 [health](const boost::system::error_code& ec,
1792 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1793 if (ec)
1794 {
1795 return;
1796 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001797
Willy Tu13451e32023-05-24 16:08:18 -07001798 health->inventory = resp;
Patrick Williams5a39f772023-10-20 11:20:21 -05001799 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001800
Willy Tu13451e32023-05-24 16:08:18 -07001801 health->populate();
1802 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001803
1804 if (ethData.nicEnabled)
1805 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001806 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001807 jsonResponse["Status"]["State"] = "Enabled";
1808 }
1809 else
1810 {
1811 jsonResponse["LinkStatus"] = "NoLink";
1812 jsonResponse["Status"]["State"] = "Disabled";
1813 }
1814
Ed Tanousbf648f72021-06-03 15:00:14 -07001815 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301816 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001817 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001818 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001819 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
Jishnu CMe4588152023-05-11 00:04:40 -05001820 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1821 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
1822 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNamev4Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001823 jsonResponse["DHCPv6"]["OperatingMode"] =
Ravi Tejab10d8db2022-05-24 09:04:12 -05001824 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Enabled"
Ed Tanousbf648f72021-06-03 15:00:14 -07001825 : "Disabled";
Jishnu CMe4588152023-05-11 00:04:40 -05001826 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1827 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
1828 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNamev6Enabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -05001829 jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1830 ethData.ipv6AcceptRa;
Ed Tanousbf648f72021-06-03 15:00:14 -07001831
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001832 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001833 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001834 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001835
1836 // When domain name is empty then it means, that it is a network
1837 // without domain names, and the host name itself must be treated as
1838 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001839 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001840 if (!ethData.domainnames.empty())
1841 {
1842 fqdn += "." + ethData.domainnames[0];
1843 }
1844 jsonResponse["FQDN"] = fqdn;
1845 }
1846
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001847 if (ethData.vlanId)
1848 {
1849 jsonResponse["EthernetInterfaceType"] = "Virtual";
1850 jsonResponse["VLAN"]["VLANEnable"] = true;
1851 jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1852 jsonResponse["VLAN"]["Tagged"] = true;
1853
1854 nlohmann::json::array_t relatedInterfaces;
1855 nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1856 parentInterface["@odata.id"] =
1857 boost::urls::format("/redfish/v1/Managers/bmc/EthernetInterfaces",
1858 extractParentInterfaceName(ifaceId));
1859 jsonResponse["Links"]["RelatedInterfaces"] =
1860 std::move(relatedInterfaces);
1861 }
1862 else
1863 {
1864 jsonResponse["EthernetInterfaceType"] = "Physical";
1865 }
1866
Ed Tanousbf648f72021-06-03 15:00:14 -07001867 jsonResponse["NameServers"] = ethData.nameServers;
1868 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1869
1870 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1871 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1872 ipv4Array = nlohmann::json::array();
1873 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001874 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001875 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001876 std::string gatewayStr = ipv4Config.gateway;
1877 if (gatewayStr.empty())
1878 {
1879 gatewayStr = "0.0.0.0";
1880 }
Ed Tanous14766872022-03-15 10:44:42 -07001881 nlohmann::json::object_t ipv4;
1882 ipv4["AddressOrigin"] = ipv4Config.origin;
1883 ipv4["SubnetMask"] = ipv4Config.netmask;
1884 ipv4["Address"] = ipv4Config.address;
1885 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001886
Ed Tanousbf648f72021-06-03 15:00:14 -07001887 if (ipv4Config.origin == "Static")
1888 {
Ed Tanous14766872022-03-15 10:44:42 -07001889 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001890 }
Ed Tanous14766872022-03-15 10:44:42 -07001891
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001892 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001893 }
1894
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001895 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001896 if (ipv6GatewayStr.empty())
1897 {
1898 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1899 }
1900
1901 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1902
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001903 nlohmann::json::array_t ipv6StaticGatewayArray;
1904 for (const auto& ipv6GatewayConfig : ipv6GatewayData)
1905 {
1906 nlohmann::json::object_t ipv6Gateway;
1907 ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
1908 ipv6Gateway["PrefixLength"] = ipv6GatewayConfig.prefixLength;
1909 ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
1910 }
1911 jsonResponse["IPv6StaticDefaultGateways"] =
1912 std::move(ipv6StaticGatewayArray);
1913
Ed Tanousbf648f72021-06-03 15:00:14 -07001914 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1915 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1916 ipv6Array = nlohmann::json::array();
1917 ipv6StaticArray = nlohmann::json::array();
1918 nlohmann::json& ipv6AddrPolicyTable =
1919 jsonResponse["IPv6AddressPolicyTable"];
1920 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001921 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001922 {
Ed Tanous14766872022-03-15 10:44:42 -07001923 nlohmann::json::object_t ipv6;
1924 ipv6["Address"] = ipv6Config.address;
1925 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1926 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05001927
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001928 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001929 if (ipv6Config.origin == "Static")
1930 {
Ed Tanous14766872022-03-15 10:44:42 -07001931 nlohmann::json::object_t ipv6Static;
1932 ipv6Static["Address"] = ipv6Config.address;
1933 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001934 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001935 }
1936 }
1937}
1938
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08001939inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1940 const std::string& ifaceId,
1941 const boost::system::error_code& ec,
1942 const sdbusplus::message_t& m)
1943{
1944 if (!ec)
1945 {
1946 return;
1947 }
1948 const sd_bus_error* dbusError = m.get_error();
1949 if (dbusError == nullptr)
1950 {
1951 messages::internalError(asyncResp->res);
1952 return;
1953 }
Ed Tanous62598e32023-07-17 17:06:25 -07001954 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08001955
1956 if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
1957 dbusError->name)
1958 {
1959 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1960 ifaceId);
1961 return;
1962 }
1963 if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
1964 dbusError->name)
1965 {
1966 messages::resourceCannotBeDeleted(asyncResp->res);
1967 return;
1968 }
1969 messages::internalError(asyncResp->res);
1970}
1971
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08001972inline void afterVlanCreate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1973 const std::string& parentInterfaceUri,
1974 const std::string& vlanInterface,
1975 const boost::system::error_code& ec,
1976 const sdbusplus::message_t& m
1977
1978)
1979{
1980 if (ec)
1981 {
1982 const sd_bus_error* dbusError = m.get_error();
1983 if (dbusError == nullptr)
1984 {
1985 messages::internalError(asyncResp->res);
1986 return;
1987 }
Ed Tanous62598e32023-07-17 17:06:25 -07001988 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08001989
1990 if (std::string_view(
1991 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
1992 dbusError->name)
1993 {
1994 messages::propertyValueNotInList(
1995 asyncResp->res, parentInterfaceUri,
1996 "Links/RelatedInterfaces/0/@odata.id");
1997 return;
1998 }
1999 if (std::string_view(
2000 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
2001 dbusError->name)
2002 {
2003 messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
2004 "Id", vlanInterface);
2005 return;
2006 }
2007 messages::internalError(asyncResp->res);
2008 return;
2009 }
2010
2011 const boost::urls::url vlanInterfaceUri = boost::urls::format(
2012 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", vlanInterface);
2013 asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
2014}
2015
Ed Tanousbf648f72021-06-03 15:00:14 -07002016inline void requestEthernetInterfacesRoutes(App& app)
2017{
2018 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07002019 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002020 .methods(boost::beast::http::verb::get)(
2021 [&app](const crow::Request& req,
2022 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002023 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002024 {
2025 return;
2026 }
2027
2028 asyncResp->res.jsonValue["@odata.type"] =
2029 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2030 asyncResp->res.jsonValue["@odata.id"] =
2031 "/redfish/v1/Managers/bmc/EthernetInterfaces";
2032 asyncResp->res.jsonValue["Name"] =
2033 "Ethernet Network Interface Collection";
2034 asyncResp->res.jsonValue["Description"] =
2035 "Collection of EthernetInterfaces for this Manager";
2036
2037 // Get eth interface list, and call the below callback for JSON
2038 // preparation
2039 getEthernetIfaceList(
Ed Tanous77179532023-02-28 10:45:28 -08002040 [asyncResp](const bool& success,
2041 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002042 if (!success)
2043 {
2044 messages::internalError(asyncResp->res);
2045 return;
2046 }
2047
2048 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
2049 ifaceArray = nlohmann::json::array();
Ed Tanous002d39b2022-05-31 08:59:27 -07002050 for (const std::string& ifaceItem : ifaceList)
2051 {
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08002052 nlohmann::json::object_t iface;
2053 iface["@odata.id"] = boost::urls::format(
2054 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
2055 ifaceItem);
2056 ifaceArray.push_back(std::move(iface));
Ed Tanous002d39b2022-05-31 08:59:27 -07002057 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002058
Ed Tanous002d39b2022-05-31 08:59:27 -07002059 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2060 asyncResp->res.jsonValue["@odata.id"] =
2061 "/redfish/v1/Managers/bmc/EthernetInterfaces";
2062 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002063 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002064
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002065 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
2066 .privileges(redfish::privileges::postEthernetInterfaceCollection)
2067 .methods(boost::beast::http::verb::post)(
2068 [&app](const crow::Request& req,
2069 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2070 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2071 {
2072 return;
2073 }
2074
2075 bool vlanEnable = false;
2076 uint32_t vlanId = 0;
2077 nlohmann::json::array_t relatedInterfaces;
2078
2079 if (!json_util::readJsonPatch(req, asyncResp->res, "VLAN/VLANEnable",
2080 vlanEnable, "VLAN/VLANId", vlanId,
2081 "Links/RelatedInterfaces",
2082 relatedInterfaces))
2083 {
2084 return;
2085 }
2086
2087 if (relatedInterfaces.size() != 1)
2088 {
2089 messages::arraySizeTooLong(asyncResp->res,
2090 "Links/RelatedInterfaces",
2091 relatedInterfaces.size());
2092 return;
2093 }
2094
2095 std::string parentInterfaceUri;
2096 if (!json_util::readJson(relatedInterfaces[0], asyncResp->res,
2097 "@odata.id", parentInterfaceUri))
2098 {
2099 messages::propertyMissing(asyncResp->res,
2100 "Links/RelatedInterfaces/0/@odata.id");
2101 return;
2102 }
Ed Tanous62598e32023-07-17 17:06:25 -07002103 BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002104
Ed Tanous6fd29552023-10-04 09:40:14 -07002105 boost::system::result<boost::urls::url_view> parsedUri =
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002106 boost::urls::parse_relative_ref(parentInterfaceUri);
2107 if (!parsedUri)
2108 {
2109 messages::propertyValueFormatError(
2110 asyncResp->res, parentInterfaceUri,
2111 "Links/RelatedInterfaces/0/@odata.id");
2112 return;
2113 }
2114
2115 std::string parentInterface;
2116 if (!crow::utility::readUrlSegments(
2117 *parsedUri, "redfish", "v1", "Managers", "bmc",
2118 "EthernetInterfaces", std::ref(parentInterface)))
2119 {
2120 messages::propertyValueNotInList(
2121 asyncResp->res, parentInterfaceUri,
2122 "Links/RelatedInterfaces/0/@odata.id");
2123 return;
2124 }
2125
2126 if (!vlanEnable)
2127 {
2128 // In OpenBMC implementation, VLANEnable cannot be false on
2129 // create
2130 messages::propertyValueIncorrect(asyncResp->res, "VLAN/VLANEnable",
2131 "false");
2132 return;
2133 }
2134
2135 std::string vlanInterface = parentInterface + "_" +
2136 std::to_string(vlanId);
2137 crow::connections::systemBus->async_method_call(
2138 [asyncResp, parentInterfaceUri,
2139 vlanInterface](const boost::system::error_code& ec,
2140 const sdbusplus::message_t& m) {
2141 afterVlanCreate(asyncResp, parentInterfaceUri, vlanInterface, ec,
2142 m);
Patrick Williams5a39f772023-10-20 11:20:21 -05002143 },
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002144 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
2145 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", parentInterface,
2146 vlanId);
Patrick Williams5a39f772023-10-20 11:20:21 -05002147 });
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002148
Ed Tanousbf648f72021-06-03 15:00:14 -07002149 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002150 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002151 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002152 [&app](const crow::Request& req,
2153 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2154 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002155 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002156 {
2157 return;
2158 }
2159 getEthernetIfaceData(
2160 ifaceId,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002161 [asyncResp,
2162 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2163 const std::vector<IPv4AddressData>& ipv4Data,
2164 const std::vector<IPv6AddressData>& ipv6Data,
2165 const std::vector<StaticGatewayData>& ipv6GatewayData) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002166 if (!success)
2167 {
2168 // TODO(Pawel)consider distinguish between non
2169 // existing object, and other errors
2170 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2171 ifaceId);
2172 return;
2173 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002174
Ed Tanous002d39b2022-05-31 08:59:27 -07002175 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao93bbc952023-02-23 14:11:20 +08002176 "#EthernetInterface.v1_9_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07002177 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
2178 asyncResp->res.jsonValue["Description"] =
2179 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05302180
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002181 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data,
2182 ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07002183 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002184 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002185
Ed Tanousbf648f72021-06-03 15:00:14 -07002186 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002187 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002188 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002189 [&app](const crow::Request& req,
2190 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2191 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002192 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 {
2194 return;
2195 }
2196 std::optional<std::string> hostname;
2197 std::optional<std::string> fqdn;
2198 std::optional<std::string> macAddress;
2199 std::optional<std::string> ipv6DefaultGateway;
Ed Tanousddd70dc2023-03-01 16:00:27 -08002200 std::optional<nlohmann::json::array_t> ipv4StaticAddresses;
2201 std::optional<nlohmann::json::array_t> ipv6StaticAddresses;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002202 std::optional<nlohmann::json::array_t> ipv6StaticDefaultGateways;
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 std::optional<std::vector<std::string>> staticNameServers;
2204 std::optional<nlohmann::json> dhcpv4;
2205 std::optional<nlohmann::json> dhcpv6;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002206 std::optional<bool> ipv6AutoConfigEnabled;
Ed Tanous002d39b2022-05-31 08:59:27 -07002207 std::optional<bool> interfaceEnabled;
2208 std::optional<size_t> mtuSize;
2209 DHCPParameters v4dhcpParms;
2210 DHCPParameters v6dhcpParms;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002211 // clang-format off
Ed Tanous002d39b2022-05-31 08:59:27 -07002212 if (!json_util::readJsonPatch(
Ravi Tejab10d8db2022-05-24 09:04:12 -05002213 req, asyncResp->res,
2214 "DHCPv4", dhcpv4,
2215 "DHCPv6", dhcpv6,
2216 "FQDN", fqdn,
2217 "HostName", hostname,
2218 "IPv4StaticAddresses", ipv4StaticAddresses,
2219 "IPv6DefaultGateway", ipv6DefaultGateway,
2220 "IPv6StaticAddresses", ipv6StaticAddresses,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002221 "IPv6StaticDefaultGateways", ipv6StaticDefaultGateways,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002222 "InterfaceEnabled", interfaceEnabled,
2223 "MACAddress", macAddress,
2224 "MTUSize", mtuSize,
2225 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled", ipv6AutoConfigEnabled,
2226 "StaticNameServers", staticNameServers
2227 )
2228 )
Ed Tanous002d39b2022-05-31 08:59:27 -07002229 {
2230 return;
2231 }
Ed Tanous5a8b6412024-03-27 11:30:54 -07002232 // clang-format on
Ed Tanous002d39b2022-05-31 08:59:27 -07002233 if (dhcpv4)
2234 {
2235 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
2236 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
2237 v4dhcpParms.useDnsServers, "UseNTPServers",
2238 v4dhcpParms.useNtpServers, "UseDomainName",
2239 v4dhcpParms.useDomainName))
2240 {
2241 return;
2242 }
2243 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002244
Ed Tanous002d39b2022-05-31 08:59:27 -07002245 if (dhcpv6)
2246 {
2247 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
2248 v6dhcpParms.dhcpv6OperatingMode,
2249 "UseDNSServers", v6dhcpParms.useDnsServers,
2250 "UseNTPServers", v6dhcpParms.useNtpServers,
2251 "UseDomainName",
2252 v6dhcpParms.useDomainName))
2253 {
2254 return;
2255 }
2256 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002257
Ed Tanous002d39b2022-05-31 08:59:27 -07002258 // Get single eth interface data, and call the below callback
2259 // for JSON preparation
2260 getEthernetIfaceData(
2261 ifaceId,
2262 [asyncResp, ifaceId, hostname = std::move(hostname),
2263 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2264 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2265 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2266 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002267 ipv6StaticDefaultGateway = std::move(ipv6StaticDefaultGateways),
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07002269 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002270 ipv6AutoConfigEnabled, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07002271 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2272 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanous77179532023-02-28 10:45:28 -08002273 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002274 const std::vector<IPv6AddressData>& ipv6Data,
2275 const std::vector<StaticGatewayData>& ipv6GatewayData) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 if (!success)
2277 {
2278 // ... otherwise return error
2279 // TODO(Pawel)consider distinguish between non
2280 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002281 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002282 ifaceId);
2283 return;
2284 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002285
Ed Tanous002d39b2022-05-31 08:59:27 -07002286 if (dhcpv4 || dhcpv6)
2287 {
2288 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07002289 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002290 }
Tejas Patil35fb5312021-09-20 15:35:20 +05302291
Ed Tanous002d39b2022-05-31 08:59:27 -07002292 if (hostname)
2293 {
2294 handleHostnamePatch(*hostname, asyncResp);
2295 }
2296
Ravi Tejab10d8db2022-05-24 09:04:12 -05002297 if (ipv6AutoConfigEnabled)
2298 {
2299 handleSLAACAutoConfigPatch(ifaceId, *ipv6AutoConfigEnabled,
2300 asyncResp);
2301 }
2302
Ed Tanous002d39b2022-05-31 08:59:27 -07002303 if (fqdn)
2304 {
2305 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2306 }
2307
2308 if (macAddress)
2309 {
2310 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2311 }
2312
2313 if (ipv4StaticAddresses)
2314 {
2315 // TODO(ed) for some reason the capture of
2316 // ipv4Addresses above is returning a const value,
2317 // not a non-const value. This doesn't really work
2318 // for us, as we need to be able to efficiently move
2319 // out the intermedia nlohmann::json objects. This
2320 // makes a copy of the structure, and operates on
2321 // that, but could be done more efficiently
Ed Tanousddd70dc2023-03-01 16:00:27 -08002322 nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses;
Ed Tanous002d39b2022-05-31 08:59:27 -07002323 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
2324 }
2325
2326 if (staticNameServers)
2327 {
2328 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2329 asyncResp);
2330 }
2331
2332 if (ipv6DefaultGateway)
2333 {
2334 messages::propertyNotWritable(asyncResp->res,
2335 "IPv6DefaultGateway");
2336 }
2337
2338 if (ipv6StaticAddresses)
2339 {
Ed Tanousddd70dc2023-03-01 16:00:27 -08002340 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
2341 ipv6Data, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002342 }
2343
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002344 if (ipv6StaticDefaultGateway)
2345 {
2346 handleIPv6DefaultGateway(ifaceId, *ipv6StaticDefaultGateway,
2347 ipv6GatewayData, asyncResp);
2348 }
2349
Ed Tanous002d39b2022-05-31 08:59:27 -07002350 if (interfaceEnabled)
2351 {
Ed Tanousd02aad32024-02-13 14:43:34 -08002352 setDbusProperty(asyncResp, "xyz.openbmc_project.Network",
2353 sdbusplus::message::object_path(
2354 "/xyz/openbmc_project/network") /
2355 ifaceId,
2356 "xyz.openbmc_project.Network.EthernetInterface",
2357 "NICEnabled", "InterfaceEnabled",
2358 *interfaceEnabled);
Ed Tanous002d39b2022-05-31 08:59:27 -07002359 }
2360
2361 if (mtuSize)
2362 {
2363 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2364 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002365 });
Ed Tanous5a8b6412024-03-27 11:30:54 -07002366 });
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002367
2368 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
2369 .privileges(redfish::privileges::deleteEthernetInterface)
2370 .methods(boost::beast::http::verb::delete_)(
2371 [&app](const crow::Request& req,
2372 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2373 const std::string& ifaceId) {
2374 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2375 {
2376 return;
2377 }
2378
2379 crow::connections::systemBus->async_method_call(
2380 [asyncResp, ifaceId](const boost::system::error_code& ec,
2381 const sdbusplus::message_t& m) {
2382 afterDelete(asyncResp, ifaceId, ec, m);
Ed Tanous5a8b6412024-03-27 11:30:54 -07002383 },
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002384 "xyz.openbmc_project.Network",
2385 std::string("/xyz/openbmc_project/network/") + ifaceId,
2386 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous5a8b6412024-03-27 11:30:54 -07002387 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002388}
2389
Ed Tanous1abe55e2018-09-05 08:30:59 -07002390} // namespace redfish