blob: 876bc2edb485ce884b9dc0e0aa962c3c1e734809 [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "error_messages.hpp"
Ed Tanous2c5875a2023-05-15 09:56:06 -070022#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "query.hpp"
24#include "registries/privilege_registry.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070025#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070027
Sunitha Harishce73d5c2023-04-07 06:46:49 -050028#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070029#include <boost/url/format.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030
George Liu7a1dbc42022-12-07 16:03:22 +080031#include <array>
Ed Tanous3dfed532024-03-06 14:41:27 -080032#include <cstddef>
Sunitha Harishce73d5c2023-04-07 06:46:49 -050033#include <memory>
Ed Tanousa24526d2018-12-10 15:17:59 -080034#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070035#include <ranges>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053036#include <regex>
George Liu7a1dbc42022-12-07 16:03:22 +080037#include <string_view>
Ed Tanous3dfed532024-03-06 14:41:27 -080038#include <variant>
Ed Tanous77179532023-02-28 10:45:28 -080039#include <vector>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010040
Ed Tanous1abe55e2018-09-05 08:30:59 -070041namespace redfish
42{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010043
Ed Tanous4a0cb852018-10-15 07:55:04 -070044enum class LinkType
45{
46 Local,
47 Global
48};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010049
50/**
51 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010052 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070053struct IPv4AddressData
54{
55 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070056 std::string address;
57 std::string domain;
58 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 std::string netmask;
60 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080061 LinkType linktype{};
62 bool isActive{};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010063};
64
65/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050066 * Structure for keeping IPv6 data required by Redfish
67 */
68struct IPv6AddressData
69{
70 std::string id;
71 std::string address;
72 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080073 uint8_t prefixLength = 0;
Ravi Tejae48c0fc2019-04-16 08:37:20 -050074};
Sunitha Harishce73d5c2023-04-07 06:46:49 -050075
76/**
77 * Structure for keeping static route data required by Redfish
78 */
79struct StaticGatewayData
80{
81 std::string id;
82 std::string gateway;
83 size_t prefixLength = 0;
84 std::string protocol;
85};
86
Ravi Tejae48c0fc2019-04-16 08:37:20 -050087/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010088 * Structure for keeping basic single Ethernet Interface information
89 * available from DBus
90 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070091struct EthernetInterfaceData
92{
Ed Tanous4a0cb852018-10-15 07:55:04 -070093 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053094 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080095 bool autoNeg;
Jishnu CMe4588152023-05-11 00:04:40 -050096 bool dnsv4Enabled;
97 bool dnsv6Enabled;
Ravi Teja91c441e2024-02-23 09:03:43 -060098 bool domainv4Enabled;
99 bool domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -0500100 bool ntpv4Enabled;
101 bool ntpv6Enabled;
102 bool hostNamev4Enabled;
103 bool hostNamev6Enabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800104 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700105 bool nicEnabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -0500106 bool ipv6AcceptRa;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800107 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700108 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800109 std::string hostName;
110 std::string defaultGateway;
111 std::string ipv6DefaultGateway;
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500112 std::string ipv6StaticDefaultGateway;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800113 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800114 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500115 std::vector<std::string> nameServers;
116 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800117 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100118};
119
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700120struct DHCPParameters
121{
122 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800123 std::optional<bool> useDnsServers;
124 std::optional<bool> useNtpServers;
125 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700126 std::optional<std::string> dhcpv6OperatingMode;
127};
128
Ed Tanous4a0cb852018-10-15 07:55:04 -0700129// Helper function that changes bits netmask notation (i.e. /24)
130// into full dot notation
131inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700133 uint32_t value = 0xffffffff << (32 - bits);
134 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
135 std::to_string((value >> 16) & 0xff) + "." +
136 std::to_string((value >> 8) & 0xff) + "." +
137 std::to_string(value & 0xff);
138 return netmask;
139}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100140
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800141inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700142 bool isIPv4)
143{
144 if (isIPv4)
145 {
146 return (
147 (inputDHCP ==
148 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
149 (inputDHCP ==
150 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
151 }
152 return ((inputDHCP ==
153 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
154 (inputDHCP ==
155 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
156}
157
Ed Tanous2c70f802020-09-28 14:29:23 -0700158inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700159{
160 if (isIPv4 && isIPv6)
161 {
162 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
163 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700164 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700165 {
166 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
167 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700168 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700169 {
170 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
171 }
172 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
173}
174
Ed Tanous4a0cb852018-10-15 07:55:04 -0700175inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500176 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700177 bool isIPv4)
178{
179 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700181 return "Static";
182 }
183 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
184 {
185 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700187 return "IPv4LinkLocal";
188 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700189 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700190 }
191 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
192 {
193 if (isIPv4)
194 {
195 return "DHCP";
196 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700197 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700198 }
199 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
200 {
201 return "SLAAC";
202 }
203 return "";
204}
205
Ed Tanous02cad962022-06-30 16:50:15 -0700206inline bool extractEthernetInterfaceData(
207 const std::string& ethifaceId,
208 const dbus::utility::ManagedObjectType& dbusData,
209 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700210{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700211 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700212 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700213 {
Ed Tanous02cad962022-06-30 16:50:15 -0700214 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000216 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700218 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500221 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700222 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500225 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800226 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700227 if (mac != nullptr)
228 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800229 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700230 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700232 }
233 }
234 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
235 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500236 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700237 {
238 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500240 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800241 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700242 if (id != nullptr)
243 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800244 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700245 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 }
248 }
249 else if (ifacePair.first ==
250 "xyz.openbmc_project.Network.EthernetInterface")
251 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500252 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700253 {
254 if (propertyPair.first == "AutoNeg")
255 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700256 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800257 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700258 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700259 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800260 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700261 }
262 }
263 else if (propertyPair.first == "Speed")
264 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500265 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800266 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700267 if (speed != nullptr)
268 {
269 ethData.speed = *speed;
270 }
271 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530272 else if (propertyPair.first == "MTU")
273 {
Anthony3e7a8da2023-10-23 14:22:43 +0800274 const size_t* mtuSize =
275 std::get_if<size_t>(&propertyPair.second);
Tejas Patil35fb5312021-09-20 15:35:20 +0530276 if (mtuSize != nullptr)
277 {
278 ethData.mtuSize = *mtuSize;
279 }
280 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800281 else if (propertyPair.first == "LinkUp")
282 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500283 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800284 std::get_if<bool>(&propertyPair.second);
285 if (linkUp != nullptr)
286 {
287 ethData.linkUp = *linkUp;
288 }
289 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700290 else if (propertyPair.first == "NICEnabled")
291 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500292 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700293 std::get_if<bool>(&propertyPair.second);
294 if (nicEnabled != nullptr)
295 {
296 ethData.nicEnabled = *nicEnabled;
297 }
298 }
Ravi Tejab10d8db2022-05-24 09:04:12 -0500299 else if (propertyPair.first == "IPv6AcceptRA")
300 {
301 const bool* ipv6AcceptRa =
302 std::get_if<bool>(&propertyPair.second);
303 if (ipv6AcceptRa != nullptr)
304 {
305 ethData.ipv6AcceptRa = *ipv6AcceptRa;
306 }
307 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500308 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700309 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500310 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500311 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800312 &propertyPair.second);
313 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700314 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700315 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500316 }
317 }
318 else if (propertyPair.first == "StaticNameServers")
319 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500320 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500321 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500322 &propertyPair.second);
323 if (staticNameServers != nullptr)
324 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700325 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700326 }
327 }
manojkiraneda2a133282019-02-19 13:09:43 +0530328 else if (propertyPair.first == "DHCPEnabled")
329 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700330 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700331 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700332 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530333 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800334 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530335 }
336 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800337 else if (propertyPair.first == "DomainName")
338 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500339 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500340 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800341 &propertyPair.second);
342 if (domainNames != nullptr)
343 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700344 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800345 }
346 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500347 else if (propertyPair.first == "DefaultGateway")
348 {
349 const std::string* defaultGateway =
350 std::get_if<std::string>(&propertyPair.second);
351 if (defaultGateway != nullptr)
352 {
353 std::string defaultGatewayStr = *defaultGateway;
354 if (defaultGatewayStr.empty())
355 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800356 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500357 }
358 else
359 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800360 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500361 }
362 }
363 }
364 else if (propertyPair.first == "DefaultGateway6")
365 {
366 const std::string* defaultGateway6 =
367 std::get_if<std::string>(&propertyPair.second);
368 if (defaultGateway6 != nullptr)
369 {
370 std::string defaultGateway6Str =
371 *defaultGateway6;
372 if (defaultGateway6Str.empty())
373 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800374 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500375 "0:0:0:0:0:0:0:0";
376 }
377 else
378 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800379 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500380 defaultGateway6Str;
381 }
382 }
383 }
Ed Tanous029573d2019-02-01 10:57:49 -0800384 }
385 }
386 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700387
Jishnu CMe4588152023-05-11 00:04:40 -0500388 sdbusplus::message::object_path path(
389 "/xyz/openbmc_project/network");
390 sdbusplus::message::object_path dhcp4Path = path / ethifaceId /
391 "dhcp4";
392
393 if (sdbusplus::message::object_path(objpath.first) == dhcp4Path)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700394 {
395 if (ifacePair.first ==
396 "xyz.openbmc_project.Network.DHCPConfiguration")
397 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500398 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700399 {
400 if (propertyPair.first == "DNSEnabled")
401 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700402 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700403 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700404 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700405 {
Jishnu CMe4588152023-05-11 00:04:40 -0500406 ethData.dnsv4Enabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700407 }
408 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600409 else if (propertyPair.first == "DomainEnabled")
410 {
411 const bool* domainEnabled =
412 std::get_if<bool>(&propertyPair.second);
413 if (domainEnabled != nullptr)
414 {
415 ethData.domainv4Enabled = *domainEnabled;
416 }
417 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700418 else if (propertyPair.first == "NTPEnabled")
419 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700420 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700421 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700422 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700423 {
Jishnu CMe4588152023-05-11 00:04:40 -0500424 ethData.ntpv4Enabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700425 }
426 }
427 else if (propertyPair.first == "HostNameEnabled")
428 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700429 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700430 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700431 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700432 {
Jishnu CMe4588152023-05-11 00:04:40 -0500433 ethData.hostNamev4Enabled = *hostNameEnabled;
434 }
435 }
436 }
437 }
438 }
439
440 sdbusplus::message::object_path dhcp6Path = path / ethifaceId /
441 "dhcp6";
442
443 if (sdbusplus::message::object_path(objpath.first) == dhcp6Path)
444 {
445 if (ifacePair.first ==
446 "xyz.openbmc_project.Network.DHCPConfiguration")
447 {
448 for (const auto& propertyPair : ifacePair.second)
449 {
450 if (propertyPair.first == "DNSEnabled")
451 {
452 const bool* dnsEnabled =
453 std::get_if<bool>(&propertyPair.second);
454 if (dnsEnabled != nullptr)
455 {
456 ethData.dnsv6Enabled = *dnsEnabled;
457 }
458 }
Ravi Teja91c441e2024-02-23 09:03:43 -0600459 if (propertyPair.first == "DomainEnabled")
460 {
461 const bool* domainEnabled =
462 std::get_if<bool>(&propertyPair.second);
463 if (domainEnabled != nullptr)
464 {
465 ethData.domainv6Enabled = *domainEnabled;
466 }
467 }
Jishnu CMe4588152023-05-11 00:04:40 -0500468 else if (propertyPair.first == "NTPEnabled")
469 {
470 const bool* ntpEnabled =
471 std::get_if<bool>(&propertyPair.second);
472 if (ntpEnabled != nullptr)
473 {
474 ethData.ntpv6Enabled = *ntpEnabled;
475 }
476 }
477 else if (propertyPair.first == "HostNameEnabled")
478 {
479 const bool* hostNameEnabled =
480 std::get_if<bool>(&propertyPair.second);
481 if (hostNameEnabled != nullptr)
482 {
483 ethData.hostNamev6Enabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700484 }
485 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700486 }
487 }
488 }
Ed Tanous029573d2019-02-01 10:57:49 -0800489 // System configuration shows up in the global namespace, so no need
490 // to check eth number
491 if (ifacePair.first ==
492 "xyz.openbmc_project.Network.SystemConfiguration")
493 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500494 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800495 {
496 if (propertyPair.first == "HostName")
497 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500498 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500499 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800500 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700501 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800502 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800503 }
504 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700505 }
506 }
507 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700508 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700509 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700510}
511
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500512// Helper function that extracts data for single ethernet ipv6 address
Ed Tanous77179532023-02-28 10:45:28 -0800513inline void extractIPV6Data(const std::string& ethifaceId,
514 const dbus::utility::ManagedObjectType& dbusData,
515 std::vector<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500516{
Patrick Williams89492a12023-05-10 07:51:34 -0500517 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
518 ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500519
520 // Since there might be several IPv6 configurations aligned with
521 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000522 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500523 {
524 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800525 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500526 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800527 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500528 {
529 if (interface.first == "xyz.openbmc_project.Network.IP")
530 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700531 auto type = std::ranges::find_if(interface.second,
532 [](const auto& property) {
Tony Lee353163e2022-11-23 11:06:10 +0800533 return property.first == "Type";
534 });
535 if (type == interface.second.end())
536 {
537 continue;
538 }
539
540 const std::string* typeStr =
541 std::get_if<std::string>(&type->second);
542
543 if (typeStr == nullptr ||
544 (*typeStr !=
545 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
546 {
547 continue;
548 }
549
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500550 // Instance IPv6AddressData structure, and set as
551 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800552 IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700553 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800554 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800555 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500556 {
557 if (property.first == "Address")
558 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500559 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500560 std::get_if<std::string>(&property.second);
561 if (address != nullptr)
562 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700563 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500564 }
565 }
566 else if (property.first == "Origin")
567 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500568 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500569 std::get_if<std::string>(&property.second);
570 if (origin != nullptr)
571 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700572 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500573 translateAddressOriginDbusToRedfish(*origin,
574 false);
575 }
576 }
577 else if (property.first == "PrefixLength")
578 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500579 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500580 std::get_if<uint8_t>(&property.second);
581 if (prefix != nullptr)
582 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700583 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500584 }
585 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600586 else if (property.first == "Type" ||
587 property.first == "Gateway")
588 {
589 // Type & Gateway is not used
590 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500591 else
592 {
Ed Tanous62598e32023-07-17 17:06:25 -0700593 BMCWEB_LOG_ERROR(
594 "Got extra property: {} on the {} object",
595 property.first, objpath.first.str);
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500596 }
597 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500598 }
599 }
600 }
601 }
602}
603
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604// Helper function that extracts data for single ethernet ipv4 address
Ed Tanous77179532023-02-28 10:45:28 -0800605inline void extractIPData(const std::string& ethifaceId,
606 const dbus::utility::ManagedObjectType& dbusData,
607 std::vector<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700608{
Patrick Williams89492a12023-05-10 07:51:34 -0500609 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
610 ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700611
612 // Since there might be several IPv4 configurations aligned with
613 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000614 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700615 {
616 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800617 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700618 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800619 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700620 {
621 if (interface.first == "xyz.openbmc_project.Network.IP")
622 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700623 auto type = std::ranges::find_if(interface.second,
624 [](const auto& property) {
Tony Lee353163e2022-11-23 11:06:10 +0800625 return property.first == "Type";
626 });
627 if (type == interface.second.end())
628 {
629 continue;
630 }
631
632 const std::string* typeStr =
633 std::get_if<std::string>(&type->second);
634
635 if (typeStr == nullptr ||
636 (*typeStr !=
637 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
638 {
639 continue;
640 }
641
Ed Tanous4a0cb852018-10-15 07:55:04 -0700642 // Instance IPv4AddressData structure, and set as
643 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800644 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700645 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800646 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800647 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 {
649 if (property.first == "Address")
650 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500651 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800652 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 if (address != nullptr)
654 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700655 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700656 }
657 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700658 else if (property.first == "Origin")
659 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500660 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800661 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 if (origin != nullptr)
663 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700664 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700665 translateAddressOriginDbusToRedfish(*origin,
666 true);
667 }
668 }
669 else if (property.first == "PrefixLength")
670 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500671 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800672 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700673 if (mask != nullptr)
674 {
675 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700676 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700677 }
678 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600679 else if (property.first == "Type" ||
680 property.first == "Gateway")
681 {
682 // Type & Gateway is not used
683 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700684 else
685 {
Ed Tanous62598e32023-07-17 17:06:25 -0700686 BMCWEB_LOG_ERROR(
687 "Got extra property: {} on the {} object",
688 property.first, objpath.first.str);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700689 }
690 }
691 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700692 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700693 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700694 ? LinkType::Local
695 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700696 }
697 }
698 }
699 }
700}
701
702/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700703 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700704 *
705 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700706 * @param[in] ipHash DBus Hash id of IP that should be deleted
707 * @param[io] asyncResp Response object that will be returned to client
708 *
709 * @return None
710 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600711inline void deleteIPAddress(const std::string& ifaceId,
712 const std::string& ipHash,
713 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700714{
715 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800716 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700717 if (ec)
718 {
719 messages::internalError(asyncResp->res);
720 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500721 },
Ed Tanous4a0cb852018-10-15 07:55:04 -0700722 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600723 "/xyz/openbmc_project/network/" + ifaceId + ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700724 "xyz.openbmc_project.Object.Delete", "Delete");
725}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700726
Gunnar Mills244b6d52021-04-12 15:44:23 -0500727inline void updateIPv4DefaultGateway(
728 const std::string& ifaceId, const std::string& gateway,
729 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500730{
Ed Tanousd02aad32024-02-13 14:43:34 -0800731 setDbusProperty(
732 asyncResp, "xyz.openbmc_project.Network",
733 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
734 ifaceId,
Ravi Teja9010ec22019-08-01 23:30:25 -0500735 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanousd02aad32024-02-13 14:43:34 -0800736 "Gateway", gateway);
Ravi Teja9010ec22019-08-01 23:30:25 -0500737}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700738/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700739 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700740 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700741 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
742 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
743 * @param[in] gateway IPv4 address of this interfaces gateway
744 * @param[in] address IPv4 address to assign to this interface
745 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700746 *
747 * @return None
748 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000749inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
750 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800751 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700752{
Patrick Williams5a39f772023-10-20 11:20:21 -0500753 auto createIpHandler = [asyncResp, ifaceId,
754 gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500755 if (ec)
756 {
757 messages::internalError(asyncResp->res);
758 return;
759 }
760 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
761 };
762
Ed Tanous4a0cb852018-10-15 07:55:04 -0700763 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500764 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700765 "/xyz/openbmc_project/network/" + ifaceId,
766 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700767 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700768 gateway);
769}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500770
771/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700772 * @brief Deletes the IPv6 entry for this interface and creates a replacement
773 * static IPv6 entry
774 *
775 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
776 * @param[in] id The unique hash entry identifying the DBus entry
777 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
778 * @param[in] address IPv6 address to assign to this interface
779 * @param[io] asyncResp Response object that will be returned to client
780 *
781 * @return None
782 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600783
784enum class IpVersion
785{
786 IpV4,
787 IpV6
788};
789
790inline void deleteAndCreateIPAddress(
791 IpVersion version, const std::string& ifaceId, const std::string& id,
792 uint8_t prefixLength, const std::string& address,
793 const std::string& gateway,
794 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700795{
796 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600797 [asyncResp, version, ifaceId, address, prefixLength,
798 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700799 if (ec)
800 {
801 messages::internalError(asyncResp->res);
802 }
Ravi Teja9c5e5852023-02-26 21:33:52 -0600803 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
804 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous002d39b2022-05-31 08:59:27 -0700805 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800806 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700807 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700808 {
809 messages::internalError(asyncResp->res);
810 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500811 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700812 "xyz.openbmc_project.Network",
813 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600814 "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
815 prefixLength, gateway);
Patrick Williams5a39f772023-10-20 11:20:21 -0500816 },
Johnathan Mantey01784822019-06-18 12:44:21 -0700817 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600818 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700819 "xyz.openbmc_project.Object.Delete", "Delete");
820}
821
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500822inline bool extractIPv6DefaultGatewayData(
823 const std::string& ethifaceId,
824 const dbus::utility::ManagedObjectType& dbusData,
825 std::vector<StaticGatewayData>& staticGatewayConfig)
826{
827 std::string staticGatewayPathStart("/xyz/openbmc_project/network/");
828 staticGatewayPathStart += ethifaceId;
829
830 for (const auto& objpath : dbusData)
831 {
832 if (!std::string_view(objpath.first.str)
833 .starts_with(staticGatewayPathStart))
834 {
835 continue;
836 }
837 for (const auto& interface : objpath.second)
838 {
839 if (interface.first != "xyz.openbmc_project.Network.StaticGateway")
840 {
841 continue;
842 }
843 StaticGatewayData& staticGateway =
844 staticGatewayConfig.emplace_back();
845 staticGateway.id = objpath.first.filename();
846
847 bool success = sdbusplus::unpackPropertiesNoThrow(
848 redfish::dbus_utils::UnpackErrorPrinter(), interface.second,
849 "Gateway", staticGateway.gateway, "PrefixLength",
850 staticGateway.prefixLength, "ProtocolType",
851 staticGateway.protocol);
852 if (!success)
853 {
854 return false;
855 }
856 }
857 }
858 return true;
859}
860
Johnathan Mantey01784822019-06-18 12:44:21 -0700861/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500862 * @brief Creates IPv6 with given data
863 *
864 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500865 * @param[in] prefixLength Prefix length that needs to be added
866 * @param[in] address IP address that needs to be added
867 * @param[io] asyncResp Response object that will be returned to client
868 *
869 * @return None
870 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500871inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
872 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800873 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500874{
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500875 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
876 path /= ifaceId;
877
Patrick Williams5a39f772023-10-20 11:20:21 -0500878 auto createIpHandler = [asyncResp,
879 address](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500880 if (ec)
881 {
Nitin Kumar Kotaniafc23ef82023-06-29 04:55:09 -0500882 if (ec == boost::system::errc::io_error)
883 {
884 messages::propertyValueFormatError(asyncResp->res, address,
885 "Address");
886 }
887 else
888 {
889 messages::internalError(asyncResp->res);
890 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500891 }
892 };
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500893 // Passing null for gateway, as per redfish spec IPv6StaticAddresses
894 // object does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500895 crow::connections::systemBus->async_method_call(
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500896 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500897 "xyz.openbmc_project.Network.IP.Create", "IP",
898 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
899 "");
900}
901
Ed Tanous4a0cb852018-10-15 07:55:04 -0700902/**
Sunitha Harishce73d5c2023-04-07 06:46:49 -0500903 * @brief Deletes given IPv6 Static Gateway
904 *
905 * @param[in] ifaceId Id of interface whose IP should be deleted
906 * @param[in] ipHash DBus Hash id of IP that should be deleted
907 * @param[io] asyncResp Response object that will be returned to client
908 *
909 * @return None
910 */
911inline void
912 deleteIPv6Gateway(std::string_view gatewayId,
913 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
914{
915 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
916 path /= gatewayId;
917 crow::connections::systemBus->async_method_call(
918 [asyncResp](const boost::system::error_code& ec) {
919 if (ec)
920 {
921 messages::internalError(asyncResp->res);
922 }
923 },
924 "xyz.openbmc_project.Network", path,
925 "xyz.openbmc_project.Object.Delete", "Delete");
926}
927
928/**
929 * @brief Creates IPv6 static default gateway with given data
930 *
931 * @param[in] ifaceId Id of interface whose IP should be added
932 * @param[in] prefixLength Prefix length that needs to be added
933 * @param[in] gateway Gateway address that needs to be added
934 * @param[io] asyncResp Response object that will be returned to client
935 *
936 * @return None
937 */
938inline void createIPv6DefaultGateway(
939 std::string_view ifaceId, size_t prefixLength, std::string_view gateway,
940 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
941{
942 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
943 path /= ifaceId;
944 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
945 if (ec)
946 {
947 messages::internalError(asyncResp->res);
948 }
949 };
950 crow::connections::systemBus->async_method_call(
951 std::move(createIpHandler), "xyz.openbmc_project.Network", path,
952 "xyz.openbmc_project.Network.StaticGateway.Create", "StaticGateway",
953 gateway, prefixLength, "xyz.openbmc_project.Network.IP.Protocol.IPv6");
954}
955
956/**
957 * @brief Deletes the IPv6 default gateway entry for this interface and
958 * creates a replacement IPv6 default gateway entry
959 *
960 * @param[in] ifaceId Id of interface upon which to create the IPv6
961 * entry
962 * @param[in] gateway IPv6 gateway to assign to this interface
963 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
964 * @param[io] asyncResp Response object that will be returned to client
965 *
966 * @return None
967 */
968inline void deleteAndCreateIPv6DefaultGateway(
969 std::string_view ifaceId, std::string_view gatewayId,
970 std::string_view gateway, size_t prefixLength,
971 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
972{
973 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
974 path /= gatewayId;
975 crow::connections::systemBus->async_method_call(
976 [asyncResp, ifaceId, gateway,
977 prefixLength](const boost::system::error_code& ec) {
978 if (ec)
979 {
980 messages::internalError(asyncResp->res);
981 return;
982 }
983 createIPv6DefaultGateway(ifaceId, prefixLength, gateway, asyncResp);
984 },
985 "xyz.openbmc_project.Network", path,
986 "xyz.openbmc_project.Object.Delete", "Delete");
987}
988
989/**
990 * @brief Sets IPv6 default gateway with given data
991 *
992 * @param[in] ifaceId Id of interface whose gateway should be added
993 * @param[in] input Contains address that needs to be added
994 * @param[in] staticGatewayData Current static gateways in the system
995 * @param[io] asyncResp Response object that will be returned to client
996 *
997 * @return None
998 */
999
1000inline void handleIPv6DefaultGateway(
Ed Tanous3dfed532024-03-06 14:41:27 -08001001 const std::string& ifaceId,
1002 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001003 const std::vector<StaticGatewayData>& staticGatewayData,
1004 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1005{
1006 size_t entryIdx = 1;
1007 std::vector<StaticGatewayData>::const_iterator staticGatewayEntry =
1008 staticGatewayData.begin();
1009
Ed Tanous3dfed532024-03-06 14:41:27 -08001010 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1011 input)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001012 {
1013 // find the next gateway entry
1014 while (staticGatewayEntry != staticGatewayData.end())
1015 {
1016 if (staticGatewayEntry->protocol ==
1017 "xyz.openbmc_project.Network.IP.Protocol.IPv6")
1018 {
1019 break;
1020 }
1021 staticGatewayEntry++;
1022 }
1023 std::string pathString = "IPv6StaticDefaultGateways/" +
1024 std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001025 nlohmann::json::object_t* obj =
1026 std::get_if<nlohmann::json::object_t>(&thisJson);
1027 if (obj == nullptr)
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001028 {
1029 if (staticGatewayEntry == staticGatewayData.end())
1030 {
1031 messages::resourceCannotBeDeleted(asyncResp->res);
1032 return;
1033 }
1034 deleteIPv6Gateway(staticGatewayEntry->id, asyncResp);
1035 return;
1036 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001037 if (obj->empty())
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001038 {
1039 // Do nothing, but make sure the entry exists.
1040 if (staticGatewayEntry == staticGatewayData.end())
1041 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001042 messages::propertyValueFormatError(asyncResp->res, *obj,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001043 pathString);
1044 return;
1045 }
1046 }
1047 std::optional<std::string> address;
1048 std::optional<size_t> prefixLength;
1049
Ed Tanous3dfed532024-03-06 14:41:27 -08001050 if (!json_util::readJsonObject(*obj, asyncResp->res, "Address", address,
1051 "PrefixLength", prefixLength))
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001052 {
1053 return;
1054 }
1055 const std::string* addr = nullptr;
1056 size_t prefix = 0;
1057 if (address)
1058 {
1059 addr = &(*address);
1060 }
1061 else if (staticGatewayEntry != staticGatewayData.end())
1062 {
1063 addr = &(staticGatewayEntry->gateway);
1064 }
1065 else
1066 {
1067 messages::propertyMissing(asyncResp->res, pathString + "/Address");
1068 return;
1069 }
1070 if (prefixLength)
1071 {
1072 prefix = *prefixLength;
1073 }
1074 else if (staticGatewayEntry != staticGatewayData.end())
1075 {
1076 prefix = staticGatewayEntry->prefixLength;
1077 }
1078 else
1079 {
1080 messages::propertyMissing(asyncResp->res,
1081 pathString + "/PrefixLength");
1082 return;
1083 }
1084 if (staticGatewayEntry != staticGatewayData.end())
1085 {
1086 deleteAndCreateIPv6DefaultGateway(ifaceId, staticGatewayEntry->id,
1087 *addr, prefix, asyncResp);
1088 staticGatewayEntry++;
1089 }
1090 else
1091 {
1092 createIPv6DefaultGateway(ifaceId, prefix, *addr, asyncResp);
1093 }
1094 entryIdx++;
1095 }
1096}
1097
1098/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001099 * Function that retrieves all properties for given Ethernet Interface
1100 * Object
1101 * from EntityManager Network Manager
1102 * @param ethiface_id a eth interface id to query on DBus
1103 * @param callback a function that shall be called to convert Dbus output
1104 * into JSON
1105 */
1106template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +00001107void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001108 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001109{
George Liuf5892d02023-03-01 10:37:08 +08001110 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1111 dbus::utility::getManagedObjects(
1112 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001113 [ethifaceId{std::string{ethifaceId}},
Ed Tanous8cb2c022024-03-27 16:31:46 -07001114 callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001115 const boost::system::error_code& ec,
Ed Tanous3dfed532024-03-06 14:41:27 -08001116 const dbus::utility::ManagedObjectType& resp) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001117 EthernetInterfaceData ethData{};
Ed Tanous77179532023-02-28 10:45:28 -08001118 std::vector<IPv4AddressData> ipv4Data;
1119 std::vector<IPv6AddressData> ipv6Data;
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001120 std::vector<StaticGatewayData> ipv6GatewayData;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001121
Ed Tanous8b242752023-06-27 17:17:13 -07001122 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001124 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001125 return;
1126 }
1127
1128 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
1129 if (!found)
1130 {
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001131 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07001132 return;
1133 }
1134
1135 extractIPData(ethifaceId, resp, ipv4Data);
1136 // Fix global GW
1137 for (IPv4AddressData& ipv4 : ipv4Data)
1138 {
1139 if (((ipv4.linktype == LinkType::Global) &&
1140 (ipv4.gateway == "0.0.0.0")) ||
1141 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001142 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001143 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001144 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001145 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001146
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 extractIPV6Data(ethifaceId, resp, ipv6Data);
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001148 if (!extractIPv6DefaultGatewayData(ethifaceId, resp, ipv6GatewayData))
1149 {
1150 callback(false, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
1151 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001152 // Finally make a callback with useful data
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001153 callback(true, ethData, ipv4Data, ipv6Data, ipv6GatewayData);
Patrick Williams5a39f772023-10-20 11:20:21 -05001154 });
Ed Tanous271584a2019-07-09 16:24:22 -07001155}
Ed Tanous4a0cb852018-10-15 07:55:04 -07001156
1157/**
1158 * Function that retrieves all Ethernet Interfaces available through Network
1159 * Manager
1160 * @param callback a function that shall be called to convert Dbus output
1161 * into JSON.
1162 */
1163template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001164void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001165{
George Liuf5892d02023-03-01 10:37:08 +08001166 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1167 dbus::utility::getManagedObjects(
1168 "xyz.openbmc_project.Network", path,
Ed Tanous8cb2c022024-03-27 16:31:46 -07001169 [callback = std::forward<CallbackFunc>(callback)](
Ed Tanous8b242752023-06-27 17:17:13 -07001170 const boost::system::error_code& ec,
George Liuf5892d02023-03-01 10:37:08 +08001171 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 // Callback requires vector<string> to retrieve all available
1173 // ethernet interfaces
Ed Tanous77179532023-02-28 10:45:28 -08001174 std::vector<std::string> ifaceList;
Ed Tanous002d39b2022-05-31 08:59:27 -07001175 ifaceList.reserve(resp.size());
Ed Tanous8b242752023-06-27 17:17:13 -07001176 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001177 {
1178 callback(false, ifaceList);
1179 return;
1180 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001181
Ed Tanous002d39b2022-05-31 08:59:27 -07001182 // Iterate over all retrieved ObjectPaths.
1183 for (const auto& objpath : resp)
1184 {
1185 // And all interfaces available for certain ObjectPath.
1186 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001187 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001188 // If interface is
1189 // xyz.openbmc_project.Network.EthernetInterface, this is
1190 // what we're looking for.
1191 if (interface.first ==
1192 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -07001193 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001194 std::string ifaceId = objpath.first.filename();
1195 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001196 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001197 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001198 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001199 // and put it into output vector.
Ed Tanous77179532023-02-28 10:45:28 -08001200 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001201 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001202 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001203 }
Ed Tanous2c5875a2023-05-15 09:56:06 -07001204
Ed Tanous3544d2a2023-08-06 18:12:20 -07001205 std::ranges::sort(ifaceList, AlphanumLess<std::string>());
Ed Tanous2c5875a2023-05-15 09:56:06 -07001206
Ed Tanous002d39b2022-05-31 08:59:27 -07001207 // Finally make a callback with useful data
1208 callback(true, ifaceList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001209 });
Ed Tanous271584a2019-07-09 16:24:22 -07001210}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001211
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001212inline void
1213 handleHostnamePatch(const std::string& hostname,
1214 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001215{
Ed Tanousbf648f72021-06-03 15:00:14 -07001216 // SHOULD handle host names of up to 255 characters(RFC 1123)
1217 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001218 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001219 messages::propertyValueFormatError(asyncResp->res, hostname,
1220 "HostName");
1221 return;
1222 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001223 setDbusProperty(
1224 asyncResp, "xyz.openbmc_project.Network",
1225 sdbusplus::message::object_path("/xyz/openbmc_project/network/config"),
1226 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1227 "HostName", hostname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001228}
1229
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001230inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301231 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1232 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1233{
Ed Tanousd02aad32024-02-13 14:43:34 -08001234 sdbusplus::message::object_path objPath("/xyz/openbmc_project/network");
1235 objPath /= ifaceId;
1236 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", objPath,
1237 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1238 "MTUSize", mtuSize);
Tejas Patil35fb5312021-09-20 15:35:20 +05301239}
1240
1241inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001242 handleDomainnamePatch(const std::string& ifaceId,
1243 const std::string& domainname,
1244 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001245{
1246 std::vector<std::string> vectorDomainname = {domainname};
Ed Tanousd02aad32024-02-13 14:43:34 -08001247 setDbusProperty(
1248 asyncResp, "xyz.openbmc_project.Network",
1249 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1250 ifaceId,
1251 "xyz.openbmc_project.Network.EthernetInterface", "DomainName", "FQDN",
1252 vectorDomainname);
Ed Tanousbf648f72021-06-03 15:00:14 -07001253}
1254
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001255inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001256{
1257 // A valid host name can never have the dotted-decimal form (RFC 1123)
Ed Tanous3544d2a2023-08-06 18:12:20 -07001258 if (std::ranges::all_of(hostname, ::isdigit))
Ed Tanousbf648f72021-06-03 15:00:14 -07001259 {
1260 return false;
1261 }
1262 // Each label(hostname/subdomains) within a valid FQDN
1263 // MUST handle host names of up to 63 characters (RFC 1123)
1264 // labels cannot start or end with hyphens (RFC 952)
1265 // labels can start with numbers (RFC 1123)
Ed Tanous4b242742023-05-11 09:51:51 -07001266 const static std::regex pattern(
Ed Tanousbf648f72021-06-03 15:00:14 -07001267 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1268
1269 return std::regex_match(hostname, pattern);
1270}
1271
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001272inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001273{
1274 // Can have multiple subdomains
1275 // Top Level Domain's min length is 2 character
Ed Tanous4b242742023-05-11 09:51:51 -07001276 const static std::regex pattern(
George Liu0fda0f12021-11-16 10:06:17 +08001277 "^([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 -07001278
1279 return std::regex_match(domainname, pattern);
1280}
1281
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001282inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1283 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001284{
1285 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1286 if (fqdn.length() > 255)
1287 {
1288 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1289 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001290 }
1291
Ed Tanousbf648f72021-06-03 15:00:14 -07001292 size_t pos = fqdn.find('.');
1293 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001294 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001295 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1296 return;
1297 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001298
Ed Tanousbf648f72021-06-03 15:00:14 -07001299 std::string hostname;
1300 std::string domainname;
1301 domainname = (fqdn).substr(pos + 1);
1302 hostname = (fqdn).substr(0, pos);
1303
1304 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1305 {
1306 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1307 return;
1308 }
1309
1310 handleHostnamePatch(hostname, asyncResp);
1311 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1312}
1313
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001314inline void
1315 handleMACAddressPatch(const std::string& ifaceId,
1316 const std::string& macAddress,
1317 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001318{
Ed Tanousd02aad32024-02-13 14:43:34 -08001319 setDbusProperty(
1320 asyncResp, "xyz.openbmc_project.Network",
1321 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1322 ifaceId,
1323 "xyz.openbmc_project.Network.MACAddress", "MACAddress", "MACAddress",
1324 macAddress);
Ed Tanousbf648f72021-06-03 15:00:14 -07001325}
1326
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001327inline void setDHCPEnabled(const std::string& ifaceId,
1328 const std::string& propertyName, const bool v4Value,
1329 const bool v6Value,
1330 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001331{
1332 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
Ed Tanousd02aad32024-02-13 14:43:34 -08001333 setDbusProperty(
1334 asyncResp, "xyz.openbmc_project.Network",
1335 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1336 ifaceId,
1337 "xyz.openbmc_project.Network.EthernetInterface", propertyName, "DHCPv4",
1338 dhcp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001339}
1340
Jishnu CMe4588152023-05-11 00:04:40 -05001341enum class NetworkType
1342{
1343 dhcp4,
1344 dhcp6
1345};
1346
1347inline void setDHCPConfig(const std::string& propertyName, const bool& value,
1348 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1349 const std::string& ethifaceId, NetworkType type)
Ed Tanousbf648f72021-06-03 15:00:14 -07001350{
Ed Tanous62598e32023-07-17 17:06:25 -07001351 BMCWEB_LOG_DEBUG("{} = {}", propertyName, value);
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001352 std::string redfishPropertyName;
Jishnu CMe4588152023-05-11 00:04:40 -05001353 sdbusplus::message::object_path path("/xyz/openbmc_project/network/");
1354 path /= ethifaceId;
1355
1356 if (type == NetworkType::dhcp4)
1357 {
1358 path /= "dhcp4";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001359 redfishPropertyName = "DHCPv4";
Jishnu CMe4588152023-05-11 00:04:40 -05001360 }
1361 else
1362 {
1363 path /= "dhcp6";
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001364 redfishPropertyName = "DHCPv6";
Jishnu CMe4588152023-05-11 00:04:40 -05001365 }
1366
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001367 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1368 "xyz.openbmc_project.Network.DHCPConfiguration",
1369 propertyName, redfishPropertyName, value);
Ed Tanousbf648f72021-06-03 15:00:14 -07001370}
1371
Ravi Tejab10d8db2022-05-24 09:04:12 -05001372inline void handleSLAACAutoConfigPatch(
1373 const std::string& ifaceId, bool ipv6AutoConfigEnabled,
1374 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1375{
1376 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1377 path /= ifaceId;
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001378 setDbusProperty(asyncResp, "xyz.openbmc_project.Network", path,
1379 "xyz.openbmc_project.Network.EthernetInterface",
1380 "IPv6AcceptRA",
1381 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled",
1382 ipv6AutoConfigEnabled);
Ravi Tejab10d8db2022-05-24 09:04:12 -05001383}
1384
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001385inline void handleDHCPPatch(const std::string& ifaceId,
1386 const EthernetInterfaceData& ethData,
1387 const DHCPParameters& v4dhcpParms,
1388 const DHCPParameters& v6dhcpParms,
1389 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001390{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001391 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1392 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001393
1394 bool nextv4DHCPState =
1395 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1396
1397 bool nextv6DHCPState{};
1398 if (v6dhcpParms.dhcpv6OperatingMode)
1399 {
Ravi Tejab10d8db2022-05-24 09:04:12 -05001400 if ((*v6dhcpParms.dhcpv6OperatingMode != "Enabled") &&
Ed Tanousbf648f72021-06-03 15:00:14 -07001401 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1402 {
1403 messages::propertyValueFormatError(asyncResp->res,
1404 *v6dhcpParms.dhcpv6OperatingMode,
1405 "OperatingMode");
1406 return;
1407 }
Ravi Tejab10d8db2022-05-24 09:04:12 -05001408 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Enabled");
Ed Tanousbf648f72021-06-03 15:00:14 -07001409 }
1410 else
1411 {
1412 nextv6DHCPState = ipv6Active;
1413 }
1414
Jishnu CMe4588152023-05-11 00:04:40 -05001415 bool nextDNSv4 = ethData.dnsv4Enabled;
1416 bool nextDNSv6 = ethData.dnsv6Enabled;
1417 if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001418 {
Jishnu CMe4588152023-05-11 00:04:40 -05001419 nextDNSv4 = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001420 }
Jishnu CMe4588152023-05-11 00:04:40 -05001421 if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001422 {
Jishnu CMe4588152023-05-11 00:04:40 -05001423 nextDNSv6 = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001424 }
1425
Jishnu CMe4588152023-05-11 00:04:40 -05001426 bool nextNTPv4 = ethData.ntpv4Enabled;
1427 bool nextNTPv6 = ethData.ntpv6Enabled;
1428 if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001429 {
Jishnu CMe4588152023-05-11 00:04:40 -05001430 nextNTPv4 = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001431 }
Jishnu CMe4588152023-05-11 00:04:40 -05001432 if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001433 {
Jishnu CMe4588152023-05-11 00:04:40 -05001434 nextNTPv6 = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001435 }
1436
Ravi Teja91c441e2024-02-23 09:03:43 -06001437 bool nextUsev4Domain = ethData.domainv4Enabled;
1438 bool nextUsev6Domain = ethData.domainv6Enabled;
Jishnu CMe4588152023-05-11 00:04:40 -05001439 if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001440 {
Jishnu CMe4588152023-05-11 00:04:40 -05001441 nextUsev4Domain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001442 }
Jishnu CMe4588152023-05-11 00:04:40 -05001443 if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001444 {
Jishnu CMe4588152023-05-11 00:04:40 -05001445 nextUsev6Domain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001446 }
1447
Ed Tanous62598e32023-07-17 17:06:25 -07001448 BMCWEB_LOG_DEBUG("set DHCPEnabled...");
Ed Tanousbf648f72021-06-03 15:00:14 -07001449 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1450 asyncResp);
Ed Tanous62598e32023-07-17 17:06:25 -07001451 BMCWEB_LOG_DEBUG("set DNSEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001452 setDHCPConfig("DNSEnabled", nextDNSv4, asyncResp, ifaceId,
1453 NetworkType::dhcp4);
Ed Tanous62598e32023-07-17 17:06:25 -07001454 BMCWEB_LOG_DEBUG("set NTPEnabled...");
Jishnu CMe4588152023-05-11 00:04:40 -05001455 setDHCPConfig("NTPEnabled", nextNTPv4, asyncResp, ifaceId,
1456 NetworkType::dhcp4);
Ravi Teja91c441e2024-02-23 09:03:43 -06001457 BMCWEB_LOG_DEBUG("set DomainEnabled...");
1458 setDHCPConfig("DomainEnabled", nextUsev4Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001459 NetworkType::dhcp4);
1460 BMCWEB_LOG_DEBUG("set DNSEnabled for dhcp6...");
1461 setDHCPConfig("DNSEnabled", nextDNSv6, asyncResp, ifaceId,
1462 NetworkType::dhcp6);
1463 BMCWEB_LOG_DEBUG("set NTPEnabled for dhcp6...");
1464 setDHCPConfig("NTPEnabled", nextNTPv6, asyncResp, ifaceId,
1465 NetworkType::dhcp6);
Ravi Teja91c441e2024-02-23 09:03:43 -06001466 BMCWEB_LOG_DEBUG("set DomainEnabled for dhcp6...");
1467 setDHCPConfig("DomainEnabled", nextUsev6Domain, asyncResp, ifaceId,
Jishnu CMe4588152023-05-11 00:04:40 -05001468 NetworkType::dhcp6);
Ed Tanousbf648f72021-06-03 15:00:14 -07001469}
1470
Ed Tanous77179532023-02-28 10:45:28 -08001471inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1472 const std::vector<IPv4AddressData>::const_iterator& head,
1473 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001474{
1475 return std::find_if(head, end, [](const IPv4AddressData& value) {
1476 return value.origin == "Static";
1477 });
1478}
1479
Ed Tanous77179532023-02-28 10:45:28 -08001480inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1481 const std::vector<IPv6AddressData>::const_iterator& head,
1482 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001483{
1484 return std::find_if(head, end, [](const IPv6AddressData& value) {
1485 return value.origin == "Static";
1486 });
1487}
1488
Ed Tanous3dfed532024-03-06 14:41:27 -08001489inline void handleIPv4StaticPatch(
1490 const std::string& ifaceId,
1491 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
1492 const std::vector<IPv4AddressData>& ipv4Data,
1493 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001494{
Ed Tanousbf648f72021-06-03 15:00:14 -07001495 unsigned entryIdx = 1;
1496 // Find the first static IP address currently active on the NIC and
1497 // match it to the first JSON element in the IPv4StaticAddresses array.
1498 // Match each subsequent JSON element to the next static IP programmed
1499 // into the NIC.
Ed Tanous77179532023-02-28 10:45:28 -08001500 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001501 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1502
Ed Tanous3dfed532024-03-06 14:41:27 -08001503 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1504 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001505 {
Patrick Williams89492a12023-05-10 07:51:34 -05001506 std::string pathString = "IPv4StaticAddresses/" +
1507 std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001508 nlohmann::json::object_t* obj =
1509 std::get_if<nlohmann::json::object_t>(&thisJson);
1510 if (obj != nullptr && !obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001511 {
1512 std::optional<std::string> address;
1513 std::optional<std::string> subnetMask;
1514 std::optional<std::string> gateway;
1515
Ed Tanous3dfed532024-03-06 14:41:27 -08001516 if (!json_util::readJsonObject(*obj, asyncResp->res, "Address",
1517 address, "SubnetMask", subnetMask,
1518 "Gateway", gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001519 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001520 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001521 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001522 return;
1523 }
1524
1525 // Find the address/subnet/gateway values. Any values that are
1526 // not explicitly provided are assumed to be unmodified from the
1527 // current state of the interface. Merge existing state into the
1528 // current request.
Ed Tanousbf648f72021-06-03 15:00:14 -07001529 if (address)
1530 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001531 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001532 {
1533 messages::propertyValueFormatError(asyncResp->res, *address,
1534 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001535 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001536 }
1537 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001538 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001539 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001540 address = (nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001541 }
1542 else
1543 {
1544 messages::propertyMissing(asyncResp->res,
1545 pathString + "/Address");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001546 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001547 }
1548
Ed Tanouse01d0c32023-06-30 13:21:32 -07001549 uint8_t prefixLength = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001550 if (subnetMask)
1551 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001552 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1553 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001554 {
1555 messages::propertyValueFormatError(
1556 asyncResp->res, *subnetMask,
1557 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001558 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001559 }
1560 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001561 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001562 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001563 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1564 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001565 {
1566 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001567 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001568 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001569 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001570 }
1571 }
1572 else
1573 {
1574 messages::propertyMissing(asyncResp->res,
1575 pathString + "/SubnetMask");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001576 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001577 }
1578
1579 if (gateway)
1580 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001581 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001582 {
1583 messages::propertyValueFormatError(asyncResp->res, *gateway,
1584 pathString + "/Gateway");
Ed Tanouse01d0c32023-06-30 13:21:32 -07001585 return;
Ed Tanousbf648f72021-06-03 15:00:14 -07001586 }
1587 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001588 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001589 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001590 gateway = nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001591 }
1592 else
1593 {
1594 messages::propertyMissing(asyncResp->res,
1595 pathString + "/Gateway");
Ed Tanousbf648f72021-06-03 15:00:14 -07001596 return;
1597 }
1598
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001599 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001600 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001601 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
Ed Tanous77eb0152023-09-06 10:19:18 -07001602 nicIpEntry->id, prefixLength, *address,
1603 *gateway, asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001604 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1605 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001606 }
1607 else
1608 {
1609 createIPv4(ifaceId, prefixLength, *gateway, *address,
1610 asyncResp);
1611 }
1612 entryIdx++;
1613 }
1614 else
1615 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001616 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001617 {
1618 // Requesting a DELETE/DO NOT MODIFY action for an item
1619 // that isn't present on the eth(n) interface. Input JSON is
1620 // in error, so bail out.
Ed Tanous3dfed532024-03-06 14:41:27 -08001621 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001622 {
1623 messages::resourceCannotBeDeleted(asyncResp->res);
1624 return;
1625 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001626 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001627 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001628 return;
1629 }
1630
Ed Tanous3dfed532024-03-06 14:41:27 -08001631 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001632 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001633 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001634 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001635 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001636 {
Patrick Williams89492a12023-05-10 07:51:34 -05001637 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1638 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001639 }
1640 entryIdx++;
1641 }
1642 }
1643}
1644
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001645inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001646 const std::string& ifaceId,
1647 const std::vector<std::string>& updatedStaticNameServers,
1648 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1649{
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001650 setDbusProperty(
1651 asyncResp, "xyz.openbmc_project.Network",
1652 sdbusplus::message::object_path("/xyz/openbmc_project/network") /
1653 ifaceId,
George Liu9ae226f2023-06-21 17:56:46 +08001654 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Asmitha Karunanithi1847f2a2024-03-26 22:03:48 -05001655 "StaticNameServers", updatedStaticNameServers);
Ed Tanousbf648f72021-06-03 15:00:14 -07001656}
1657
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001658inline void handleIPv6StaticAddressesPatch(
Ed Tanous3dfed532024-03-06 14:41:27 -08001659 const std::string& ifaceId,
1660 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
Ed Tanous77179532023-02-28 10:45:28 -08001661 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001662 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1663{
Ed Tanousbf648f72021-06-03 15:00:14 -07001664 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001665 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001666 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanous3dfed532024-03-06 14:41:27 -08001667 for (std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson :
1668 input)
Ed Tanousbf648f72021-06-03 15:00:14 -07001669 {
Patrick Williams89492a12023-05-10 07:51:34 -05001670 std::string pathString = "IPv6StaticAddresses/" +
1671 std::to_string(entryIdx);
Ed Tanous3dfed532024-03-06 14:41:27 -08001672 nlohmann::json::object_t* obj =
1673 std::get_if<nlohmann::json::object_t>(&thisJson);
1674 if (obj != nullptr && !obj->empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001675 {
1676 std::optional<std::string> address;
1677 std::optional<uint8_t> prefixLength;
Ed Tanous3dfed532024-03-06 14:41:27 -08001678 nlohmann::json::object_t thisJsonCopy = *obj;
1679 if (!json_util::readJsonObject(thisJsonCopy, asyncResp->res,
1680 "Address", address, "PrefixLength",
1681 prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001682 {
Ed Tanous3dfed532024-03-06 14:41:27 -08001683 messages::propertyValueFormatError(asyncResp->res, thisJsonCopy,
Ed Tanousf818b042022-06-27 13:17:35 -07001684 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001685 return;
1686 }
1687
Ed Tanousbf648f72021-06-03 15:00:14 -07001688 // Find the address and prefixLength values. Any values that are
1689 // not explicitly provided are assumed to be unmodified from the
1690 // current state of the interface. Merge existing state into the
1691 // current request.
Ed Tanousd547d8d2024-03-16 18:04:41 -07001692 if (!address)
Ed Tanousbf648f72021-06-03 15:00:14 -07001693 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001694 if (nicIpEntry == ipv6Data.end())
1695 {
1696 messages::propertyMissing(asyncResp->res,
1697 pathString + "/Address");
1698 return;
1699 }
1700 address = nicIpEntry->address;
Ed Tanousbf648f72021-06-03 15:00:14 -07001701 }
1702
Ed Tanousd547d8d2024-03-16 18:04:41 -07001703 if (!prefixLength)
Ed Tanousbf648f72021-06-03 15:00:14 -07001704 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001705 if (nicIpEntry == ipv6Data.end())
1706 {
1707 messages::propertyMissing(asyncResp->res,
1708 pathString + "/PrefixLength");
1709 return;
1710 }
1711 prefixLength = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001712 }
1713
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001714 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001715 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001716 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
Ed Tanousd547d8d2024-03-16 18:04:41 -07001717 nicIpEntry->id, *prefixLength,
1718 *address, "", asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001719 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1720 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001721 }
1722 else
1723 {
Ed Tanousd547d8d2024-03-16 18:04:41 -07001724 createIPv6(ifaceId, *prefixLength, *address, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001725 }
1726 entryIdx++;
1727 }
1728 else
1729 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001730 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001731 {
1732 // Requesting a DELETE/DO NOT MODIFY action for an item
1733 // that isn't present on the eth(n) interface. Input JSON is
1734 // in error, so bail out.
Ed Tanous3dfed532024-03-06 14:41:27 -08001735 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001736 {
1737 messages::resourceCannotBeDeleted(asyncResp->res);
1738 return;
1739 }
Ed Tanous3dfed532024-03-06 14:41:27 -08001740 messages::propertyValueFormatError(asyncResp->res, *obj,
Ed Tanousf818b042022-06-27 13:17:35 -07001741 pathString);
Ed Tanousbf648f72021-06-03 15:00:14 -07001742 return;
1743 }
1744
Ed Tanous3dfed532024-03-06 14:41:27 -08001745 if (obj == nullptr)
Ed Tanousbf648f72021-06-03 15:00:14 -07001746 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001747 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001748 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001749 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001750 {
Patrick Williams89492a12023-05-10 07:51:34 -05001751 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1752 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001753 }
1754 entryIdx++;
1755 }
1756 }
1757}
1758
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001759inline std::string extractParentInterfaceName(const std::string& ifaceId)
1760{
1761 std::size_t pos = ifaceId.find('_');
1762 return ifaceId.substr(0, pos);
1763}
1764
Ed Tanous77179532023-02-28 10:45:28 -08001765inline void
1766 parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1767 const std::string& ifaceId,
1768 const EthernetInterfaceData& ethData,
1769 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001770 const std::vector<IPv6AddressData>& ipv6Data,
1771 const std::vector<StaticGatewayData>& ipv6GatewayData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001772{
Ed Tanousbf648f72021-06-03 15:00:14 -07001773 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1774 jsonResponse["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001775 jsonResponse["@odata.id"] = boost::urls::format(
1776 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001777 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1778
Ed Tanousbf648f72021-06-03 15:00:14 -07001779 if (ethData.nicEnabled)
1780 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001781 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001782 jsonResponse["Status"]["State"] = "Enabled";
1783 }
1784 else
1785 {
1786 jsonResponse["LinkStatus"] = "NoLink";
1787 jsonResponse["Status"]["State"] = "Disabled";
1788 }
1789
Ed Tanousbf648f72021-06-03 15:00:14 -07001790 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301791 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001792 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001793 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001794 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
Jishnu CMe4588152023-05-11 00:04:40 -05001795 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpv4Enabled;
1796 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsv4Enabled;
1797 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNamev4Enabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001798 jsonResponse["DHCPv6"]["OperatingMode"] =
Ravi Tejab10d8db2022-05-24 09:04:12 -05001799 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Enabled"
Ed Tanousbf648f72021-06-03 15:00:14 -07001800 : "Disabled";
Jishnu CMe4588152023-05-11 00:04:40 -05001801 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpv6Enabled;
1802 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsv6Enabled;
1803 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNamev6Enabled;
Ravi Tejab10d8db2022-05-24 09:04:12 -05001804 jsonResponse["StatelessAddressAutoConfig"]["IPv6AutoConfigEnabled"] =
1805 ethData.ipv6AcceptRa;
Ed Tanousbf648f72021-06-03 15:00:14 -07001806
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001807 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001808 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001809 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001810
1811 // When domain name is empty then it means, that it is a network
1812 // without domain names, and the host name itself must be treated as
1813 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001814 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001815 if (!ethData.domainnames.empty())
1816 {
1817 fqdn += "." + ethData.domainnames[0];
1818 }
1819 jsonResponse["FQDN"] = fqdn;
1820 }
1821
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08001822 if (ethData.vlanId)
1823 {
1824 jsonResponse["EthernetInterfaceType"] = "Virtual";
1825 jsonResponse["VLAN"]["VLANEnable"] = true;
1826 jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId;
1827 jsonResponse["VLAN"]["Tagged"] = true;
1828
1829 nlohmann::json::array_t relatedInterfaces;
1830 nlohmann::json& parentInterface = relatedInterfaces.emplace_back();
1831 parentInterface["@odata.id"] =
1832 boost::urls::format("/redfish/v1/Managers/bmc/EthernetInterfaces",
1833 extractParentInterfaceName(ifaceId));
1834 jsonResponse["Links"]["RelatedInterfaces"] =
1835 std::move(relatedInterfaces);
1836 }
1837 else
1838 {
1839 jsonResponse["EthernetInterfaceType"] = "Physical";
1840 }
1841
Ed Tanousbf648f72021-06-03 15:00:14 -07001842 jsonResponse["NameServers"] = ethData.nameServers;
1843 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1844
1845 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1846 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1847 ipv4Array = nlohmann::json::array();
1848 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001849 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001850 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001851 std::string gatewayStr = ipv4Config.gateway;
1852 if (gatewayStr.empty())
1853 {
1854 gatewayStr = "0.0.0.0";
1855 }
Ed Tanous14766872022-03-15 10:44:42 -07001856 nlohmann::json::object_t ipv4;
1857 ipv4["AddressOrigin"] = ipv4Config.origin;
1858 ipv4["SubnetMask"] = ipv4Config.netmask;
1859 ipv4["Address"] = ipv4Config.address;
1860 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001861
Ed Tanousbf648f72021-06-03 15:00:14 -07001862 if (ipv4Config.origin == "Static")
1863 {
Ed Tanous14766872022-03-15 10:44:42 -07001864 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001865 }
Ed Tanous14766872022-03-15 10:44:42 -07001866
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001867 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001868 }
1869
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001870 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001871 if (ipv6GatewayStr.empty())
1872 {
1873 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1874 }
1875
1876 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1877
Sunitha Harishce73d5c2023-04-07 06:46:49 -05001878 nlohmann::json::array_t ipv6StaticGatewayArray;
1879 for (const auto& ipv6GatewayConfig : ipv6GatewayData)
1880 {
1881 nlohmann::json::object_t ipv6Gateway;
1882 ipv6Gateway["Address"] = ipv6GatewayConfig.gateway;
1883 ipv6Gateway["PrefixLength"] = ipv6GatewayConfig.prefixLength;
1884 ipv6StaticGatewayArray.emplace_back(std::move(ipv6Gateway));
1885 }
1886 jsonResponse["IPv6StaticDefaultGateways"] =
1887 std::move(ipv6StaticGatewayArray);
1888
Ed Tanousbf648f72021-06-03 15:00:14 -07001889 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1890 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1891 ipv6Array = nlohmann::json::array();
1892 ipv6StaticArray = nlohmann::json::array();
1893 nlohmann::json& ipv6AddrPolicyTable =
1894 jsonResponse["IPv6AddressPolicyTable"];
1895 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001896 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001897 {
Ed Tanous14766872022-03-15 10:44:42 -07001898 nlohmann::json::object_t ipv6;
1899 ipv6["Address"] = ipv6Config.address;
1900 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1901 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05001902
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001903 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001904 if (ipv6Config.origin == "Static")
1905 {
Ed Tanous14766872022-03-15 10:44:42 -07001906 nlohmann::json::object_t ipv6Static;
1907 ipv6Static["Address"] = ipv6Config.address;
1908 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001909 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001910 }
1911 }
1912}
1913
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08001914inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1915 const std::string& ifaceId,
1916 const boost::system::error_code& ec,
1917 const sdbusplus::message_t& m)
1918{
1919 if (!ec)
1920 {
1921 return;
1922 }
1923 const sd_bus_error* dbusError = m.get_error();
1924 if (dbusError == nullptr)
1925 {
1926 messages::internalError(asyncResp->res);
1927 return;
1928 }
Ed Tanous62598e32023-07-17 17:06:25 -07001929 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08001930
1931 if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") ==
1932 dbusError->name)
1933 {
1934 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1935 ifaceId);
1936 return;
1937 }
1938 if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") ==
1939 dbusError->name)
1940 {
1941 messages::resourceCannotBeDeleted(asyncResp->res);
1942 return;
1943 }
1944 messages::internalError(asyncResp->res);
1945}
1946
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08001947inline void afterVlanCreate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1948 const std::string& parentInterfaceUri,
1949 const std::string& vlanInterface,
1950 const boost::system::error_code& ec,
1951 const sdbusplus::message_t& m
1952
1953)
1954{
1955 if (ec)
1956 {
1957 const sd_bus_error* dbusError = m.get_error();
1958 if (dbusError == nullptr)
1959 {
1960 messages::internalError(asyncResp->res);
1961 return;
1962 }
Ed Tanous62598e32023-07-17 17:06:25 -07001963 BMCWEB_LOG_DEBUG("DBus error: {}", dbusError->name);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08001964
1965 if (std::string_view(
1966 "xyz.openbmc_project.Common.Error.ResourceNotFound") ==
1967 dbusError->name)
1968 {
1969 messages::propertyValueNotInList(
1970 asyncResp->res, parentInterfaceUri,
1971 "Links/RelatedInterfaces/0/@odata.id");
1972 return;
1973 }
1974 if (std::string_view(
1975 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
1976 dbusError->name)
1977 {
1978 messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface",
1979 "Id", vlanInterface);
1980 return;
1981 }
1982 messages::internalError(asyncResp->res);
1983 return;
1984 }
1985
1986 const boost::urls::url vlanInterfaceUri = boost::urls::format(
1987 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", vlanInterface);
1988 asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer());
1989}
1990
Ed Tanousbf648f72021-06-03 15:00:14 -07001991inline void requestEthernetInterfacesRoutes(App& app)
1992{
1993 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001994 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001995 .methods(boost::beast::http::verb::get)(
1996 [&app](const crow::Request& req,
1997 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001998 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001999 {
2000 return;
2001 }
2002
2003 asyncResp->res.jsonValue["@odata.type"] =
2004 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
2005 asyncResp->res.jsonValue["@odata.id"] =
2006 "/redfish/v1/Managers/bmc/EthernetInterfaces";
2007 asyncResp->res.jsonValue["Name"] =
2008 "Ethernet Network Interface Collection";
2009 asyncResp->res.jsonValue["Description"] =
2010 "Collection of EthernetInterfaces for this Manager";
2011
2012 // Get eth interface list, and call the below callback for JSON
2013 // preparation
2014 getEthernetIfaceList(
Ed Tanous77179532023-02-28 10:45:28 -08002015 [asyncResp](const bool& success,
2016 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002017 if (!success)
2018 {
2019 messages::internalError(asyncResp->res);
2020 return;
2021 }
2022
2023 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
2024 ifaceArray = nlohmann::json::array();
Ed Tanous002d39b2022-05-31 08:59:27 -07002025 for (const std::string& ifaceItem : ifaceList)
2026 {
Jiaqing Zhao7857cb82023-03-03 11:23:08 +08002027 nlohmann::json::object_t iface;
2028 iface["@odata.id"] = boost::urls::format(
2029 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
2030 ifaceItem);
2031 ifaceArray.push_back(std::move(iface));
Ed Tanous002d39b2022-05-31 08:59:27 -07002032 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002033
Ed Tanous002d39b2022-05-31 08:59:27 -07002034 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2035 asyncResp->res.jsonValue["@odata.id"] =
2036 "/redfish/v1/Managers/bmc/EthernetInterfaces";
2037 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002038 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002039
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002040 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
2041 .privileges(redfish::privileges::postEthernetInterfaceCollection)
2042 .methods(boost::beast::http::verb::post)(
2043 [&app](const crow::Request& req,
2044 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2045 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2046 {
2047 return;
2048 }
2049
2050 bool vlanEnable = false;
2051 uint32_t vlanId = 0;
Ed Tanous3dfed532024-03-06 14:41:27 -08002052 std::vector<nlohmann::json::object_t> relatedInterfaces;
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002053
2054 if (!json_util::readJsonPatch(req, asyncResp->res, "VLAN/VLANEnable",
2055 vlanEnable, "VLAN/VLANId", vlanId,
2056 "Links/RelatedInterfaces",
2057 relatedInterfaces))
2058 {
2059 return;
2060 }
2061
2062 if (relatedInterfaces.size() != 1)
2063 {
2064 messages::arraySizeTooLong(asyncResp->res,
2065 "Links/RelatedInterfaces",
2066 relatedInterfaces.size());
2067 return;
2068 }
2069
2070 std::string parentInterfaceUri;
Ed Tanous3dfed532024-03-06 14:41:27 -08002071 if (!json_util::readJsonObject(relatedInterfaces[0], asyncResp->res,
2072 "@odata.id", parentInterfaceUri))
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002073 {
2074 messages::propertyMissing(asyncResp->res,
2075 "Links/RelatedInterfaces/0/@odata.id");
2076 return;
2077 }
Ed Tanous62598e32023-07-17 17:06:25 -07002078 BMCWEB_LOG_INFO("Parent Interface URI: {}", parentInterfaceUri);
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002079
Ed Tanous6fd29552023-10-04 09:40:14 -07002080 boost::system::result<boost::urls::url_view> parsedUri =
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002081 boost::urls::parse_relative_ref(parentInterfaceUri);
2082 if (!parsedUri)
2083 {
2084 messages::propertyValueFormatError(
2085 asyncResp->res, parentInterfaceUri,
2086 "Links/RelatedInterfaces/0/@odata.id");
2087 return;
2088 }
2089
2090 std::string parentInterface;
2091 if (!crow::utility::readUrlSegments(
2092 *parsedUri, "redfish", "v1", "Managers", "bmc",
2093 "EthernetInterfaces", std::ref(parentInterface)))
2094 {
2095 messages::propertyValueNotInList(
2096 asyncResp->res, parentInterfaceUri,
2097 "Links/RelatedInterfaces/0/@odata.id");
2098 return;
2099 }
2100
2101 if (!vlanEnable)
2102 {
2103 // In OpenBMC implementation, VLANEnable cannot be false on
2104 // create
2105 messages::propertyValueIncorrect(asyncResp->res, "VLAN/VLANEnable",
2106 "false");
2107 return;
2108 }
2109
2110 std::string vlanInterface = parentInterface + "_" +
2111 std::to_string(vlanId);
2112 crow::connections::systemBus->async_method_call(
2113 [asyncResp, parentInterfaceUri,
2114 vlanInterface](const boost::system::error_code& ec,
2115 const sdbusplus::message_t& m) {
2116 afterVlanCreate(asyncResp, parentInterfaceUri, vlanInterface, ec,
2117 m);
Patrick Williams5a39f772023-10-20 11:20:21 -05002118 },
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002119 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
2120 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", parentInterface,
2121 vlanId);
Patrick Williams5a39f772023-10-20 11:20:21 -05002122 });
Jiaqing Zhaob5ca3fd2023-03-08 15:14:58 +08002123
Ed Tanousbf648f72021-06-03 15:00:14 -07002124 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002125 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002126 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002127 [&app](const crow::Request& req,
2128 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2129 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002130 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 {
2132 return;
2133 }
2134 getEthernetIfaceData(
2135 ifaceId,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002136 [asyncResp,
2137 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2138 const std::vector<IPv4AddressData>& ipv4Data,
2139 const std::vector<IPv6AddressData>& ipv6Data,
2140 const std::vector<StaticGatewayData>& ipv6GatewayData) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002141 if (!success)
2142 {
2143 // TODO(Pawel)consider distinguish between non
2144 // existing object, and other errors
2145 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
2146 ifaceId);
2147 return;
2148 }
Johnathan Mantey01784822019-06-18 12:44:21 -07002149
Ed Tanous002d39b2022-05-31 08:59:27 -07002150 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao93bbc952023-02-23 14:11:20 +08002151 "#EthernetInterface.v1_9_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07002152 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
2153 asyncResp->res.jsonValue["Description"] =
2154 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05302155
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002156 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data,
2157 ipv6GatewayData);
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002159 });
Johnathan Mantey01784822019-06-18 12:44:21 -07002160
Ed Tanousbf648f72021-06-03 15:00:14 -07002161 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002162 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002163 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002164 [&app](const crow::Request& req,
2165 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2166 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002167 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002168 {
2169 return;
2170 }
2171 std::optional<std::string> hostname;
2172 std::optional<std::string> fqdn;
2173 std::optional<std::string> macAddress;
2174 std::optional<std::string> ipv6DefaultGateway;
Ed Tanous3dfed532024-03-06 14:41:27 -08002175 std::optional<
2176 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2177 ipv4StaticAddresses;
2178 std::optional<
2179 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2180 ipv6StaticAddresses;
2181 std::optional<
2182 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
2183 ipv6StaticDefaultGateways;
Ed Tanous002d39b2022-05-31 08:59:27 -07002184 std::optional<std::vector<std::string>> staticNameServers;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002185 std::optional<bool> ipv6AutoConfigEnabled;
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 std::optional<bool> interfaceEnabled;
2187 std::optional<size_t> mtuSize;
2188 DHCPParameters v4dhcpParms;
2189 DHCPParameters v6dhcpParms;
Ravi Tejab10d8db2022-05-24 09:04:12 -05002190 // clang-format off
Ed Tanous3dfed532024-03-06 14:41:27 -08002191 if (!json_util::readJsonPatch(req, asyncResp->res,
2192 "DHCPv4/DHCPEnabled", v4dhcpParms.dhcpv4Enabled,
2193 "DHCPv4/UseDNSServers", v4dhcpParms.useDnsServers,
2194 "DHCPv4/UseDomainName", v4dhcpParms.useDomainName,
2195 "DHCPv4/UseNTPServers", v4dhcpParms.useNtpServers,
2196 "DHCPv6/OperatingMode", v6dhcpParms.dhcpv6OperatingMode,
2197 "DHCPv6/UseDNSServers", v6dhcpParms.useDnsServers,
2198 "DHCPv6/UseDomainName", v6dhcpParms.useDomainName,
2199 "DHCPv6/UseNTPServers", v6dhcpParms.useNtpServers,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002200 "FQDN", fqdn,
2201 "HostName", hostname,
2202 "IPv4StaticAddresses", ipv4StaticAddresses,
2203 "IPv6DefaultGateway", ipv6DefaultGateway,
2204 "IPv6StaticAddresses", ipv6StaticAddresses,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002205 "IPv6StaticDefaultGateways", ipv6StaticDefaultGateways,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002206 "InterfaceEnabled", interfaceEnabled,
2207 "MACAddress", macAddress,
2208 "MTUSize", mtuSize,
2209 "StatelessAddressAutoConfig/IPv6AutoConfigEnabled", ipv6AutoConfigEnabled,
2210 "StaticNameServers", staticNameServers
2211 )
2212 )
Ed Tanous002d39b2022-05-31 08:59:27 -07002213 {
2214 return;
2215 }
Ed Tanous5a8b6412024-03-27 11:30:54 -07002216 // clang-format on
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002217
Ed Tanous002d39b2022-05-31 08:59:27 -07002218 // Get single eth interface data, and call the below callback
2219 // for JSON preparation
2220 getEthernetIfaceData(
2221 ifaceId,
2222 [asyncResp, ifaceId, hostname = std::move(hostname),
2223 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2224 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2225 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2226 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002227 ipv6StaticDefaultGateway = std::move(ipv6StaticDefaultGateways),
Ed Tanous3dfed532024-03-06 14:41:27 -08002228 staticNameServers = std::move(staticNameServers), mtuSize,
Ravi Tejab10d8db2022-05-24 09:04:12 -05002229 ipv6AutoConfigEnabled, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
Ed Tanous3dfed532024-03-06 14:41:27 -08002231 const bool success, const EthernetInterfaceData& ethData,
Ed Tanous77179532023-02-28 10:45:28 -08002232 const std::vector<IPv4AddressData>& ipv4Data,
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002233 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanous3dfed532024-03-06 14:41:27 -08002234 const std::vector<StaticGatewayData>& ipv6GatewayData) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07002235 if (!success)
2236 {
2237 // ... otherwise return error
2238 // TODO(Pawel)consider distinguish between non
2239 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002240 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 ifaceId);
2242 return;
2243 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002244
Ed Tanous3dfed532024-03-06 14:41:27 -08002245 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
2246 asyncResp);
Tejas Patil35fb5312021-09-20 15:35:20 +05302247
Ed Tanous002d39b2022-05-31 08:59:27 -07002248 if (hostname)
2249 {
2250 handleHostnamePatch(*hostname, asyncResp);
2251 }
2252
Ravi Tejab10d8db2022-05-24 09:04:12 -05002253 if (ipv6AutoConfigEnabled)
2254 {
2255 handleSLAACAutoConfigPatch(ifaceId, *ipv6AutoConfigEnabled,
2256 asyncResp);
2257 }
2258
Ed Tanous002d39b2022-05-31 08:59:27 -07002259 if (fqdn)
2260 {
2261 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2262 }
2263
2264 if (macAddress)
2265 {
2266 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2267 }
2268
2269 if (ipv4StaticAddresses)
2270 {
Ed Tanous3dfed532024-03-06 14:41:27 -08002271 handleIPv4StaticPatch(ifaceId, *ipv4StaticAddresses, ipv4Data,
2272 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 }
2274
2275 if (staticNameServers)
2276 {
2277 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2278 asyncResp);
2279 }
2280
2281 if (ipv6DefaultGateway)
2282 {
2283 messages::propertyNotWritable(asyncResp->res,
2284 "IPv6DefaultGateway");
2285 }
2286
2287 if (ipv6StaticAddresses)
2288 {
Ed Tanousddd70dc2023-03-01 16:00:27 -08002289 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
2290 ipv6Data, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002291 }
2292
Sunitha Harishce73d5c2023-04-07 06:46:49 -05002293 if (ipv6StaticDefaultGateway)
2294 {
2295 handleIPv6DefaultGateway(ifaceId, *ipv6StaticDefaultGateway,
2296 ipv6GatewayData, asyncResp);
2297 }
2298
Ed Tanous002d39b2022-05-31 08:59:27 -07002299 if (interfaceEnabled)
2300 {
Ed Tanousd02aad32024-02-13 14:43:34 -08002301 setDbusProperty(asyncResp, "xyz.openbmc_project.Network",
2302 sdbusplus::message::object_path(
2303 "/xyz/openbmc_project/network") /
2304 ifaceId,
2305 "xyz.openbmc_project.Network.EthernetInterface",
2306 "NICEnabled", "InterfaceEnabled",
2307 *interfaceEnabled);
Ed Tanous002d39b2022-05-31 08:59:27 -07002308 }
2309
2310 if (mtuSize)
2311 {
2312 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2313 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002314 });
Ed Tanous5a8b6412024-03-27 11:30:54 -07002315 });
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002316
2317 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
2318 .privileges(redfish::privileges::deleteEthernetInterface)
2319 .methods(boost::beast::http::verb::delete_)(
2320 [&app](const crow::Request& req,
2321 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2322 const std::string& ifaceId) {
2323 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2324 {
2325 return;
2326 }
2327
2328 crow::connections::systemBus->async_method_call(
2329 [asyncResp, ifaceId](const boost::system::error_code& ec,
2330 const sdbusplus::message_t& m) {
2331 afterDelete(asyncResp, ifaceId, ec, m);
Ed Tanous5a8b6412024-03-27 11:30:54 -07002332 },
Jiaqing Zhaoe7caf252023-03-09 11:14:44 +08002333 "xyz.openbmc_project.Network",
2334 std::string("/xyz/openbmc_project/network/") + ifaceId,
2335 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous5a8b6412024-03-27 11:30:54 -07002336 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002337}
2338
Ed Tanous1abe55e2018-09-05 08:30:59 -07002339} // namespace redfish