blob: 8bd4cdc62b733e15370dcc3364b9dc65742e8359 [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{
George Liu9ae226f2023-06-21 17:56:46 +0800732 sdbusplus::asio::setProperty(
733 *crow::connections::systemBus, "xyz.openbmc_project.Network",
Ravi Teja9010ec22019-08-01 23:30:25 -0500734 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9010ec22019-08-01 23:30:25 -0500735 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
George Liu9ae226f2023-06-21 17:56:46 +0800736 gateway, [asyncResp](const boost::system::error_code& ec) {
Patrick Williams5a39f772023-10-20 11:20:21 -0500737 if (ec)
738 {
739 messages::internalError(asyncResp->res);
740 return;
741 }
742 asyncResp->res.result(boost::beast::http::status::no_content);
743 });
Ravi Teja9010ec22019-08-01 23:30:25 -0500744}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700745/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700746 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700747 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700748 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
749 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
750 * @param[in] gateway IPv4 address of this interfaces gateway
751 * @param[in] address IPv4 address to assign to this interface
752 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700753 *
754 * @return None
755 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000756inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
757 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800758 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700759{
Patrick Williams5a39f772023-10-20 11:20:21 -0500760 auto createIpHandler = [asyncResp, ifaceId,
761 gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500762 if (ec)
763 {
764 messages::internalError(asyncResp->res);
765 return;
766 }
767 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
768 };
769
Ed Tanous4a0cb852018-10-15 07:55:04 -0700770 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500771 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700772 "/xyz/openbmc_project/network/" + ifaceId,
773 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700774 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700775 gateway);
776}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500777
778/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700779 * @brief Deletes the IPv6 entry for this interface and creates a replacement
780 * static IPv6 entry
781 *
782 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
783 * @param[in] id The unique hash entry identifying the DBus entry
784 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
785 * @param[in] address IPv6 address to assign to this interface
786 * @param[io] asyncResp Response object that will be returned to client
787 *
788 * @return None
789 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600790
791enum class IpVersion
792{
793 IpV4,
794 IpV6
795};
796
797inline void deleteAndCreateIPAddress(
798 IpVersion version, const std::string& ifaceId, const std::string& id,
799 uint8_t prefixLength, const std::string& address,
800 const std::string& gateway,
801 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700802{
803 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600804 [asyncResp, version, ifaceId, address, prefixLength,
805 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700806 if (ec)
807 {
808 messages::internalError(asyncResp->res);
809 }
Ravi Teja9c5e5852023-02-26 21:33:52 -0600810 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
811 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous002d39b2022-05-31 08:59:27 -0700812 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800813 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700814 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700815 {
816 messages::internalError(asyncResp->res);
817 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500818 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700819 "xyz.openbmc_project.Network",
820 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600821 "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
822 prefixLength, gateway);
Patrick Williams5a39f772023-10-20 11:20:21 -0500823 },
Johnathan Mantey01784822019-06-18 12:44:21 -0700824 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600825 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700826 "xyz.openbmc_project.Object.Delete", "Delete");
827}
828
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500829inline bool extractIPv6DefaultGatewayData(
830 const std::string& ethifaceId,
831 const dbus::utility::ManagedObjectType& dbusData,
832 std::vector<StaticGatewayData>& staticGatewayConfig)
833{
834 std::string staticGatewayPathStart("/xyz/openbmc_project/network/");
835 staticGatewayPathStart += ethifaceId;
836
837 for (const auto& objpath : dbusData)
838 {
839 if (!std::string_view(objpath.first.str)
840 .starts_with(staticGatewayPathStart))
841 {
842 continue;
843 }
844 for (const auto& interface : objpath.second)
845 {
846 if (interface.first != "xyz.openbmc_project.Network.StaticGateway")
847 {
848 continue;
849 }
850 StaticGatewayData& staticGateway =
851 staticGatewayConfig.emplace_back();
852 staticGateway.id = objpath.first.filename();
853
854 bool success = sdbusplus::unpackPropertiesNoThrow(
855 redfish::dbus_utils::UnpackErrorPrinter(), interface.second,
856 "Gateway", staticGateway.gateway, "PrefixLength",
857 staticGateway.prefixLength, "ProtocolType",
858 staticGateway.protocol);
859 if (!success)
860 {
861 return false;
862 }
863 }
864 }
865 return true;
866}
867
Johnathan Mantey01784822019-06-18 12:44:21 -0700868/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500869 * @brief Creates IPv6 with given data
870 *
871 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500872 * @param[in] prefixLength Prefix length that needs to be added
873 * @param[in] address IP address that needs to be added
874 * @param[io] asyncResp Response object that will be returned to client
875 *
876 * @return None
877 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500878inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
879 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800880 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500881{
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500882 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
883 path /= ifaceId;
884
Patrick Williams5a39f772023-10-20 11:20:21 -0500885 auto createIpHandler = [asyncResp,
886 address](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500887 if (ec)
888 {
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500889 if (ec == boost::system::errc::io_error)
890 {
891 messages::propertyValueFormatError(asyncResp->res, address,
892 "Address");
893 }
894 else
895 {
896 messages::internalError(asyncResp->res);
897 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500898 }
899 };
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500900 // Passing null for gateway, as per redfish spec IPv6StaticAddresses
901 // object does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500902 crow::connections::systemBus->async_method_call(
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500903 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500904 "xyz.openbmc_project.Network.IP.Create", "IP",
905 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
906 "");
907}
908
Ed Tanous4a0cb852018-10-15 07:55:04 -0700909/**
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500910 * @brief Deletes given IPv6 Static Gateway
911 *
912 * @param[in] ifaceId Id of interface whose IP should be deleted
913 * @param[in] ipHash DBus Hash id of IP that should be deleted
914 * @param[io] asyncResp Response object that will be returned to client
915 *
916 * @return None
917 */
918inline void
919 deleteIPv6Gateway(std::string_view gatewayId,
920 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
921{
922 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
923 path /= gatewayId;
924 crow::connections::systemBus->async_method_call(
925 [asyncResp](const boost::system::error_code& ec) {
926 if (ec)
927 {
928 messages::internalError(asyncResp->res);
929 }
930 },
931 "xyz.openbmc_project.Network", path,
932 "xyz.openbmc_project.Object.Delete", "Delete");
933}
934
935/**
936 * @brief Creates IPv6 static default gateway with given data
937 *
938 * @param[in] ifaceId Id of interface whose IP should be added
939 * @param[in] prefixLength Prefix length that needs to be added
940 * @param[in] gateway Gateway address that needs to be added
941 * @param[io] asyncResp Response object that will be returned to client
942 *
943 * @return None
944 */
945inline void createIPv6DefaultGateway(
946 std::string_view ifaceId, size_t prefixLength, std::string_view gateway,
947 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
948{
949 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
950 path /= ifaceId;
951 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
952 if (ec)
953 {
954 messages::internalError(asyncResp->res);
955 }
956 };
957 crow::connections::systemBus->async_method_call(
958 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
959 "xyz.openbmc_project.Network.StaticGateway.Create", "StaticGateway",
960 gateway, prefixLength, "xyz.openbmc_project.Network.IP.Protocol.IPv6");
961}
962
963/**
964 * @brief Deletes the IPv6 default gateway entry for this interface and
965 * creates a replacement IPv6 default gateway entry
966 *
967 * @param[in] ifaceId Id of interface upon which to create the IPv6
968 * entry
969 * @param[in] gateway IPv6 gateway to assign to this interface
970 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
971 * @param[io] asyncResp Response object that will be returned to client
972 *
973 * @return None
974 */
975inline void deleteAndCreateIPv6DefaultGateway(
976 std::string_view ifaceId, std::string_view gatewayId,
977 std::string_view gateway, size_t prefixLength,
978 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
979{
980 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
981 path /= gatewayId;
982 crow::connections::systemBus->async_method_call(
983 [asyncResp, ifaceId, gateway,
984 prefixLength](const boost::system::error_code& ec) {
985 if (ec)
986 {
987 messages::internalError(asyncResp->res);
988 return;
989 }
990 createIPv6DefaultGateway(ifaceId, prefixLength, gateway, asyncResp);
991 },
992 "xyz.openbmc_project.Network", path,
993 "xyz.openbmc_project.Object.Delete", "Delete");
994}
995
996/**
997 * @brief Sets IPv6 default gateway with given data
998 *
999 * @param[in] ifaceId Id of interface whose gateway should be added
1000 * @param[in] input Contains address that needs to be added
1001 * @param[in] staticGatewayData Current static gateways in the system
1002 * @param[io] asyncResp Response object that will be returned to client
1003 *
1004 * @return None
1005 */
1006
1007inline void handleIPv6DefaultGateway(
1008 const std::string& ifaceId, const nlohmann::json::array_t& input,
1009 const std::vector<StaticGatewayData>& staticGatewayData,
1010 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1011{
1012 size_t entryIdx = 1;
1013 std::vector<StaticGatewayData>::const_iterator staticGatewayEntry =
1014 staticGatewayData.begin();
1015
1016 for (const nlohmann::json& thisJson : input)
1017 {
1018 // find the next gateway entry
1019 while (staticGatewayEntry != staticGatewayData.end())
1020 {
1021 if (staticGatewayEntry->protocol ==
1022 "xyz.openbmc_project.Network.IP.Protocol.IPv6")
1023 {
1024 break;
1025 }
1026 staticGatewayEntry++;
1027 }
1028 std::string pathString = "IPv6StaticDefaultGateways/" +
1029 std::to_string(entryIdx);
1030 if (thisJson.is_null())
1031 {
1032 if (staticGatewayEntry == staticGatewayData.end())
1033 {
1034 messages::resourceCannotBeDeleted(asyncResp->res);
1035 return;
1036 }
1037 deleteIPv6Gateway(staticGatewayEntry->id, asyncResp);
1038 return;
1039 }
1040 if (thisJson.is_object() && thisJson.empty())
1041 {
1042 // Do nothing, but make sure the entry exists.
1043 if (staticGatewayEntry == staticGatewayData.end())
1044 {
1045 messages::propertyValueFormatError(asyncResp->res, thisJson,
1046 pathString);
1047 return;
1048 }
1049 }
1050 std::optional<std::string> address;
1051 std::optional<size_t> prefixLength;
1052
1053 nlohmann::json thisJsonCopy = thisJson;
1054 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1055 address, "PrefixLength", prefixLength))
1056 {
1057 return;
1058 }
1059 const std::string* addr = nullptr;
1060 size_t prefix = 0;
1061 if (address)
1062 {
1063 addr = &(*address);
1064 }
1065 else if (staticGatewayEntry != staticGatewayData.end())
1066 {
1067 addr = &(staticGatewayEntry->gateway);
1068 }
1069 else
1070 {
1071 messages::propertyMissing(asyncResp->res, pathString + "/Address");
1072 return;
1073 }
1074 if (prefixLength)
1075 {
1076 prefix = *prefixLength;
1077 }
1078 else if (staticGatewayEntry != staticGatewayData.end())
1079 {
1080 prefix = staticGatewayEntry->prefixLength;
1081 }
1082 else
1083 {
1084 messages::propertyMissing(asyncResp->res,
1085 pathString + "/PrefixLength");
1086 return;
1087 }
1088 if (staticGatewayEntry != staticGatewayData.end())
1089 {
1090 deleteAndCreateIPv6DefaultGateway(ifaceId, staticGatewayEntry->id,
1091 *addr, prefix, asyncResp);
1092 staticGatewayEntry++;
1093 }
1094 else
1095 {
1096 createIPv6DefaultGateway(ifaceId, prefix, *addr, asyncResp);
1097 }
1098 entryIdx++;
1099 }
1100}
1101
1102/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001103 * Function that retrieves all properties for given Ethernet Interface
1104 * Object
1105 * from EntityManager Network Manager
1106 * @param ethiface_id a eth interface id to query on DBus
1107 * @param callback a function that shall be called to convert Dbus output
1108 * into JSON
1109 */
1110template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +00001111void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001112 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001113{
George Liuf5892d02023-03-01 10:37:08 +08001114 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1115 dbus::utility::getManagedObjects(
1116 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001117 [ethifaceId{std::string{ethifaceId}},
1118 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous8b242752023-06-27 17:17:13 -07001119 const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001120 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 EthernetInterfaceData ethData{};
Ed Tanous77179532023-02-28 10:45:28 -08001122 std::vector<IPv4AddressData> ipv4Data;
1123 std::vector<IPv6AddressData> ipv6Data;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001124 std::vector<StaticGatewayData> ipv6GatewayData;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001125
Ed Tanous8b242752023-06-27 17:17:13 -07001126 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001127 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001128 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001129 return;
1130 }
1131
1132 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
1133 if (!found)
1134 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001135 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001136 return;
1137 }
1138
1139 extractIPData(ethifaceId, resp, ipv4Data);
1140 // Fix global GW
1141 for (IPv4AddressData& ipv4 : ipv4Data)
1142 {
1143 if (((ipv4.linktype == LinkType::Global) &&
1144 (ipv4.gateway == "0.0.0.0")) ||
1145 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001146 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001148 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001149 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001150
Ed Tanous002d39b2022-05-31 08:59:27 -07001151 extractIPV6Data(ethifaceId, resp, ipv6Data);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001152 if (!extractIPv6DefaultGatewayData(ethifaceId, resp, ipv6GatewayData))
1153 {
1154 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1155 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001156 // Finally make a callback with useful data
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001157 callback(true, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Patrick Williams5a39f772023-10-20 11:20:21 -05001158 });
Ed Tanous271584a2019-07-09 16:24:22 -07001159}
Ed Tanous4a0cb852018-10-15 07:55:04 -07001160
1161/**
1162 * Function that retrieves all Ethernet Interfaces available through Network
1163 * Manager
1164 * @param callback a function that shall be called to convert Dbus output
1165 * into JSON.
1166 */
1167template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001168void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001169{
George Liuf5892d02023-03-01 10:37:08 +08001170 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1171 dbus::utility::getManagedObjects(
1172 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001173 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous8b242752023-06-27 17:17:13 -07001174 const boost::system::error_code& ec,
George Liuf5892d02023-03-01 10:37:08 +08001175 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 // Callback requires vector<string> to retrieve all available
1177 // ethernet interfaces
Ed Tanous77179532023-02-28 10:45:28 -08001178 std::vector<std::string> ifaceList;
Ed Tanous002d39b2022-05-31 08:59:27 -07001179 ifaceList.reserve(resp.size());
Ed Tanous8b242752023-06-27 17:17:13 -07001180 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001181 {
1182 callback(false, ifaceList);
1183 return;
1184 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001185
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 // Iterate over all retrieved ObjectPaths.
1187 for (const auto& objpath : resp)
1188 {
1189 // And all interfaces available for certain ObjectPath.
1190 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001191 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 // If interface is
1193 // xyz.openbmc_project.Network.EthernetInterface, this is
1194 // what we're looking for.
1195 if (interface.first ==
1196 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -07001197 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001198 std::string ifaceId = objpath.first.filename();
1199 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001200 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001201 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001202 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001203 // and put it into output vector.
Ed Tanous77179532023-02-28 10:45:28 -08001204 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001205 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001206 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001207 }
Ed Tanous2c5875a2023-05-15 09:56:06 -07001208
Ed Tanous3544d2a2023-08-06 18:12:20 -07001209 std::ranges::sort(ifaceList, AlphanumLess<std::string>());
Ed Tanous2c5875a2023-05-15 09:56:06 -07001210
Ed Tanous002d39b2022-05-31 08:59:27 -07001211 // Finally make a callback with useful data
1212 callback(true, ifaceList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001213 });
Ed Tanous271584a2019-07-09 16:24:22 -07001214}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001215
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001216inline void
1217 handleHostnamePatch(const std::string& hostname,
1218 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001219{
Ed Tanousbf648f72021-06-03 15:00:14 -07001220 // SHOULD handle host names of up to 255 characters(RFC 1123)
1221 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001222 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001223 messages::propertyValueFormatError(asyncResp->res, hostname,
1224 "HostName");
1225 return;
1226 }
George Liu9ae226f2023-06-21 17:56:46 +08001227 sdbusplus::asio::setProperty(
1228 *crow::connections::systemBus, "xyz.openbmc_project.Network",
1229 "/xyz/openbmc_project/network/config",
1230 "xyz.openbmc_project.Network.SystemConfiguration", "HostName", hostname,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001231 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001232 if (ec)
1233 {
1234 messages::internalError(asyncResp->res);
1235 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001236 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001237}
1238
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001239inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301240 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1241 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1242{
Patrick Williams89492a12023-05-10 07:51:34 -05001243 sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
1244 ifaceId;
George Liu9ae226f2023-06-21 17:56:46 +08001245 sdbusplus::asio::setProperty(
1246 *crow::connections::systemBus, "xyz.openbmc_project.Network", objPath,
1247 "xyz.openbmc_project.Network.EthernetInterface", "MTU", mtuSize,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001248 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001249 if (ec)
1250 {
1251 messages::internalError(asyncResp->res);
1252 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001253 });
Tejas Patil35fb5312021-09-20 15:35:20 +05301254}
1255
1256inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001257 handleDomainnamePatch(const std::string& ifaceId,
1258 const std::string& domainname,
1259 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001260{
1261 std::vector<std::string> vectorDomainname = {domainname};
George Liu9ae226f2023-06-21 17:56:46 +08001262 sdbusplus::asio::setProperty(
1263 *crow::connections::systemBus, "xyz.openbmc_project.Network",
Ed Tanousbf648f72021-06-03 15:00:14 -07001264 "/xyz/openbmc_project/network/" + ifaceId,
Ed Tanousbf648f72021-06-03 15:00:14 -07001265 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
George Liu9ae226f2023-06-21 17:56:46 +08001266 vectorDomainname, [asyncResp](const boost::system::error_code& ec) {
Patrick Williams5a39f772023-10-20 11:20:21 -05001267 if (ec)
1268 {
1269 messages::internalError(asyncResp->res);
1270 }
1271 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001272}
1273
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001274inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001275{
1276 // A valid host name can never have the dotted-decimal form (RFC 1123)
Ed Tanous3544d2a2023-08-06 18:12:20 -07001277 if (std::ranges::all_of(hostname, ::isdigit))
Ed Tanousbf648f72021-06-03 15:00:14 -07001278 {
1279 return false;
1280 }
1281 // Each label(hostname/subdomains) within a valid FQDN
1282 // MUST handle host names of up to 63 characters (RFC 1123)
1283 // labels cannot start or end with hyphens (RFC 952)
1284 // labels can start with numbers (RFC 1123)
Ed Tanous4b242742023-05-11 09:51:51 -07001285 const static std::regex pattern(
Ed Tanousbf648f72021-06-03 15:00:14 -07001286 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1287
1288 return std::regex_match(hostname, pattern);
1289}
1290
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001291inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001292{
1293 // Can have multiple subdomains
1294 // Top Level Domain's min length is 2 character
Ed Tanous4b242742023-05-11 09:51:51 -07001295 const static std::regex pattern(
George Liu0fda0f12021-11-16 10:06:17 +08001296 "^([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 -07001297
1298 return std::regex_match(domainname, pattern);
1299}
1300
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001301inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1302 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001303{
1304 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1305 if (fqdn.length() > 255)
1306 {
1307 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1308 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001309 }
1310
Ed Tanousbf648f72021-06-03 15:00:14 -07001311 size_t pos = fqdn.find('.');
1312 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001313 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001314 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1315 return;
1316 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001317
Ed Tanousbf648f72021-06-03 15:00:14 -07001318 std::string hostname;
1319 std::string domainname;
1320 domainname = (fqdn).substr(pos + 1);
1321 hostname = (fqdn).substr(0, pos);
1322
1323 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1324 {
1325 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1326 return;
1327 }
1328
1329 handleHostnamePatch(hostname, asyncResp);
1330 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1331}
1332
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001333inline void
1334 handleMACAddressPatch(const std::string& ifaceId,
1335 const std::string& macAddress,
1336 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001337{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001338 static constexpr std::string_view dbusNotAllowedError =
1339 "xyz.openbmc_project.Common.Error.NotAllowed";
1340
George Liu9ae226f2023-06-21 17:56:46 +08001341 sdbusplus::asio::setProperty(
1342 *crow::connections::systemBus, "xyz.openbmc_project.Network",
1343 "/xyz/openbmc_project/network/" + ifaceId,
1344 "xyz.openbmc_project.Network.MACAddress", "MACAddress", macAddress,
1345 [asyncResp](const boost::system::error_code& ec,
1346 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001347 if (ec)
1348 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001349 const sd_bus_error* err = msg.get_error();
1350 if (err == nullptr)
1351 {
1352 messages::internalError(asyncResp->res);
1353 return;
1354 }
1355 if (err->name == dbusNotAllowedError)
1356 {
1357 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1358 return;
1359 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001360 messages::internalError(asyncResp->res);
1361 return;
1362 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001363 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001364}
1365
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001366inline void setDHCPEnabled(const std::string& ifaceId,
1367 const std::string& propertyName, const bool v4Value,
1368 const bool v6Value,
1369 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001370{
1371 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
George Liu9ae226f2023-06-21 17:56:46 +08001372 sdbusplus::asio::setProperty(
1373 *crow::connections::systemBus, "xyz.openbmc_project.Network",
1374 "/xyz/openbmc_project/network/" + ifaceId,
1375 "xyz.openbmc_project.Network.EthernetInterface", propertyName, dhcp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001376 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001377 if (ec)
1378 {
Ed Tanous62598e32023-07-17 17:06:25 -07001379 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001380 messages::internalError(asyncResp->res);
1381 return;
1382 }
1383 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001384 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001385}
1386
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001387inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001388 const std::string& ifaceId, const std::string& propertyName,
1389 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1390{
George Liu9ae226f2023-06-21 17:56:46 +08001391 sdbusplus::asio::setProperty(
1392 *crow::connections::systemBus, "xyz.openbmc_project.Network",
1393 "/xyz/openbmc_project/network/" + ifaceId,
1394 "xyz.openbmc_project.Network.EthernetInterface", propertyName, value,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001395 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001396 if (ec)
1397 {
Ed Tanous62598e32023-07-17 17:06:25 -07001398 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001399 messages::internalError(asyncResp->res);
1400 return;
1401 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001402 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001403}
1404
Jishnu CMe4588152023-05-11 00:04:40 -05001405enum class NetworkType
1406{
1407 dhcp4,
1408 dhcp6
1409};
1410
1411inline void setDHCPConfig(const std::string& propertyName, const bool& value,
1412 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1413 const std::string& ethifaceId, NetworkType type)
Ed Tanousbf648f72021-06-03 15:00:14 -07001414{
Ed Tanous62598e32023-07-17 17:06:25 -07001415 BMCWEB_LOG_DEBUG("{} = {}", propertyName, value);
Jishnu CMe4588152023-05-11 00:04:40 -05001416 sdbusplus::message::object_path path("/xyz/openbmc_project/network/");
1417 path /= ethifaceId;
1418
1419 if (type == NetworkType::dhcp4)
1420 {
1421 path /= "dhcp4";
1422 }
1423 else
1424 {
1425 path /= "dhcp6";
1426 }
1427
George Liu9ae226f2023-06-21 17:56:46 +08001428 sdbusplus::asio::setProperty(
Jishnu CMe4588152023-05-11 00:04:40 -05001429 *crow::connections::systemBus, "xyz.openbmc_project.Network", path,
George Liu9ae226f2023-06-21 17:56:46 +08001430 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, value,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001431 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001432 if (ec)
1433 {
Ed Tanous62598e32023-07-17 17:06:25 -07001434 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001435 messages::internalError(asyncResp->res);
1436 return;
1437 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001438 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001439}
1440
Ravi Tejab10d8db2022-05-24 09:04:12 -05001441inline void handleSLAACAutoConfigPatch(
1442 const std::string& ifaceId, bool ipv6AutoConfigEnabled,
1443 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1444{
1445 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1446 path /= ifaceId;
1447 sdbusplus::asio::setProperty(
1448 *crow::connections::systemBus, "xyz.openbmc_project.Network", path,
1449 "xyz.openbmc_project.Network.EthernetInterface", "IPv6AcceptRA",
1450 ipv6AutoConfigEnabled,
1451 [asyncResp](const boost::system::error_code& ec) {
1452 if (ec)
1453 {
1454 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
1455 messages::internalError(asyncResp->res);
1456 return;
1457 }
1458 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001459 });
Ravi Tejab10d8db2022-05-24 09:04:12 -05001460}
1461
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001462inline void handleDHCPPatch(const std::string& ifaceId,
1463 const EthernetInterfaceData& ethData,
1464 const DHCPParameters& v4dhcpParms,
1465 const DHCPParameters& v6dhcpParms,
1466 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001467{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001468 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1469 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001470
1471 bool nextv4DHCPState =
1472 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1473
1474 bool nextv6DHCPState{};
1475 if (v6dhcpParms.dhcpv6OperatingMode)
1476 {
Ravi Tejab10d8db2022-05-24 09:04:12 -05001477 if ((*v6dhcpParms.dhcpv6OperatingMode != "Enabled") &&
Ed Tanousbf648f72021-06-03 15:00:14 -07001478 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1479 {
1480 messages::propertyValueFormatError(asyncResp->res,
1481 *v6dhcpParms.dhcpv6OperatingMode,
1482 "OperatingMode");
1483 return;
1484 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05001485 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Enabled");
Ed Tanousbf648f72021-06-03 15:00:14 -07001486 }
1487 else
1488 {
1489 nextv6DHCPState = ipv6Active;
1490 }
1491
Jishnu CMe4588152023-05-11 00:04:40 -05001492 bool nextDNSv4 = ethData.dnsv4Enabled;
1493 bool nextDNSv6 = ethData.dnsv6Enabled;
1494 if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001495 {
Jishnu CMe4588152023-05-11 00:04:40 -05001496 nextDNSv4 = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001497 }
Jishnu CMe4588152023-05-11 00:04:40 -05001498 if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001499 {
Jishnu CMe4588152023-05-11 00:04:40 -05001500 nextDNSv6 = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001501 }
1502
Jishnu CMe4588152023-05-11 00:04:40 -05001503 bool nextNTPv4 = ethData.ntpv4Enabled;
1504 bool nextNTPv6 = ethData.ntpv6Enabled;
1505 if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001506 {
Jishnu CMe4588152023-05-11 00:04:40 -05001507 nextNTPv4 = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001508 }
Jishnu CMe4588152023-05-11 00:04:40 -05001509 if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001510 {
Jishnu CMe4588152023-05-11 00:04:40 -05001511 nextNTPv6 = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001512 }
1513
Ravi Teja91c441e2024-02-23 09:03:43 -06001514 bool nextUsev4Domain = ethData.domainv4Enabled;
1515 bool nextUsev6Domain = ethData.domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -05001516 if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001517 {
Jishnu CMe4588152023-05-11 00:04:40 -05001518 nextUsev4Domain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001519 }
Jishnu CMe4588152023-05-11 00:04:40 -05001520 if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001521 {
Jishnu CMe4588152023-05-11 00:04:40 -05001522 nextUsev6Domain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001523 }
1524
Ed Tanous62598e32023-07-17 17:06:25 -07001525 BMCWEB_LOG_DEBUG("set DHCPEnabled...");
Ed Tanousbf648f72021-06-03 15:00:14 -07001526 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1527 asyncResp);
Ed Tanous62598e32023-07-17 17:06:25 -07001528 BMCWEB_LOG_DEBUG("set DNSEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001529 setDHCPConfig("DNSEnabled", nextDNSv4, asyncResp, ifaceId,
1530 NetworkType::dhcp4);
Ed Tanous62598e32023-07-17 17:06:25 -07001531 BMCWEB_LOG_DEBUG("set NTPEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001532 setDHCPConfig("NTPEnabled", nextNTPv4, asyncResp, ifaceId,
1533 NetworkType::dhcp4);
Ravi Teja91c441e2024-02-23 09:03:43 -06001534 BMCWEB_LOG_DEBUG("set DomainEnabled...");
1535 setDHCPConfig("DomainEnabled", nextUsev4Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001536 NetworkType::dhcp4);
1537 BMCWEB_LOG_DEBUG("set DNSEnabled for dhcp6...");
1538 setDHCPConfig("DNSEnabled", nextDNSv6, asyncResp, ifaceId,
1539 NetworkType::dhcp6);
1540 BMCWEB_LOG_DEBUG("set NTPEnabled for dhcp6...");
1541 setDHCPConfig("NTPEnabled", nextNTPv6, asyncResp, ifaceId,
1542 NetworkType::dhcp6);
Ravi Teja91c441e2024-02-23 09:03:43 -06001543 BMCWEB_LOG_DEBUG("set DomainEnabled for dhcp6...");
1544 setDHCPConfig("DomainEnabled", nextUsev6Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001545 NetworkType::dhcp6);
Ed Tanousbf648f72021-06-03 15:00:14 -07001546}
1547
Ed Tanous77179532023-02-28 10:45:28 -08001548inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1549 const std::vector<IPv4AddressData>::const_iterator& head,
1550 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001551{
1552 return std::find_if(head, end, [](const IPv4AddressData& value) {
1553 return value.origin == "Static";
1554 });
1555}
1556
Ed Tanous77179532023-02-28 10:45:28 -08001557inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1558 const std::vector<IPv6AddressData>::const_iterator& head,
1559 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001560{
1561 return std::find_if(head, end, [](const IPv6AddressData& value) {
1562 return value.origin == "Static";
1563 });
1564}
1565
Ed Tanous77179532023-02-28 10:45:28 -08001566inline void
Ed Tanousddd70dc2023-03-01 16:00:27 -08001567 handleIPv4StaticPatch(const std::string& ifaceId,
1568 nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001569 const std::vector<IPv4AddressData>& ipv4Data,
1570 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001571{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001572 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001573 {
Ed Tanous2e8c4bd2022-06-27 12:59:12 -07001574 messages::propertyValueTypeError(asyncResp->res, input,
1575 "IPv4StaticAddresses");
Ed Tanousbf648f72021-06-03 15:00:14 -07001576 return;
1577 }
1578
1579 unsigned entryIdx = 1;
1580 // Find the first static IP address currently active on the NIC and
1581 // match it to the first JSON element in the IPv4StaticAddresses array.
1582 // Match each subsequent JSON element to the next static IP programmed
1583 // into the NIC.
Ed Tanous77179532023-02-28 10:45:28 -08001584 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001585 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1586
1587 for (nlohmann::json& thisJson : input)
1588 {
Patrick Williams89492a12023-05-10 07:51:34 -05001589 std::string pathString = "IPv4StaticAddresses/" +
1590 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001591
1592 if (!thisJson.is_null() && !thisJson.empty())
1593 {
1594 std::optional<std::string> address;
1595 std::optional<std::string> subnetMask;
1596 std::optional<std::string> gateway;
1597
1598 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1599 address, "SubnetMask", subnetMask,
1600 "Gateway", gateway))
1601 {
Ed Tanousf818b042022-06-27 13:17:35 -07001602 messages::propertyValueFormatError(asyncResp->res, thisJson,
1603 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001604 return;
1605 }
1606
1607 // Find the address/subnet/gateway values. Any values that are
1608 // not explicitly provided are assumed to be unmodified from the
1609 // current state of the interface. Merge existing state into the
1610 // current request.
Ed Tanousbf648f72021-06-03 15:00:14 -07001611 if (address)
1612 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001613 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001614 {
1615 messages::propertyValueFormatError(asyncResp->res, *address,
1616 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001617 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001618 }
1619 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001620 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001621 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001622 address = (nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001623 }
1624 else
1625 {
1626 messages::propertyMissing(asyncResp->res,
1627 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001628 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001629 }
1630
Ed Tanouse01d0c32023-06-30 13:21:32 -07001631 uint8_t prefixLength = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001632 if (subnetMask)
1633 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001634 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1635 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001636 {
1637 messages::propertyValueFormatError(
1638 asyncResp->res, *subnetMask,
1639 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001640 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001641 }
1642 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001643 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001644 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001645 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1646 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001647 {
1648 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001649 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001650 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001651 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001652 }
1653 }
1654 else
1655 {
1656 messages::propertyMissing(asyncResp->res,
1657 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001658 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001659 }
1660
1661 if (gateway)
1662 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001663 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001664 {
1665 messages::propertyValueFormatError(asyncResp->res, *gateway,
1666 pathString + "/Gateway");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001667 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001668 }
1669 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001670 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001671 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001672 gateway = nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001673 }
1674 else
1675 {
1676 messages::propertyMissing(asyncResp->res,
1677 pathString + "/Gateway");
Ed Tanousbf648f72021-06-03 15:00:14 -07001678 return;
1679 }
1680
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001681 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001682 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001683 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
Ed Tanous77eb0152023-09-06 10:19:18 -07001684 nicIpEntry->id, prefixLength, *address,
1685 *gateway, asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001686 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1687 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001688 }
1689 else
1690 {
1691 createIPv4(ifaceId, prefixLength, *gateway, *address,
1692 asyncResp);
1693 }
1694 entryIdx++;
1695 }
1696 else
1697 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001698 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001699 {
1700 // Requesting a DELETE/DO NOT MODIFY action for an item
1701 // that isn't present on the eth(n) interface. Input JSON is
1702 // in error, so bail out.
1703 if (thisJson.is_null())
1704 {
1705 messages::resourceCannotBeDeleted(asyncResp->res);
1706 return;
1707 }
Ed Tanousf818b042022-06-27 13:17:35 -07001708 messages::propertyValueFormatError(asyncResp->res, thisJson,
1709 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001710 return;
1711 }
1712
1713 if (thisJson.is_null())
1714 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001715 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001716 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001717 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001718 {
Patrick Williams89492a12023-05-10 07:51:34 -05001719 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1720 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001721 }
1722 entryIdx++;
1723 }
1724 }
1725}
1726
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001727inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001728 const std::string& ifaceId,
1729 const std::vector<std::string>& updatedStaticNameServers,
1730 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1731{
George Liu9ae226f2023-06-21 17:56:46 +08001732 sdbusplus::asio::setProperty(
1733 *crow::connections::systemBus, "xyz.openbmc_project.Network",
1734 "/xyz/openbmc_project/network/" + ifaceId,
1735 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1736 updatedStaticNameServers,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001737 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001738 if (ec)
1739 {
1740 messages::internalError(asyncResp->res);
1741 return;
1742 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001743 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001744}
1745
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001746inline void handleIPv6StaticAddressesPatch(
Ed Tanousddd70dc2023-03-01 16:00:27 -08001747 const std::string& ifaceId, const nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001748 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001749 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1750{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001751 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001752 {
Ed Tanous2e8c4bd2022-06-27 12:59:12 -07001753 messages::propertyValueTypeError(asyncResp->res, input,
1754 "IPv6StaticAddresses");
Ed Tanousbf648f72021-06-03 15:00:14 -07001755 return;
1756 }
1757 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001758 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001759 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1760 for (const nlohmann::json& thisJson : input)
1761 {
Patrick Williams89492a12023-05-10 07:51:34 -05001762 std::string pathString = "IPv6StaticAddresses/" +
1763 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001764
1765 if (!thisJson.is_null() && !thisJson.empty())
1766 {
1767 std::optional<std::string> address;
1768 std::optional<uint8_t> prefixLength;
1769 nlohmann::json thisJsonCopy = thisJson;
1770 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1771 address, "PrefixLength", prefixLength))
1772 {
Ed Tanousf818b042022-06-27 13:17:35 -07001773 messages::propertyValueFormatError(asyncResp->res, thisJson,
1774 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001775 return;
1776 }
1777
Ed Tanous543f4402022-01-06 13:12:53 -08001778 const std::string* addr = nullptr;
1779 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001780
1781 // Find the address and prefixLength values. Any values that are
1782 // not explicitly provided are assumed to be unmodified from the
1783 // current state of the interface. Merge existing state into the
1784 // current request.
1785 if (address)
1786 {
1787 addr = &(*address);
1788 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001789 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001790 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001791 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001792 }
1793 else
1794 {
1795 messages::propertyMissing(asyncResp->res,
1796 pathString + "/Address");
1797 return;
1798 }
1799
1800 if (prefixLength)
1801 {
1802 prefix = *prefixLength;
1803 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001804 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001805 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001806 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001807 }
1808 else
1809 {
1810 messages::propertyMissing(asyncResp->res,
1811 pathString + "/PrefixLength");
1812 return;
1813 }
1814
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001815 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001816 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001817 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
Ed Tanous77eb0152023-09-06 10:19:18 -07001818 nicIpEntry->id, prefix, *addr, "",
Ravi Teja9c5e5852023-02-26 21:33:52 -06001819 asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001820 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1821 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001822 }
1823 else
1824 {
1825 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1826 }
1827 entryIdx++;
1828 }
1829 else
1830 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001831 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001832 {
1833 // Requesting a DELETE/DO NOT MODIFY action for an item
1834 // that isn't present on the eth(n) interface. Input JSON is
1835 // in error, so bail out.
1836 if (thisJson.is_null())
1837 {
1838 messages::resourceCannotBeDeleted(asyncResp->res);
1839 return;
1840 }
Ed Tanousf818b042022-06-27 13:17:35 -07001841 messages::propertyValueFormatError(asyncResp->res, thisJson,
1842 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001843 return;
1844 }
1845
1846 if (thisJson.is_null())
1847 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001848 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001849 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001850 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001851 {
Patrick Williams89492a12023-05-10 07:51:34 -05001852 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1853 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001854 }
1855 entryIdx++;
1856 }
1857 }
1858}
1859
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001860inline std::string extractParentInterfaceName(const std::string& ifaceId)
1861{
1862 std::size_t pos = ifaceId.find('_');
1863 return ifaceId.substr(0, pos);
1864}
1865
Ed Tanous77179532023-02-28 10:45:28 -08001866inline void
1867 parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1868 const std::string& ifaceId,
1869 const EthernetInterfaceData& ethData,
1870 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001871 const std::vector<IPv6AddressData>& ipv6Data,
1872 const std::vector<StaticGatewayData>& ipv6GatewayData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001873{
Ed Tanousbf648f72021-06-03 15:00:14 -07001874 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1875 jsonResponse["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001876 jsonResponse["@odata.id"] = boost::urls::format(
1877 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001878 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1879
Willy Tu13451e32023-05-24 16:08:18 -07001880 if constexpr (bmcwebEnableHealthPopulate)
1881 {
1882 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1883 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1884 auto health = std::make_shared<HealthPopulate>(asyncResp);
1885 dbus::utility::getSubTreePaths(
1886 "/", 0, inventoryForEthernet,
1887 [health](const boost::system::error_code& ec,
1888 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1889 if (ec)
1890 {
1891 return;
1892 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001893
Willy Tu13451e32023-05-24 16:08:18 -07001894 health->inventory = resp;
Patrick Williams5a39f772023-10-20 11:20:21 -05001895 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001896
Willy Tu13451e32023-05-24 16:08:18 -07001897 health->populate();
1898 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001899
1900 if (ethData.nicEnabled)
1901 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001902 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001903 jsonResponse["Status"]["State"] = "Enabled";
1904 }
1905 else
1906 {
1907 jsonResponse["LinkStatus"] = "NoLink";
1908 jsonResponse["Status"]["State"] = "Disabled";
1909 }
1910
Ed Tanousbf648f72021-06-03 15:00:14 -07001911 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301912 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001913 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001914 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001915 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
Jishnu CMe4588152023-05-11 00:04:40 -05001916 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1917 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
1918 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNamev4Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001919 jsonResponse["DHCPv6"]["OperatingMode"] =
Ravi Tejab10d8db2022-05-24 09:04:12 -05001920 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Enabled"
Ed Tanousbf648f72021-06-03 15:00:14 -07001921 : "Disabled";
Jishnu CMe4588152023-05-11 00:04:40 -05001922 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1923 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
1924 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNamev6Enabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -05001925 jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1926 ethData.ipv6AcceptRa;
Ed Tanousbf648f72021-06-03 15:00:14 -07001927
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001928 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001929 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001930 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001931
1932 // When domain name is empty then it means, that it is a network
1933 // without domain names, and the host name itself must be treated as
1934 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001935 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001936 if (!ethData.domainnames.empty())
1937 {
1938 fqdn += "." + ethData.domainnames[0];
1939 }
1940 jsonResponse["FQDN"] = fqdn;
1941 }
1942
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001943 if (ethData.vlanId)
1944 {
1945 jsonResponse["EthernetInterfaceType"] = "Virtual";
1946 jsonResponse["VLAN"]["VLANEnable"] = true;
1947 jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1948 jsonResponse["VLAN"]["Tagged"] = true;
1949
1950 nlohmann::json::array_t relatedInterfaces;
1951 nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1952 parentInterface["@odata.id"] =
1953 boost::urls::format("/redfish/v1/Managers/bmc/EthernetInterfaces",
1954 extractParentInterfaceName(ifaceId));
1955 jsonResponse["Links"]["RelatedInterfaces"] =
1956 std::move(relatedInterfaces);
1957 }
1958 else
1959 {
1960 jsonResponse["EthernetInterfaceType"] = "Physical";
1961 }
1962
Ed Tanousbf648f72021-06-03 15:00:14 -07001963 jsonResponse["NameServers"] = ethData.nameServers;
1964 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1965
1966 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1967 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1968 ipv4Array = nlohmann::json::array();
1969 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001970 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001971 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001972 std::string gatewayStr = ipv4Config.gateway;
1973 if (gatewayStr.empty())
1974 {
1975 gatewayStr = "0.0.0.0";
1976 }
Ed Tanous14766872022-03-15 10:44:42 -07001977 nlohmann::json::object_t ipv4;
1978 ipv4["AddressOrigin"] = ipv4Config.origin;
1979 ipv4["SubnetMask"] = ipv4Config.netmask;
1980 ipv4["Address"] = ipv4Config.address;
1981 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001982
Ed Tanousbf648f72021-06-03 15:00:14 -07001983 if (ipv4Config.origin == "Static")
1984 {
Ed Tanous14766872022-03-15 10:44:42 -07001985 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001986 }
Ed Tanous14766872022-03-15 10:44:42 -07001987
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001988 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001989 }
1990
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001991 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001992 if (ipv6GatewayStr.empty())
1993 {
1994 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1995 }
1996
1997 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1998
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001999 nlohmann::json::array_t ipv6StaticGatewayArray;
2000 for (const auto& ipv6GatewayConfig : ipv6GatewayData)
2001 {
2002 nlohmann::json::object_t ipv6Gateway;
2003 ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
2004 ipv6Gateway["PrefixLength"] = ipv6GatewayConfig.prefixLength;
2005 ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
2006 }
2007 jsonResponse["IPv6StaticDefaultGateways"] =
2008 std::move(ipv6StaticGatewayArray);
2009
Ed Tanousbf648f72021-06-03 15:00:14 -07002010 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
2011 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
2012 ipv6Array = nlohmann::json::array();
2013 ipv6StaticArray = nlohmann::json::array();
2014 nlohmann::json& ipv6AddrPolicyTable =
2015 jsonResponse["IPv6AddressPolicyTable"];
2016 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08002017 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07002018 {
Ed Tanous14766872022-03-15 10:44:42 -07002019 nlohmann::json::object_t ipv6;
2020 ipv6["Address"] = ipv6Config.address;
2021 ipv6["PrefixLength"] = ipv6Config.prefixLength;
2022 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05002023
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002024 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07002025 if (ipv6Config.origin == "Static")
2026 {
Ed Tanous14766872022-03-15 10:44:42 -07002027 nlohmann::json::object_t ipv6Static;
2028 ipv6Static["Address"] = ipv6Config.address;
2029 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002030 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07002031 }
2032 }
2033}
2034
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002035inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2036 const std::string& ifaceId,
2037 const boost::system::error_code& ec,
2038 const sdbusplus::message_t& m)
2039{
2040 if (!ec)
2041 {
2042 return;
2043 }
2044 const sd_bus_error* dbusError = m.get_error();
2045 if (dbusError == nullptr)
2046 {
2047 messages::internalError(asyncResp->res);
2048 return;
2049 }
Ed Tanous62598e32023-07-17 17:06:25 -07002050 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002051
2052 if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
2053 dbusError->name)
2054 {
2055 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2056 ifaceId);
2057 return;
2058 }
2059 if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
2060 dbusError->name)
2061 {
2062 messages::resourceCannotBeDeleted(asyncResp->res);
2063 return;
2064 }
2065 messages::internalError(asyncResp->res);
2066}
2067
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002068inline void afterVlanCreate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2069 const std::string& parentInterfaceUri,
2070 const std::string& vlanInterface,
2071 const boost::system::error_code& ec,
2072 const sdbusplus::message_t& m
2073
2074)
2075{
2076 if (ec)
2077 {
2078 const sd_bus_error* dbusError = m.get_error();
2079 if (dbusError == nullptr)
2080 {
2081 messages::internalError(asyncResp->res);
2082 return;
2083 }
Ed Tanous62598e32023-07-17 17:06:25 -07002084 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002085
2086 if (std::string_view(
2087 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
2088 dbusError->name)
2089 {
2090 messages::propertyValueNotInList(
2091 asyncResp->res, parentInterfaceUri,
2092 "Links/RelatedInterfaces/0/@odata.id");
2093 return;
2094 }
2095 if (std::string_view(
2096 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
2097 dbusError->name)
2098 {
2099 messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
2100 "Id", vlanInterface);
2101 return;
2102 }
2103 messages::internalError(asyncResp->res);
2104 return;
2105 }
2106
2107 const boost::urls::url vlanInterfaceUri = boost::urls::format(
2108 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", vlanInterface);
2109 asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
2110}
2111
Ed Tanousbf648f72021-06-03 15:00:14 -07002112inline void requestEthernetInterfacesRoutes(App& app)
2113{
2114 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07002115 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002116 .methods(boost::beast::http::verb::get)(
2117 [&app](const crow::Request& req,
2118 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002119 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002120 {
2121 return;
2122 }
2123
2124 asyncResp->res.jsonValue["@odata.type"] =
2125 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2126 asyncResp->res.jsonValue["@odata.id"] =
2127 "/redfish/v1/Managers/bmc/EthernetInterfaces";
2128 asyncResp->res.jsonValue["Name"] =
2129 "Ethernet Network Interface Collection";
2130 asyncResp->res.jsonValue["Description"] =
2131 "Collection of EthernetInterfaces for this Manager";
2132
2133 // Get eth interface list, and call the below callback for JSON
2134 // preparation
2135 getEthernetIfaceList(
Ed Tanous77179532023-02-28 10:45:28 -08002136 [asyncResp](const bool& success,
2137 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002138 if (!success)
2139 {
2140 messages::internalError(asyncResp->res);
2141 return;
2142 }
2143
2144 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
2145 ifaceArray = nlohmann::json::array();
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 for (const std::string& ifaceItem : ifaceList)
2147 {
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08002148 nlohmann::json::object_t iface;
2149 iface["@odata.id"] = boost::urls::format(
2150 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
2151 ifaceItem);
2152 ifaceArray.push_back(std::move(iface));
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002154
Ed Tanous002d39b2022-05-31 08:59:27 -07002155 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2156 asyncResp->res.jsonValue["@odata.id"] =
2157 "/redfish/v1/Managers/bmc/EthernetInterfaces";
2158 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002159 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002160
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002161 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
2162 .privileges(redfish::privileges::postEthernetInterfaceCollection)
2163 .methods(boost::beast::http::verb::post)(
2164 [&app](const crow::Request& req,
2165 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2166 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2167 {
2168 return;
2169 }
2170
2171 bool vlanEnable = false;
2172 uint32_t vlanId = 0;
2173 nlohmann::json::array_t relatedInterfaces;
2174
2175 if (!json_util::readJsonPatch(req, asyncResp->res, "VLAN/VLANEnable",
2176 vlanEnable, "VLAN/VLANId", vlanId,
2177 "Links/RelatedInterfaces",
2178 relatedInterfaces))
2179 {
2180 return;
2181 }
2182
2183 if (relatedInterfaces.size() != 1)
2184 {
2185 messages::arraySizeTooLong(asyncResp->res,
2186 "Links/RelatedInterfaces",
2187 relatedInterfaces.size());
2188 return;
2189 }
2190
2191 std::string parentInterfaceUri;
2192 if (!json_util::readJson(relatedInterfaces[0], asyncResp->res,
2193 "@odata.id", parentInterfaceUri))
2194 {
2195 messages::propertyMissing(asyncResp->res,
2196 "Links/RelatedInterfaces/0/@odata.id");
2197 return;
2198 }
Ed Tanous62598e32023-07-17 17:06:25 -07002199 BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002200
Ed Tanous6fd29552023-10-04 09:40:14 -07002201 boost::system::result<boost::urls::url_view> parsedUri =
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002202 boost::urls::parse_relative_ref(parentInterfaceUri);
2203 if (!parsedUri)
2204 {
2205 messages::propertyValueFormatError(
2206 asyncResp->res, parentInterfaceUri,
2207 "Links/RelatedInterfaces/0/@odata.id");
2208 return;
2209 }
2210
2211 std::string parentInterface;
2212 if (!crow::utility::readUrlSegments(
2213 *parsedUri, "redfish", "v1", "Managers", "bmc",
2214 "EthernetInterfaces", std::ref(parentInterface)))
2215 {
2216 messages::propertyValueNotInList(
2217 asyncResp->res, parentInterfaceUri,
2218 "Links/RelatedInterfaces/0/@odata.id");
2219 return;
2220 }
2221
2222 if (!vlanEnable)
2223 {
2224 // In OpenBMC implementation, VLANEnable cannot be false on
2225 // create
2226 messages::propertyValueIncorrect(asyncResp->res, "VLAN/VLANEnable",
2227 "false");
2228 return;
2229 }
2230
2231 std::string vlanInterface = parentInterface + "_" +
2232 std::to_string(vlanId);
2233 crow::connections::systemBus->async_method_call(
2234 [asyncResp, parentInterfaceUri,
2235 vlanInterface](const boost::system::error_code& ec,
2236 const sdbusplus::message_t& m) {
2237 afterVlanCreate(asyncResp, parentInterfaceUri, vlanInterface, ec,
2238 m);
Patrick Williams5a39f772023-10-20 11:20:21 -05002239 },
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002240 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
2241 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", parentInterface,
2242 vlanId);
Patrick Williams5a39f772023-10-20 11:20:21 -05002243 });
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002244
Ed Tanousbf648f72021-06-03 15:00:14 -07002245 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002246 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002247 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002248 [&app](const crow::Request& req,
2249 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2250 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002251 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002252 {
2253 return;
2254 }
2255 getEthernetIfaceData(
2256 ifaceId,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002257 [asyncResp,
2258 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2259 const std::vector<IPv4AddressData>& ipv4Data,
2260 const std::vector<IPv6AddressData>& ipv6Data,
2261 const std::vector<StaticGatewayData>& ipv6GatewayData) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002262 if (!success)
2263 {
2264 // TODO(Pawel)consider distinguish between non
2265 // existing object, and other errors
2266 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2267 ifaceId);
2268 return;
2269 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002270
Ed Tanous002d39b2022-05-31 08:59:27 -07002271 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao93bbc952023-02-23 14:11:20 +08002272 "#EthernetInterface.v1_9_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
2274 asyncResp->res.jsonValue["Description"] =
2275 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05302276
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002277 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data,
2278 ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07002279 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002280 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002281
Ed Tanousbf648f72021-06-03 15:00:14 -07002282 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002283 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002284 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002285 [&app](const crow::Request& req,
2286 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2287 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002288 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002289 {
2290 return;
2291 }
2292 std::optional<std::string> hostname;
2293 std::optional<std::string> fqdn;
2294 std::optional<std::string> macAddress;
2295 std::optional<std::string> ipv6DefaultGateway;
Ed Tanousddd70dc2023-03-01 16:00:27 -08002296 std::optional<nlohmann::json::array_t> ipv4StaticAddresses;
2297 std::optional<nlohmann::json::array_t> ipv6StaticAddresses;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002298 std::optional<nlohmann::json::array_t> ipv6StaticDefaultGateways;
Ed Tanous002d39b2022-05-31 08:59:27 -07002299 std::optional<std::vector<std::string>> staticNameServers;
2300 std::optional<nlohmann::json> dhcpv4;
2301 std::optional<nlohmann::json> dhcpv6;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002302 std::optional<bool> ipv6AutoConfigEnabled;
Ed Tanous002d39b2022-05-31 08:59:27 -07002303 std::optional<bool> interfaceEnabled;
2304 std::optional<size_t> mtuSize;
2305 DHCPParameters v4dhcpParms;
2306 DHCPParameters v6dhcpParms;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002307 // clang-format off
Ed Tanous002d39b2022-05-31 08:59:27 -07002308 if (!json_util::readJsonPatch(
Ravi Tejab10d8db2022-05-24 09:04:12 -05002309 req, asyncResp->res,
2310 "DHCPv4", dhcpv4,
2311 "DHCPv6", dhcpv6,
2312 "FQDN", fqdn,
2313 "HostName", hostname,
2314 "IPv4StaticAddresses", ipv4StaticAddresses,
2315 "IPv6DefaultGateway", ipv6DefaultGateway,
2316 "IPv6StaticAddresses", ipv6StaticAddresses,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002317 "IPv6StaticDefaultGateways", ipv6StaticDefaultGateways,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002318 "InterfaceEnabled", interfaceEnabled,
2319 "MACAddress", macAddress,
2320 "MTUSize", mtuSize,
2321 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled", ipv6AutoConfigEnabled,
2322 "StaticNameServers", staticNameServers
2323 )
2324 )
Ed Tanous002d39b2022-05-31 08:59:27 -07002325 {
2326 return;
2327 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05002328 //clang-format on
Ed Tanous002d39b2022-05-31 08:59:27 -07002329 if (dhcpv4)
2330 {
2331 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
2332 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
2333 v4dhcpParms.useDnsServers, "UseNTPServers",
2334 v4dhcpParms.useNtpServers, "UseDomainName",
2335 v4dhcpParms.useDomainName))
2336 {
2337 return;
2338 }
2339 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002340
Ed Tanous002d39b2022-05-31 08:59:27 -07002341 if (dhcpv6)
2342 {
2343 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
2344 v6dhcpParms.dhcpv6OperatingMode,
2345 "UseDNSServers", v6dhcpParms.useDnsServers,
2346 "UseNTPServers", v6dhcpParms.useNtpServers,
2347 "UseDomainName",
2348 v6dhcpParms.useDomainName))
2349 {
2350 return;
2351 }
2352 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002353
Ed Tanous002d39b2022-05-31 08:59:27 -07002354 // Get single eth interface data, and call the below callback
2355 // for JSON preparation
2356 getEthernetIfaceData(
2357 ifaceId,
2358 [asyncResp, ifaceId, hostname = std::move(hostname),
2359 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2360 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2361 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2362 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002363 ipv6StaticDefaultGateway = std::move(ipv6StaticDefaultGateways),
Ed Tanous002d39b2022-05-31 08:59:27 -07002364 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07002365 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002366 ipv6AutoConfigEnabled, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07002367 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2368 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanous77179532023-02-28 10:45:28 -08002369 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002370 const std::vector<IPv6AddressData>& ipv6Data,
2371 const std::vector<StaticGatewayData>& ipv6GatewayData) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002372 if (!success)
2373 {
2374 // ... otherwise return error
2375 // TODO(Pawel)consider distinguish between non
2376 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002377 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002378 ifaceId);
2379 return;
2380 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002381
Ed Tanous002d39b2022-05-31 08:59:27 -07002382 if (dhcpv4 || dhcpv6)
2383 {
2384 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07002385 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002386 }
Tejas Patil35fb5312021-09-20 15:35:20 +05302387
Ed Tanous002d39b2022-05-31 08:59:27 -07002388 if (hostname)
2389 {
2390 handleHostnamePatch(*hostname, asyncResp);
2391 }
2392
Ravi Tejab10d8db2022-05-24 09:04:12 -05002393 if (ipv6AutoConfigEnabled)
2394 {
2395 handleSLAACAutoConfigPatch(ifaceId, *ipv6AutoConfigEnabled,
2396 asyncResp);
2397 }
2398
Ed Tanous002d39b2022-05-31 08:59:27 -07002399 if (fqdn)
2400 {
2401 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2402 }
2403
2404 if (macAddress)
2405 {
2406 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2407 }
2408
2409 if (ipv4StaticAddresses)
2410 {
2411 // TODO(ed) for some reason the capture of
2412 // ipv4Addresses above is returning a const value,
2413 // not a non-const value. This doesn't really work
2414 // for us, as we need to be able to efficiently move
2415 // out the intermedia nlohmann::json objects. This
2416 // makes a copy of the structure, and operates on
2417 // that, but could be done more efficiently
Ed Tanousddd70dc2023-03-01 16:00:27 -08002418 nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses;
Ed Tanous002d39b2022-05-31 08:59:27 -07002419 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
2420 }
2421
2422 if (staticNameServers)
2423 {
2424 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2425 asyncResp);
2426 }
2427
2428 if (ipv6DefaultGateway)
2429 {
2430 messages::propertyNotWritable(asyncResp->res,
2431 "IPv6DefaultGateway");
2432 }
2433
2434 if (ipv6StaticAddresses)
2435 {
Ed Tanousddd70dc2023-03-01 16:00:27 -08002436 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
2437 ipv6Data, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002438 }
2439
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002440 if (ipv6StaticDefaultGateway)
2441 {
2442 handleIPv6DefaultGateway(ifaceId, *ipv6StaticDefaultGateway,
2443 ipv6GatewayData, asyncResp);
2444 }
2445
Ed Tanous002d39b2022-05-31 08:59:27 -07002446 if (interfaceEnabled)
2447 {
2448 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2449 *interfaceEnabled, asyncResp);
2450 }
2451
2452 if (mtuSize)
2453 {
2454 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2455 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002456 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002457 });
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002458
2459 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
2460 .privileges(redfish::privileges::deleteEthernetInterface)
2461 .methods(boost::beast::http::verb::delete_)(
2462 [&app](const crow::Request& req,
2463 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2464 const std::string& ifaceId) {
2465 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2466 {
2467 return;
2468 }
2469
2470 crow::connections::systemBus->async_method_call(
2471 [asyncResp, ifaceId](const boost::system::error_code& ec,
2472 const sdbusplus::message_t& m) {
2473 afterDelete(asyncResp, ifaceId, ec, m);
2474 },
2475 "xyz.openbmc_project.Network",
2476 std::string("/xyz/openbmc_project/network/") + ifaceId,
2477 "xyz.openbmc_project.Object.Delete", "Delete");
2478 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002479}
2480
Ed Tanous1abe55e2018-09-05 08:30:59 -07002481} // namespace redfish