blob: bdc2a6515212d55ec4d72251ee9afb4cd5ebcdf9 [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
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070019#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020020#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080021#include <dbus_utility.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020022#include <error_messages.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070023#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050025#include <utils/json_utils.hpp>
26
Ed Tanousa24526d2018-12-10 15:17:59 -080027#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053028#include <regex>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030namespace redfish
31{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010032
Ed Tanous4a0cb852018-10-15 07:55:04 -070033enum class LinkType
34{
35 Local,
36 Global
37};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010038
39/**
40 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010041 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070042struct IPv4AddressData
43{
44 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070045 std::string address;
46 std::string domain;
47 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 std::string netmask;
49 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070050 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050051 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070052
Gunnar Mills1214b7e2020-06-04 10:11:30 -050053 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070055 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010057};
58
59/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050060 * Structure for keeping IPv6 data required by Redfish
61 */
62struct IPv6AddressData
63{
64 std::string id;
65 std::string address;
66 std::string origin;
67 uint8_t prefixLength;
68
Gunnar Mills1214b7e2020-06-04 10:11:30 -050069 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050070 {
71 return id < obj.id;
72 }
73};
74/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010075 * Structure for keeping basic single Ethernet Interface information
76 * available from DBus
77 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070078struct EthernetInterfaceData
79{
Ed Tanous4a0cb852018-10-15 07:55:04 -070080 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053081 size_t mtuSize;
Ed Tanous4a0cb852018-10-15 07:55:04 -070082 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070083 bool DNSEnabled;
84 bool NTPEnabled;
85 bool HostNameEnabled;
86 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080087 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070088 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070089 std::string DHCPEnabled;
90 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -070091 std::string hostname;
92 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -050093 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -070094 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -050095 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -050096 std::vector<std::string> nameServers;
97 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -080098 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010099};
100
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700101struct DHCPParameters
102{
103 std::optional<bool> dhcpv4Enabled;
104 std::optional<bool> useDNSServers;
105 std::optional<bool> useNTPServers;
106 std::optional<bool> useUseDomainName;
107 std::optional<std::string> dhcpv6OperatingMode;
108};
109
Ed Tanous4a0cb852018-10-15 07:55:04 -0700110// Helper function that changes bits netmask notation (i.e. /24)
111// into full dot notation
112inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700114 uint32_t value = 0xffffffff << (32 - bits);
115 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
116 std::to_string((value >> 16) & 0xff) + "." +
117 std::to_string((value >> 8) & 0xff) + "." +
118 std::to_string(value & 0xff);
119 return netmask;
120}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100121
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500122inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700123 bool isIPv4)
124{
125 if (isIPv4)
126 {
127 return (
128 (inputDHCP ==
129 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
130 (inputDHCP ==
131 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
132 }
133 return ((inputDHCP ==
134 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
135 (inputDHCP ==
136 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
137}
138
Ed Tanous2c70f802020-09-28 14:29:23 -0700139inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700140{
141 if (isIPv4 && isIPv6)
142 {
143 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
144 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700145 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700146 {
147 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
148 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700149 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700150 {
151 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
152 }
153 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
154}
155
Ed Tanous4a0cb852018-10-15 07:55:04 -0700156inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500157 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700158 bool isIPv4)
159{
160 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700162 return "Static";
163 }
164 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
165 {
166 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700167 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700168 return "IPv4LinkLocal";
169 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700170 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700171 }
172 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
173 {
174 if (isIPv4)
175 {
176 return "DHCP";
177 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700178 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700179 }
180 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
181 {
182 return "SLAAC";
183 }
184 return "";
185}
186
Ed Tanous711ac7a2021-12-20 09:34:41 -0800187inline bool
188 extractEthernetInterfaceData(const std::string& ethifaceId,
189 dbus::utility::ManagedObjectType& dbusData,
190 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700191{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700192 bool idFound = false;
Ed Tanous81ce6092020-12-17 16:54:55 +0000193 for (auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700194 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700195 for (auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700196 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000197 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700199 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700200 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500202 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700204 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700205 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500206 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800207 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700208 if (mac != nullptr)
209 {
210 ethData.mac_address = *mac;
211 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700212 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700213 }
214 }
215 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
216 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500217 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700218 {
219 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500221 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800222 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 if (id != nullptr)
224 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500225 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700226 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700228 }
229 }
230 else if (ifacePair.first ==
231 "xyz.openbmc_project.Network.EthernetInterface")
232 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500233 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700234 {
235 if (propertyPair.first == "AutoNeg")
236 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700237 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800238 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700239 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700240 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700241 ethData.auto_neg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700242 }
243 }
244 else if (propertyPair.first == "Speed")
245 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500246 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800247 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700248 if (speed != nullptr)
249 {
250 ethData.speed = *speed;
251 }
252 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530253 else if (propertyPair.first == "MTU")
254 {
255 const uint32_t* mtuSize =
256 std::get_if<uint32_t>(&propertyPair.second);
257 if (mtuSize != nullptr)
258 {
259 ethData.mtuSize = *mtuSize;
260 }
261 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800262 else if (propertyPair.first == "LinkUp")
263 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500264 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800265 std::get_if<bool>(&propertyPair.second);
266 if (linkUp != nullptr)
267 {
268 ethData.linkUp = *linkUp;
269 }
270 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700271 else if (propertyPair.first == "NICEnabled")
272 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500273 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700274 std::get_if<bool>(&propertyPair.second);
275 if (nicEnabled != nullptr)
276 {
277 ethData.nicEnabled = *nicEnabled;
278 }
279 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500280 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700281 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500282 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500283 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800284 &propertyPair.second);
285 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700286 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700287 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500288 }
289 }
290 else if (propertyPair.first == "StaticNameServers")
291 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500292 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500293 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500294 &propertyPair.second);
295 if (staticNameServers != nullptr)
296 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700297 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700298 }
299 }
manojkiraneda2a133282019-02-19 13:09:43 +0530300 else if (propertyPair.first == "DHCPEnabled")
301 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700302 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700303 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700304 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530305 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700306 ethData.DHCPEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530307 }
308 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800309 else if (propertyPair.first == "DomainName")
310 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500311 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500312 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800313 &propertyPair.second);
314 if (domainNames != nullptr)
315 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700316 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800317 }
318 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500319 else if (propertyPair.first == "DefaultGateway")
320 {
321 const std::string* defaultGateway =
322 std::get_if<std::string>(&propertyPair.second);
323 if (defaultGateway != nullptr)
324 {
325 std::string defaultGatewayStr = *defaultGateway;
326 if (defaultGatewayStr.empty())
327 {
328 ethData.default_gateway = "0.0.0.0";
329 }
330 else
331 {
332 ethData.default_gateway = defaultGatewayStr;
333 }
334 }
335 }
336 else if (propertyPair.first == "DefaultGateway6")
337 {
338 const std::string* defaultGateway6 =
339 std::get_if<std::string>(&propertyPair.second);
340 if (defaultGateway6 != nullptr)
341 {
342 std::string defaultGateway6Str =
343 *defaultGateway6;
344 if (defaultGateway6Str.empty())
345 {
346 ethData.ipv6_default_gateway =
347 "0:0:0:0:0:0:0:0";
348 }
349 else
350 {
351 ethData.ipv6_default_gateway =
352 defaultGateway6Str;
353 }
354 }
355 }
Ed Tanous029573d2019-02-01 10:57:49 -0800356 }
357 }
358 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700359
360 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
361 {
362 if (ifacePair.first ==
363 "xyz.openbmc_project.Network.DHCPConfiguration")
364 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500365 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700366 {
367 if (propertyPair.first == "DNSEnabled")
368 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700369 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700370 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700371 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700372 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700373 ethData.DNSEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700374 }
375 }
376 else if (propertyPair.first == "NTPEnabled")
377 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700378 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700379 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700380 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700381 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700382 ethData.NTPEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700383 }
384 }
385 else if (propertyPair.first == "HostNameEnabled")
386 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700387 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700388 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700389 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700390 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700391 ethData.HostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700392 }
393 }
394 else if (propertyPair.first == "SendHostNameEnabled")
395 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700396 const bool* sendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700397 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700398 if (sendHostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700399 {
400 ethData.SendHostNameEnabled =
Ed Tanous2c70f802020-09-28 14:29:23 -0700401 *sendHostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700402 }
403 }
404 }
405 }
406 }
Ed Tanous029573d2019-02-01 10:57:49 -0800407 // System configuration shows up in the global namespace, so no need
408 // to check eth number
409 if (ifacePair.first ==
410 "xyz.openbmc_project.Network.SystemConfiguration")
411 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500412 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800413 {
414 if (propertyPair.first == "HostName")
415 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500416 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500417 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800418 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700419 {
Ed Tanous029573d2019-02-01 10:57:49 -0800420 ethData.hostname = *hostname;
421 }
422 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 }
424 }
425 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700426 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700427 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700428}
429
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500430// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700431inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000432 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800433 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000434 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500435{
436 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000437 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500438
439 // Since there might be several IPv6 configurations aligned with
440 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000441 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500442 {
443 // Check if proper pattern for object path appears
444 if (boost::starts_with(objpath.first.str, ipv6PathStart))
445 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800446 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500447 {
448 if (interface.first == "xyz.openbmc_project.Network.IP")
449 {
450 // Instance IPv6AddressData structure, and set as
451 // appropriate
452 std::pair<
453 boost::container::flat_set<IPv6AddressData>::iterator,
454 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000455 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700456 IPv6AddressData& ipv6Address = *it.first;
457 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700458 objpath.first.str.substr(ipv6PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800459 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500460 {
461 if (property.first == "Address")
462 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500463 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500464 std::get_if<std::string>(&property.second);
465 if (address != nullptr)
466 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700467 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500468 }
469 }
470 else if (property.first == "Origin")
471 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500472 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500473 std::get_if<std::string>(&property.second);
474 if (origin != nullptr)
475 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700476 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500477 translateAddressOriginDbusToRedfish(*origin,
478 false);
479 }
480 }
481 else if (property.first == "PrefixLength")
482 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500483 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500484 std::get_if<uint8_t>(&property.second);
485 if (prefix != nullptr)
486 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700487 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500488 }
489 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600490 else if (property.first == "Type" ||
491 property.first == "Gateway")
492 {
493 // Type & Gateway is not used
494 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500495 else
496 {
497 BMCWEB_LOG_ERROR
498 << "Got extra property: " << property.first
499 << " on the " << objpath.first.str << " object";
500 }
501 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500502 }
503 }
504 }
505 }
506}
507
Ed Tanous4a0cb852018-10-15 07:55:04 -0700508// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700509inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000510 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800511 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000512 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700513{
514 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000515 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700516
517 // Since there might be several IPv4 configurations aligned with
518 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000519 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700520 {
521 // Check if proper pattern for object path appears
522 if (boost::starts_with(objpath.first.str, ipv4PathStart))
523 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800524 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700525 {
526 if (interface.first == "xyz.openbmc_project.Network.IP")
527 {
528 // Instance IPv4AddressData structure, and set as
529 // appropriate
530 std::pair<
531 boost::container::flat_set<IPv4AddressData>::iterator,
532 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000533 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700534 IPv4AddressData& ipv4Address = *it.first;
535 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700536 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800537 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700538 {
539 if (property.first == "Address")
540 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500541 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800542 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700543 if (address != nullptr)
544 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700545 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700546 }
547 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700548 else if (property.first == "Origin")
549 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500550 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800551 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700552 if (origin != nullptr)
553 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700554 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700555 translateAddressOriginDbusToRedfish(*origin,
556 true);
557 }
558 }
559 else if (property.first == "PrefixLength")
560 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500561 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800562 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700563 if (mask != nullptr)
564 {
565 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700566 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700567 }
568 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600569 else if (property.first == "Type" ||
570 property.first == "Gateway")
571 {
572 // Type & Gateway is not used
573 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700574 else
575 {
576 BMCWEB_LOG_ERROR
577 << "Got extra property: " << property.first
578 << " on the " << objpath.first.str << " object";
579 }
580 }
581 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700582 ipv4Address.linktype =
583 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700584 ? LinkType::Local
585 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700586 }
587 }
588 }
589 }
590}
591
592/**
593 * @brief Sets given Id on the given VLAN interface through D-Bus
594 *
595 * @param[in] ifaceId Id of VLAN interface that should be modified
596 * @param[in] inputVlanId New ID of the VLAN
597 * @param[in] callback Function that will be called after the operation
598 *
599 * @return None.
600 */
601template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500602void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
603 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604{
605 crow::connections::systemBus->async_method_call(
606 callback, "xyz.openbmc_project.Network",
607 std::string("/xyz/openbmc_project/network/") + ifaceId,
608 "org.freedesktop.DBus.Properties", "Set",
609 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanous168e20c2021-12-13 14:39:53 -0800610 dbus::utility::DbusVariantType(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700611}
612
613/**
614 * @brief Helper function that verifies IP address to check if it is in
615 * proper format. If bits pointer is provided, also calculates active
616 * bit count for Subnet Mask.
617 *
618 * @param[in] ip IP that will be verified
619 * @param[out] bits Calculated mask in bits notation
620 *
621 * @return true in case of success, false otherwise
622 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500623inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
624 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700625{
626 std::vector<std::string> bytesInMask;
627
628 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629
630 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700631 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700633 return false;
634 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700635
Ed Tanous4a0cb852018-10-15 07:55:04 -0700636 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 *bits = 0;
639 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640
Ed Tanous543f4402022-01-06 13:12:53 -0800641 char* endPtr = nullptr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700642 long previousValue = 255;
Ed Tanous543f4402022-01-06 13:12:53 -0800643 bool firstZeroInByteHit = false;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500644 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700645 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700646 if (byte.empty())
647 {
648 return false;
649 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650
Ed Tanous4a0cb852018-10-15 07:55:04 -0700651 // Use strtol instead of stroi to avoid exceptions
652 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653
Ed Tanous4a0cb852018-10-15 07:55:04 -0700654 // endPtr should point to the end of the string, otherwise given string
655 // is not 100% number
656 if (*endPtr != '\0')
657 {
658 return false;
659 }
660
661 // Value should be contained in byte
662 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700663 {
664 return false;
665 }
666
667 if (bits != nullptr)
668 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700669 // Mask has to be continuous between bytes
670 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700671 {
672 return false;
673 }
674
Ed Tanous4a0cb852018-10-15 07:55:04 -0700675 // Mask has to be continuous inside bytes
676 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677
Ed Tanous4a0cb852018-10-15 07:55:04 -0700678 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000679 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700680 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800681 if ((value & (1L << bitIdx)) != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700682 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700683 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700685 // Continuity not preserved
686 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700688 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700690 else
691 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700692 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700694 }
695 }
696
697 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700698 }
699
Ed Tanous4a0cb852018-10-15 07:55:04 -0700700 return true;
701}
702
703/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700704 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700705 *
706 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700707 * @param[in] ipHash DBus Hash id of IP that should be deleted
708 * @param[io] asyncResp Response object that will be returned to client
709 *
710 * @return None
711 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500712inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800713 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700714{
715 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700716 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700717 if (ec)
718 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800719 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100720 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700721 },
722 "xyz.openbmc_project.Network",
723 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
724 "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{
731 crow::connections::systemBus->async_method_call(
732 [asyncResp](const boost::system::error_code ec) {
733 if (ec)
734 {
735 messages::internalError(asyncResp->res);
736 return;
737 }
738 asyncResp->res.result(boost::beast::http::status::no_content);
739 },
740 "xyz.openbmc_project.Network",
741 "/xyz/openbmc_project/network/" + ifaceId,
742 "org.freedesktop.DBus.Properties", "Set",
743 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800744 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500745}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700746/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700747 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700748 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700749 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
750 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
751 * @param[in] gateway IPv4 address of this interfaces gateway
752 * @param[in] address IPv4 address to assign to this interface
753 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700754 *
755 * @return None
756 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000757inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
758 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800759 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700760{
Ravi Teja9010ec22019-08-01 23:30:25 -0500761 auto createIpHandler = [asyncResp, ifaceId,
762 gateway](const boost::system::error_code ec) {
763 if (ec)
764 {
765 messages::internalError(asyncResp->res);
766 return;
767 }
768 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
769 };
770
Ed Tanous4a0cb852018-10-15 07:55:04 -0700771 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500772 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700773 "/xyz/openbmc_project/network/" + ifaceId,
774 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700775 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700776 gateway);
777}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500778
779/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700780 * @brief Deletes the IPv4 entry for this interface and creates a replacement
781 * static IPv4 entry
782 *
783 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
784 * @param[in] id The unique hash entry identifying the DBus entry
785 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
786 * @param[in] gateway IPv4 address of this interfaces gateway
787 * @param[in] address IPv4 address to assign to this interface
788 * @param[io] asyncResp Response object that will be returned to client
789 *
790 * @return None
791 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800792inline void
793 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
794 uint8_t prefixLength, const std::string& gateway,
795 const std::string& address,
796 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700797{
798 crow::connections::systemBus->async_method_call(
799 [asyncResp, ifaceId, address, prefixLength,
800 gateway](const boost::system::error_code ec) {
801 if (ec)
802 {
803 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500804 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700805 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500806
Johnathan Mantey01784822019-06-18 12:44:21 -0700807 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500808 [asyncResp, ifaceId,
809 gateway](const boost::system::error_code ec2) {
Ed Tanous23a21a12020-07-25 04:45:05 +0000810 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700811 {
812 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500813 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700814 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500815 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -0700816 },
817 "xyz.openbmc_project.Network",
818 "/xyz/openbmc_project/network/" + ifaceId,
819 "xyz.openbmc_project.Network.IP.Create", "IP",
820 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
821 prefixLength, gateway);
822 },
823 "xyz.openbmc_project.Network",
824 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
825 "xyz.openbmc_project.Object.Delete", "Delete");
826}
827
828/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500829 * @brief Deletes given IPv6
830 *
831 * @param[in] ifaceId Id of interface whose IP should be deleted
832 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500833 * @param[io] asyncResp Response object that will be returned to client
834 *
835 * @return None
836 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500837inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800838 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500839{
840 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700841 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500842 if (ec)
843 {
844 messages::internalError(asyncResp->res);
845 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500846 },
847 "xyz.openbmc_project.Network",
848 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
849 "xyz.openbmc_project.Object.Delete", "Delete");
850}
851
852/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700853 * @brief Deletes the IPv6 entry for this interface and creates a replacement
854 * static IPv6 entry
855 *
856 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
857 * @param[in] id The unique hash entry identifying the DBus entry
858 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
859 * @param[in] address IPv6 address to assign to this interface
860 * @param[io] asyncResp Response object that will be returned to client
861 *
862 * @return None
863 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800864inline void
865 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
866 uint8_t prefixLength, const std::string& address,
867 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700868{
869 crow::connections::systemBus->async_method_call(
870 [asyncResp, ifaceId, address,
871 prefixLength](const boost::system::error_code ec) {
872 if (ec)
873 {
874 messages::internalError(asyncResp->res);
875 }
876 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000877 [asyncResp](const boost::system::error_code ec2) {
878 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700879 {
880 messages::internalError(asyncResp->res);
881 }
882 },
883 "xyz.openbmc_project.Network",
884 "/xyz/openbmc_project/network/" + ifaceId,
885 "xyz.openbmc_project.Network.IP.Create", "IP",
886 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
887 prefixLength, "");
888 },
889 "xyz.openbmc_project.Network",
890 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
891 "xyz.openbmc_project.Object.Delete", "Delete");
892}
893
894/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500895 * @brief Creates IPv6 with given data
896 *
897 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500898 * @param[in] prefixLength Prefix length that needs to be added
899 * @param[in] address IP address that needs to be added
900 * @param[io] asyncResp Response object that will be returned to client
901 *
902 * @return None
903 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500904inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
905 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800906 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500907{
908 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
909 if (ec)
910 {
911 messages::internalError(asyncResp->res);
912 }
913 };
914 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500915 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500916 crow::connections::systemBus->async_method_call(
917 std::move(createIpHandler), "xyz.openbmc_project.Network",
918 "/xyz/openbmc_project/network/" + ifaceId,
919 "xyz.openbmc_project.Network.IP.Create", "IP",
920 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
921 "");
922}
923
Ed Tanous4a0cb852018-10-15 07:55:04 -0700924/**
925 * Function that retrieves all properties for given Ethernet Interface
926 * Object
927 * from EntityManager Network Manager
928 * @param ethiface_id a eth interface id to query on DBus
929 * @param callback a function that shall be called to convert Dbus output
930 * into JSON
931 */
932template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000933void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500934 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700935{
936 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800937 [ethifaceId{std::string{ethifaceId}},
938 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000939 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800940 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700941 EthernetInterfaceData ethData{};
942 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500943 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700944
Ed Tanous81ce6092020-12-17 16:54:55 +0000945 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700946 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700947 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700948 return;
949 }
950
Ed Tanous4c9afe42019-05-03 16:59:57 -0700951 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700952 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700953 if (!found)
954 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700955 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700956 return;
957 }
958
Ed Tanous2c70f802020-09-28 14:29:23 -0700959 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700960 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500961 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 {
Ravi Tejac6191412019-07-30 00:53:50 -0500963 if (((ipv4.linktype == LinkType::Global) &&
964 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500965 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700966 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700967 ipv4.gateway = ethData.default_gateway;
968 }
969 }
970
Ed Tanous2c70f802020-09-28 14:29:23 -0700971 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500972 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700973 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700974 },
975 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
976 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700977}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700978
979/**
980 * Function that retrieves all Ethernet Interfaces available through Network
981 * Manager
982 * @param callback a function that shall be called to convert Dbus output
983 * into JSON.
984 */
985template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500986void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700987{
988 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800989 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000990 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800991 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700992 // Callback requires vector<string> to retrieve all available
993 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -0700994 boost::container::flat_set<std::string> ifaceList;
995 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +0000996 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700997 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700998 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700999 return;
1000 }
1001
1002 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001003 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001004 {
1005 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001006 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001007 {
1008 // If interface is
1009 // xyz.openbmc_project.Network.EthernetInterface, this is
1010 // what we're looking for.
1011 if (interface.first ==
1012 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001013 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001014 std::string ifaceId = objpath.first.filename();
1015 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001016 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001017 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001018 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001019 // and put it into output vector.
1020 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001021 }
1022 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001023 }
1024 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -07001025 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001026 },
1027 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1028 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001029}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001030
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001031inline void
1032 handleHostnamePatch(const std::string& hostname,
1033 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034{
Ed Tanousbf648f72021-06-03 15:00:14 -07001035 // SHOULD handle host names of up to 255 characters(RFC 1123)
1036 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001037 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001038 messages::propertyValueFormatError(asyncResp->res, hostname,
1039 "HostName");
1040 return;
1041 }
1042 crow::connections::systemBus->async_method_call(
1043 [asyncResp](const boost::system::error_code ec) {
1044 if (ec)
1045 {
1046 messages::internalError(asyncResp->res);
1047 }
1048 },
1049 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1050 "org.freedesktop.DBus.Properties", "Set",
1051 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001052 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001053}
1054
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001055inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301056 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1057 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1058{
1059 sdbusplus::message::object_path objPath =
1060 "/xyz/openbmc_project/network/" + ifaceId;
1061 crow::connections::systemBus->async_method_call(
1062 [asyncResp](const boost::system::error_code ec) {
1063 if (ec)
1064 {
1065 messages::internalError(asyncResp->res);
1066 }
1067 },
1068 "xyz.openbmc_project.Network", objPath,
1069 "org.freedesktop.DBus.Properties", "Set",
1070 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1071 std::variant<size_t>(mtuSize));
1072}
1073
1074inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001075 handleDomainnamePatch(const std::string& ifaceId,
1076 const std::string& domainname,
1077 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001078{
1079 std::vector<std::string> vectorDomainname = {domainname};
1080 crow::connections::systemBus->async_method_call(
1081 [asyncResp](const boost::system::error_code ec) {
1082 if (ec)
1083 {
1084 messages::internalError(asyncResp->res);
1085 }
1086 },
1087 "xyz.openbmc_project.Network",
1088 "/xyz/openbmc_project/network/" + ifaceId,
1089 "org.freedesktop.DBus.Properties", "Set",
1090 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001091 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001092}
1093
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001094inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001095{
1096 // A valid host name can never have the dotted-decimal form (RFC 1123)
1097 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1098 {
1099 return false;
1100 }
1101 // Each label(hostname/subdomains) within a valid FQDN
1102 // MUST handle host names of up to 63 characters (RFC 1123)
1103 // labels cannot start or end with hyphens (RFC 952)
1104 // labels can start with numbers (RFC 1123)
1105 const std::regex pattern(
1106 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1107
1108 return std::regex_match(hostname, pattern);
1109}
1110
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001111inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001112{
1113 // Can have multiple subdomains
1114 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001115 const std::regex pattern(
1116 "^([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 -07001117
1118 return std::regex_match(domainname, pattern);
1119}
1120
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001121inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1122 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001123{
1124 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1125 if (fqdn.length() > 255)
1126 {
1127 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1128 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001129 }
1130
Ed Tanousbf648f72021-06-03 15:00:14 -07001131 size_t pos = fqdn.find('.');
1132 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001133 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001134 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1135 return;
1136 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001137
Ed Tanousbf648f72021-06-03 15:00:14 -07001138 std::string hostname;
1139 std::string domainname;
1140 domainname = (fqdn).substr(pos + 1);
1141 hostname = (fqdn).substr(0, pos);
1142
1143 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1144 {
1145 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1146 return;
1147 }
1148
1149 handleHostnamePatch(hostname, asyncResp);
1150 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1151}
1152
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001153inline void
1154 handleMACAddressPatch(const std::string& ifaceId,
1155 const std::string& macAddress,
1156 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001157{
1158 crow::connections::systemBus->async_method_call(
1159 [asyncResp, macAddress](const boost::system::error_code ec) {
1160 if (ec)
1161 {
1162 messages::internalError(asyncResp->res);
1163 return;
1164 }
1165 },
1166 "xyz.openbmc_project.Network",
1167 "/xyz/openbmc_project/network/" + ifaceId,
1168 "org.freedesktop.DBus.Properties", "Set",
1169 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001170 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001171}
1172
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001173inline void setDHCPEnabled(const std::string& ifaceId,
1174 const std::string& propertyName, const bool v4Value,
1175 const bool v6Value,
1176 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001177{
1178 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1179 crow::connections::systemBus->async_method_call(
1180 [asyncResp](const boost::system::error_code ec) {
1181 if (ec)
1182 {
1183 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1184 messages::internalError(asyncResp->res);
1185 return;
1186 }
1187 messages::success(asyncResp->res);
1188 },
1189 "xyz.openbmc_project.Network",
1190 "/xyz/openbmc_project/network/" + ifaceId,
1191 "org.freedesktop.DBus.Properties", "Set",
1192 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001193 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001194}
1195
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001196inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001197 const std::string& ifaceId, const std::string& propertyName,
1198 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1199{
1200 crow::connections::systemBus->async_method_call(
1201 [asyncResp](const boost::system::error_code ec) {
1202 if (ec)
1203 {
1204 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1205 messages::internalError(asyncResp->res);
1206 return;
1207 }
1208 },
1209 "xyz.openbmc_project.Network",
1210 "/xyz/openbmc_project/network/" + ifaceId,
1211 "org.freedesktop.DBus.Properties", "Set",
1212 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001213 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001214}
1215
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001216inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1217 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001218{
1219 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1220 crow::connections::systemBus->async_method_call(
1221 [asyncResp](const boost::system::error_code ec) {
1222 if (ec)
1223 {
1224 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1225 messages::internalError(asyncResp->res);
1226 return;
1227 }
1228 },
1229 "xyz.openbmc_project.Network",
1230 "/xyz/openbmc_project/network/config/dhcp",
1231 "org.freedesktop.DBus.Properties", "Set",
1232 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001233 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001234}
1235
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001236inline void handleDHCPPatch(const std::string& ifaceId,
1237 const EthernetInterfaceData& ethData,
1238 const DHCPParameters& v4dhcpParms,
1239 const DHCPParameters& v6dhcpParms,
1240 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001241{
1242 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1243 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1244
1245 bool nextv4DHCPState =
1246 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1247
1248 bool nextv6DHCPState{};
1249 if (v6dhcpParms.dhcpv6OperatingMode)
1250 {
1251 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1252 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1253 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1254 {
1255 messages::propertyValueFormatError(asyncResp->res,
1256 *v6dhcpParms.dhcpv6OperatingMode,
1257 "OperatingMode");
1258 return;
1259 }
1260 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1261 }
1262 else
1263 {
1264 nextv6DHCPState = ipv6Active;
1265 }
1266
1267 bool nextDNS{};
1268 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1269 {
1270 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1271 {
1272 messages::generalError(asyncResp->res);
1273 return;
1274 }
1275 nextDNS = *v4dhcpParms.useDNSServers;
1276 }
1277 else if (v4dhcpParms.useDNSServers)
1278 {
1279 nextDNS = *v4dhcpParms.useDNSServers;
1280 }
1281 else if (v6dhcpParms.useDNSServers)
1282 {
1283 nextDNS = *v6dhcpParms.useDNSServers;
1284 }
1285 else
1286 {
1287 nextDNS = ethData.DNSEnabled;
1288 }
1289
1290 bool nextNTP{};
1291 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1292 {
1293 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1294 {
1295 messages::generalError(asyncResp->res);
1296 return;
1297 }
1298 nextNTP = *v4dhcpParms.useNTPServers;
1299 }
1300 else if (v4dhcpParms.useNTPServers)
1301 {
1302 nextNTP = *v4dhcpParms.useNTPServers;
1303 }
1304 else if (v6dhcpParms.useNTPServers)
1305 {
1306 nextNTP = *v6dhcpParms.useNTPServers;
1307 }
1308 else
1309 {
1310 nextNTP = ethData.NTPEnabled;
1311 }
1312
1313 bool nextUseDomain{};
1314 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1315 {
1316 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1317 {
1318 messages::generalError(asyncResp->res);
1319 return;
1320 }
1321 nextUseDomain = *v4dhcpParms.useUseDomainName;
1322 }
1323 else if (v4dhcpParms.useUseDomainName)
1324 {
1325 nextUseDomain = *v4dhcpParms.useUseDomainName;
1326 }
1327 else if (v6dhcpParms.useUseDomainName)
1328 {
1329 nextUseDomain = *v6dhcpParms.useUseDomainName;
1330 }
1331 else
1332 {
1333 nextUseDomain = ethData.HostNameEnabled;
1334 }
1335
1336 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1337 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1338 asyncResp);
1339 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1340 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1341 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1342 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1343 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1344 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1345}
1346
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001347inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001348 getNextStaticIpEntry(
1349 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1350 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1351{
1352 return std::find_if(head, end, [](const IPv4AddressData& value) {
1353 return value.origin == "Static";
1354 });
1355}
1356
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001357inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001358 getNextStaticIpEntry(
1359 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1360 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1361{
1362 return std::find_if(head, end, [](const IPv6AddressData& value) {
1363 return value.origin == "Static";
1364 });
1365}
1366
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001367inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001368 const std::string& ifaceId, nlohmann::json& input,
1369 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1370 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1371{
1372 if ((!input.is_array()) || input.empty())
1373 {
1374 messages::propertyValueTypeError(
1375 asyncResp->res,
1376 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1377 "IPv4StaticAddresses");
1378 return;
1379 }
1380
1381 unsigned entryIdx = 1;
1382 // Find the first static IP address currently active on the NIC and
1383 // match it to the first JSON element in the IPv4StaticAddresses array.
1384 // Match each subsequent JSON element to the next static IP programmed
1385 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001386 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001387 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1388
1389 for (nlohmann::json& thisJson : input)
1390 {
1391 std::string pathString =
1392 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1393
1394 if (!thisJson.is_null() && !thisJson.empty())
1395 {
1396 std::optional<std::string> address;
1397 std::optional<std::string> subnetMask;
1398 std::optional<std::string> gateway;
1399
1400 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1401 address, "SubnetMask", subnetMask,
1402 "Gateway", gateway))
1403 {
1404 messages::propertyValueFormatError(
1405 asyncResp->res,
1406 thisJson.dump(2, ' ', true,
1407 nlohmann::json::error_handler_t::replace),
1408 pathString);
1409 return;
1410 }
1411
1412 // Find the address/subnet/gateway values. Any values that are
1413 // not explicitly provided are assumed to be unmodified from the
1414 // current state of the interface. Merge existing state into the
1415 // current request.
1416 const std::string* addr = nullptr;
1417 const std::string* gw = nullptr;
1418 uint8_t prefixLength = 0;
1419 bool errorInEntry = false;
1420 if (address)
1421 {
1422 if (ipv4VerifyIpAndGetBitcount(*address))
1423 {
1424 addr = &(*address);
1425 }
1426 else
1427 {
1428 messages::propertyValueFormatError(asyncResp->res, *address,
1429 pathString + "/Address");
1430 errorInEntry = true;
1431 }
1432 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001433 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001434 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001435 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001436 }
1437 else
1438 {
1439 messages::propertyMissing(asyncResp->res,
1440 pathString + "/Address");
1441 errorInEntry = true;
1442 }
1443
1444 if (subnetMask)
1445 {
1446 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1447 {
1448 messages::propertyValueFormatError(
1449 asyncResp->res, *subnetMask,
1450 pathString + "/SubnetMask");
1451 errorInEntry = true;
1452 }
1453 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001454 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001455 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001456 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001457 &prefixLength))
1458 {
1459 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001460 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001461 pathString + "/SubnetMask");
1462 errorInEntry = true;
1463 }
1464 }
1465 else
1466 {
1467 messages::propertyMissing(asyncResp->res,
1468 pathString + "/SubnetMask");
1469 errorInEntry = true;
1470 }
1471
1472 if (gateway)
1473 {
1474 if (ipv4VerifyIpAndGetBitcount(*gateway))
1475 {
1476 gw = &(*gateway);
1477 }
1478 else
1479 {
1480 messages::propertyValueFormatError(asyncResp->res, *gateway,
1481 pathString + "/Gateway");
1482 errorInEntry = true;
1483 }
1484 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001485 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001486 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001487 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001488 }
1489 else
1490 {
1491 messages::propertyMissing(asyncResp->res,
1492 pathString + "/Gateway");
1493 errorInEntry = true;
1494 }
1495
1496 if (errorInEntry)
1497 {
1498 return;
1499 }
1500
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001501 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001502 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001503 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001504 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001505 nicIpEntry =
1506 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001507 }
1508 else
1509 {
1510 createIPv4(ifaceId, prefixLength, *gateway, *address,
1511 asyncResp);
1512 }
1513 entryIdx++;
1514 }
1515 else
1516 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001517 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001518 {
1519 // Requesting a DELETE/DO NOT MODIFY action for an item
1520 // that isn't present on the eth(n) interface. Input JSON is
1521 // in error, so bail out.
1522 if (thisJson.is_null())
1523 {
1524 messages::resourceCannotBeDeleted(asyncResp->res);
1525 return;
1526 }
1527 messages::propertyValueFormatError(
1528 asyncResp->res,
1529 thisJson.dump(2, ' ', true,
1530 nlohmann::json::error_handler_t::replace),
1531 pathString);
1532 return;
1533 }
1534
1535 if (thisJson.is_null())
1536 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001537 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001538 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001539 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001540 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001541 nicIpEntry =
1542 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001543 }
1544 entryIdx++;
1545 }
1546 }
1547}
1548
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001549inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001550 const std::string& ifaceId,
1551 const std::vector<std::string>& updatedStaticNameServers,
1552 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1553{
1554 crow::connections::systemBus->async_method_call(
1555 [asyncResp](const boost::system::error_code ec) {
1556 if (ec)
1557 {
1558 messages::internalError(asyncResp->res);
1559 return;
1560 }
1561 },
1562 "xyz.openbmc_project.Network",
1563 "/xyz/openbmc_project/network/" + ifaceId,
1564 "org.freedesktop.DBus.Properties", "Set",
1565 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001566 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001567}
1568
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001569inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001570 const std::string& ifaceId, const nlohmann::json& input,
1571 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1572 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1573{
1574 if (!input.is_array() || input.empty())
1575 {
1576 messages::propertyValueTypeError(
1577 asyncResp->res,
1578 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1579 "IPv6StaticAddresses");
1580 return;
1581 }
1582 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001583 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001584 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1585 for (const nlohmann::json& thisJson : input)
1586 {
1587 std::string pathString =
1588 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1589
1590 if (!thisJson.is_null() && !thisJson.empty())
1591 {
1592 std::optional<std::string> address;
1593 std::optional<uint8_t> prefixLength;
1594 nlohmann::json thisJsonCopy = thisJson;
1595 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1596 address, "PrefixLength", prefixLength))
1597 {
1598 messages::propertyValueFormatError(
1599 asyncResp->res,
1600 thisJson.dump(2, ' ', true,
1601 nlohmann::json::error_handler_t::replace),
1602 pathString);
1603 return;
1604 }
1605
Ed Tanous543f4402022-01-06 13:12:53 -08001606 const std::string* addr = nullptr;
1607 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001608
1609 // Find the address and prefixLength values. Any values that are
1610 // not explicitly provided are assumed to be unmodified from the
1611 // current state of the interface. Merge existing state into the
1612 // current request.
1613 if (address)
1614 {
1615 addr = &(*address);
1616 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001617 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001618 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001619 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001620 }
1621 else
1622 {
1623 messages::propertyMissing(asyncResp->res,
1624 pathString + "/Address");
1625 return;
1626 }
1627
1628 if (prefixLength)
1629 {
1630 prefix = *prefixLength;
1631 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001632 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001633 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001634 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001635 }
1636 else
1637 {
1638 messages::propertyMissing(asyncResp->res,
1639 pathString + "/PrefixLength");
1640 return;
1641 }
1642
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001643 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001644 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001645 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001646 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001647 nicIpEntry =
1648 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001649 }
1650 else
1651 {
1652 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1653 }
1654 entryIdx++;
1655 }
1656 else
1657 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001658 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001659 {
1660 // Requesting a DELETE/DO NOT MODIFY action for an item
1661 // that isn't present on the eth(n) interface. Input JSON is
1662 // in error, so bail out.
1663 if (thisJson.is_null())
1664 {
1665 messages::resourceCannotBeDeleted(asyncResp->res);
1666 return;
1667 }
1668 messages::propertyValueFormatError(
1669 asyncResp->res,
1670 thisJson.dump(2, ' ', true,
1671 nlohmann::json::error_handler_t::replace),
1672 pathString);
1673 return;
1674 }
1675
1676 if (thisJson.is_null())
1677 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001678 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001679 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001680 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001681 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001682 nicIpEntry =
1683 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001684 }
1685 entryIdx++;
1686 }
1687 }
1688}
1689
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001690inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001691 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1692 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1693 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1694 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1695{
1696 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1697 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1698
1699 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1700 jsonResponse["Id"] = ifaceId;
1701 jsonResponse["@odata.id"] =
1702 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1703 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1704
1705 auto health = std::make_shared<HealthPopulate>(asyncResp);
1706
1707 crow::connections::systemBus->async_method_call(
1708 [health](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001709 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanousbf648f72021-06-03 15:00:14 -07001710 if (ec)
1711 {
1712 return;
1713 }
1714
Ed Tanous914e2d52022-01-07 11:38:34 -08001715 health->inventory = resp;
Ed Tanousbf648f72021-06-03 15:00:14 -07001716 },
1717 "xyz.openbmc_project.ObjectMapper",
1718 "/xyz/openbmc_project/object_mapper",
1719 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1720 inventoryForEthernet);
1721
1722 health->populate();
1723
1724 if (ethData.nicEnabled)
1725 {
1726 jsonResponse["LinkStatus"] = "LinkUp";
1727 jsonResponse["Status"]["State"] = "Enabled";
1728 }
1729 else
1730 {
1731 jsonResponse["LinkStatus"] = "NoLink";
1732 jsonResponse["Status"]["State"] = "Disabled";
1733 }
1734
1735 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1736 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301737 jsonResponse["MTUSize"] = ethData.mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001738 jsonResponse["MACAddress"] = ethData.mac_address;
1739 jsonResponse["DHCPv4"]["DHCPEnabled"] =
1740 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1741 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1742 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1743 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1744
1745 jsonResponse["DHCPv6"]["OperatingMode"] =
1746 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1747 : "Disabled";
1748 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1749 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1750 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1751
1752 if (!ethData.hostname.empty())
1753 {
1754 jsonResponse["HostName"] = ethData.hostname;
1755
1756 // When domain name is empty then it means, that it is a network
1757 // without domain names, and the host name itself must be treated as
1758 // FQDN
1759 std::string fqdn = ethData.hostname;
1760 if (!ethData.domainnames.empty())
1761 {
1762 fqdn += "." + ethData.domainnames[0];
1763 }
1764 jsonResponse["FQDN"] = fqdn;
1765 }
1766
1767 jsonResponse["VLANs"] = {
1768 {"@odata.id",
1769 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1770
1771 jsonResponse["NameServers"] = ethData.nameServers;
1772 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1773
1774 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1775 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1776 ipv4Array = nlohmann::json::array();
1777 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001778 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001779 {
1780
1781 std::string gatewayStr = ipv4Config.gateway;
1782 if (gatewayStr.empty())
1783 {
1784 gatewayStr = "0.0.0.0";
1785 }
1786
1787 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1788 {"SubnetMask", ipv4Config.netmask},
1789 {"Address", ipv4Config.address},
1790 {"Gateway", gatewayStr}});
1791 if (ipv4Config.origin == "Static")
1792 {
1793 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1794 {"SubnetMask", ipv4Config.netmask},
1795 {"Address", ipv4Config.address},
1796 {"Gateway", gatewayStr}});
1797 }
1798 }
1799
1800 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1801 if (ipv6GatewayStr.empty())
1802 {
1803 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1804 }
1805
1806 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1807
1808 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1809 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1810 ipv6Array = nlohmann::json::array();
1811 ipv6StaticArray = nlohmann::json::array();
1812 nlohmann::json& ipv6AddrPolicyTable =
1813 jsonResponse["IPv6AddressPolicyTable"];
1814 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001815 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001816 {
1817 ipv6Array.push_back({{"Address", ipv6Config.address},
1818 {"PrefixLength", ipv6Config.prefixLength},
1819 {"AddressOrigin", ipv6Config.origin},
1820 {"AddressState", nullptr}});
1821 if (ipv6Config.origin == "Static")
1822 {
1823 ipv6StaticArray.push_back(
1824 {{"Address", ipv6Config.address},
Jiaqing Zhao7a474a52021-12-03 17:44:36 +08001825 {"PrefixLength", ipv6Config.prefixLength}});
Ed Tanousbf648f72021-06-03 15:00:14 -07001826 }
1827 }
1828}
1829
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001830inline void parseInterfaceData(nlohmann::json& jsonResponse,
1831 const std::string& parentIfaceId,
1832 const std::string& ifaceId,
1833 const EthernetInterfaceData& ethData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001834{
1835 // Fill out obvious data...
1836 jsonResponse["Id"] = ifaceId;
1837 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1838 parentIfaceId + "/VLANs/" + ifaceId;
1839
1840 jsonResponse["VLANEnable"] = true;
1841 if (!ethData.vlan_id.empty())
1842 {
1843 jsonResponse["VLANId"] = ethData.vlan_id.back();
1844 }
1845}
1846
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001847inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001848{
Ed Tanousdcf2ebc2022-01-25 10:07:45 -08001849 return boost::starts_with(iface, parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001850}
1851
1852inline void requestEthernetInterfacesRoutes(App& app)
1853{
1854 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001855 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001856 .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1857 const std::shared_ptr<
1858 bmcweb::AsyncResp>&
1859 asyncResp) {
1860 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1861 {
1862 return;
1863 }
1864
Ed Tanousbf648f72021-06-03 15:00:14 -07001865 asyncResp->res.jsonValue["@odata.type"] =
1866 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1867 asyncResp->res.jsonValue["@odata.id"] =
1868 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1869 asyncResp->res.jsonValue["Name"] =
1870 "Ethernet Network Interface Collection";
1871 asyncResp->res.jsonValue["Description"] =
1872 "Collection of EthernetInterfaces for this Manager";
1873
1874 // Get eth interface list, and call the below callback for JSON
1875 // preparation
1876 getEthernetIfaceList([asyncResp](const bool& success,
1877 const boost::container::flat_set<
1878 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001879 if (!success)
1880 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001881 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001882 return;
1883 }
1884
Ed Tanous2c70f802020-09-28 14:29:23 -07001885 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001886 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001887 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001888 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001889 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001890 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001891 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001892 if (found == std::string::npos)
1893 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001894 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001895 {{"@odata.id",
1896 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001897 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001898 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001899 }
1900
Ed Tanous4c9afe42019-05-03 16:59:57 -07001901 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001902 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001903 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001904 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001905 });
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301906 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001907
Ed Tanousbf648f72021-06-03 15:00:14 -07001908 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001909 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001910 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001911 [&app](const crow::Request& req,
1912 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1913 const std::string& ifaceId) {
1914 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1915 {
1916 return;
1917 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001918 getEthernetIfaceData(
1919 ifaceId,
1920 [asyncResp,
1921 ifaceId](const bool& success,
1922 const EthernetInterfaceData& ethData,
1923 const boost::container::flat_set<IPv4AddressData>&
1924 ipv4Data,
1925 const boost::container::flat_set<IPv6AddressData>&
1926 ipv6Data) {
1927 if (!success)
1928 {
1929 // TODO(Pawel)consider distinguish between non
1930 // existing object, and other errors
1931 messages::resourceNotFound(
1932 asyncResp->res, "EthernetInterface", ifaceId);
1933 return;
1934 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001935
Ed Tanousbf648f72021-06-03 15:00:14 -07001936 asyncResp->res.jsonValue["@odata.type"] =
1937 "#EthernetInterface.v1_4_1.EthernetInterface";
1938 asyncResp->res.jsonValue["Name"] =
1939 "Manager Ethernet Interface";
1940 asyncResp->res.jsonValue["Description"] =
1941 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301942
Ed Tanousbf648f72021-06-03 15:00:14 -07001943 parseInterfaceData(asyncResp, ifaceId, ethData,
1944 ipv4Data, ipv6Data);
1945 });
1946 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001947
Ed Tanousbf648f72021-06-03 15:00:14 -07001948 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001949 .privileges(redfish::privileges::patchEthernetInterface)
1950
Ed Tanousbf648f72021-06-03 15:00:14 -07001951 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001952 [&app](const crow::Request& req,
1953 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1954 const std::string& ifaceId) {
1955 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1956 {
1957 return;
1958 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001959 std::optional<std::string> hostname;
1960 std::optional<std::string> fqdn;
1961 std::optional<std::string> macAddress;
1962 std::optional<std::string> ipv6DefaultGateway;
1963 std::optional<nlohmann::json> ipv4StaticAddresses;
1964 std::optional<nlohmann::json> ipv6StaticAddresses;
1965 std::optional<std::vector<std::string>> staticNameServers;
1966 std::optional<nlohmann::json> dhcpv4;
1967 std::optional<nlohmann::json> dhcpv6;
1968 std::optional<bool> interfaceEnabled;
Tejas Patil35fb5312021-09-20 15:35:20 +05301969 std::optional<size_t> mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001970 DHCPParameters v4dhcpParms;
1971 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001972
Willy Tu15ed6782021-12-14 11:03:16 -08001973 if (!json_util::readJsonPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001974 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1975 "IPv4StaticAddresses", ipv4StaticAddresses,
1976 "MACAddress", macAddress, "StaticNameServers",
1977 staticNameServers, "IPv6DefaultGateway",
1978 ipv6DefaultGateway, "IPv6StaticAddresses",
1979 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
Tejas Patil35fb5312021-09-20 15:35:20 +05301980 "MTUSize", mtuSize, "InterfaceEnabled",
1981 interfaceEnabled))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001982 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001983 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001984 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001985 if (dhcpv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001986 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001987 if (!json_util::readJson(
1988 *dhcpv4, asyncResp->res, "DHCPEnabled",
1989 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1990 v4dhcpParms.useDNSServers, "UseNTPServers",
1991 v4dhcpParms.useNTPServers, "UseDomainName",
1992 v4dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001993 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001994 return;
1995 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001996 }
1997
Ed Tanousbf648f72021-06-03 15:00:14 -07001998 if (dhcpv6)
Johnathan Mantey01784822019-06-18 12:44:21 -07001999 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002000 if (!json_util::readJson(
2001 *dhcpv6, asyncResp->res, "OperatingMode",
2002 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
2003 v6dhcpParms.useDNSServers, "UseNTPServers",
2004 v6dhcpParms.useNTPServers, "UseDomainName",
2005 v6dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07002006 {
Johnathan Mantey01784822019-06-18 12:44:21 -07002007 return;
2008 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002009 }
2010
Ed Tanousbf648f72021-06-03 15:00:14 -07002011 // Get single eth interface data, and call the below callback
2012 // for JSON preparation
2013 getEthernetIfaceData(
2014 ifaceId,
2015 [asyncResp, ifaceId, hostname = std::move(hostname),
2016 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2017 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2018 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2019 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
2020 staticNameServers = std::move(staticNameServers),
2021 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
Tejas Patil35fb5312021-09-20 15:35:20 +05302022 mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanousbf648f72021-06-03 15:00:14 -07002023 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2024 const bool& success,
2025 const EthernetInterfaceData& ethData,
2026 const boost::container::flat_set<IPv4AddressData>&
2027 ipv4Data,
2028 const boost::container::flat_set<IPv6AddressData>&
2029 ipv6Data) {
2030 if (!success)
2031 {
2032 // ... otherwise return error
2033 // TODO(Pawel)consider distinguish between non
2034 // existing object, and other errors
2035 messages::resourceNotFound(
2036 asyncResp->res, "Ethernet Interface", ifaceId);
2037 return;
2038 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002039
Ed Tanousbf648f72021-06-03 15:00:14 -07002040 if (dhcpv4 || dhcpv6)
2041 {
2042 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2043 v6dhcpParms, asyncResp);
2044 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002045
Ed Tanousbf648f72021-06-03 15:00:14 -07002046 if (hostname)
2047 {
2048 handleHostnamePatch(*hostname, asyncResp);
2049 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002050
Ed Tanousbf648f72021-06-03 15:00:14 -07002051 if (fqdn)
2052 {
2053 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2054 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002055
Ed Tanousbf648f72021-06-03 15:00:14 -07002056 if (macAddress)
2057 {
2058 handleMACAddressPatch(ifaceId, *macAddress,
2059 asyncResp);
2060 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002061
Ed Tanousbf648f72021-06-03 15:00:14 -07002062 if (ipv4StaticAddresses)
2063 {
2064 // TODO(ed) for some reason the capture of
2065 // ipv4Addresses above is returning a const value,
2066 // not a non-const value. This doesn't really work
2067 // for us, as we need to be able to efficiently move
2068 // out the intermedia nlohmann::json objects. This
2069 // makes a copy of the structure, and operates on
2070 // that, but could be done more efficiently
2071 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2072 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2073 asyncResp);
2074 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002075
Ed Tanousbf648f72021-06-03 15:00:14 -07002076 if (staticNameServers)
2077 {
2078 handleStaticNameServersPatch(
2079 ifaceId, *staticNameServers, asyncResp);
2080 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002081
Ed Tanousbf648f72021-06-03 15:00:14 -07002082 if (ipv6DefaultGateway)
2083 {
2084 messages::propertyNotWritable(asyncResp->res,
2085 "IPv6DefaultGateway");
2086 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08002087
Ed Tanousbf648f72021-06-03 15:00:14 -07002088 if (ipv6StaticAddresses)
2089 {
Ed Tanousf5b191a2022-02-15 11:30:39 -08002090 const nlohmann::json& ipv6Static =
2091 *ipv6StaticAddresses;
Ed Tanousbf648f72021-06-03 15:00:14 -07002092 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2093 ipv6Data, asyncResp);
2094 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002095
Ed Tanousbf648f72021-06-03 15:00:14 -07002096 if (interfaceEnabled)
2097 {
2098 setEthernetInterfaceBoolProperty(
2099 ifaceId, "NICEnabled", *interfaceEnabled,
2100 asyncResp);
2101 }
Tejas Patil35fb5312021-09-20 15:35:20 +05302102
2103 if (mtuSize)
2104 {
2105 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2106 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002107 });
2108 });
manojkiraneda2a133282019-02-19 13:09:43 +05302109
Ed Tanousbf648f72021-06-03 15:00:14 -07002110 BMCWEB_ROUTE(
2111 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002112 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002113 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002114 [&app](const crow::Request& req,
2115 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2116 const std::string& parentIfaceId,
2117 const std::string& ifaceId) {
2118 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2119 {
2120 return;
2121 }
Ed Tanous0f74e642018-11-12 15:17:05 -08002122 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousbf648f72021-06-03 15:00:14 -07002123 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2124 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002125
Ed Tanousbf648f72021-06-03 15:00:14 -07002126 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002127 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002128 return;
2129 }
2130
Ed Tanousbf648f72021-06-03 15:00:14 -07002131 // Get single eth interface data, and call the below callback
2132 // for JSON preparation
2133 getEthernetIfaceData(
2134 ifaceId,
2135 [asyncResp, parentIfaceId, ifaceId](
2136 const bool& success,
2137 const EthernetInterfaceData& ethData,
2138 const boost::container::flat_set<IPv4AddressData>&,
2139 const boost::container::flat_set<IPv6AddressData>&) {
Ed Tanous26f69762022-01-25 09:49:11 -08002140 if (success && !ethData.vlan_id.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07002141 {
2142 parseInterfaceData(asyncResp->res.jsonValue,
2143 parentIfaceId, ifaceId, ethData);
2144 }
2145 else
2146 {
2147 // ... otherwise return error
2148 // TODO(Pawel)consider distinguish between non
2149 // existing object, and other errors
2150 messages::resourceNotFound(asyncResp->res,
2151 "VLAN Network Interface",
2152 ifaceId);
2153 }
2154 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002155 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002156
Ed Tanousbf648f72021-06-03 15:00:14 -07002157 BMCWEB_ROUTE(
2158 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002159 // This privilege is incorrect, it should be ConfigureManager
2160 //.privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002161 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002162 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002163 [&app](const crow::Request& req,
2164 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2165 const std::string& parentIfaceId,
2166 const std::string& ifaceId) {
2167 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2168 {
2169 return;
2170 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002171 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002172 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002173 messages::resourceNotFound(
2174 asyncResp->res, "VLAN Network Interface", ifaceId);
2175 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002176 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002177
Ed Tanousbf648f72021-06-03 15:00:14 -07002178 bool vlanEnable = false;
2179 uint32_t vlanId = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002180
Willy Tu15ed6782021-12-14 11:03:16 -08002181 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2182 vlanEnable, "VLANId", vlanId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002183 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002184 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002185 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002186
2187 // Get single eth interface data, and call the below callback
2188 // for JSON preparation
2189 getEthernetIfaceData(
2190 ifaceId,
2191 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2192 const bool& success,
2193 const EthernetInterfaceData& ethData,
2194 const boost::container::flat_set<IPv4AddressData>&,
2195 const boost::container::flat_set<IPv6AddressData>&) {
2196 if (success && !ethData.vlan_id.empty())
2197 {
2198 auto callback =
2199 [asyncResp](
2200 const boost::system::error_code ec) {
2201 if (ec)
2202 {
2203 messages::internalError(asyncResp->res);
2204 }
2205 };
2206
Ed Tanouse05aec52022-01-25 10:28:56 -08002207 if (vlanEnable)
Ed Tanousbf648f72021-06-03 15:00:14 -07002208 {
2209 crow::connections::systemBus->async_method_call(
2210 std::move(callback),
2211 "xyz.openbmc_project.Network",
2212 "/xyz/openbmc_project/network/" + ifaceId,
2213 "org.freedesktop.DBus.Properties", "Set",
2214 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanous168e20c2021-12-13 14:39:53 -08002215 dbus::utility::DbusVariantType(vlanId));
Ed Tanousbf648f72021-06-03 15:00:14 -07002216 }
2217 else
2218 {
2219 BMCWEB_LOG_DEBUG
2220 << "vlanEnable is false. Deleting the "
2221 "vlan interface";
2222 crow::connections::systemBus->async_method_call(
2223 std::move(callback),
2224 "xyz.openbmc_project.Network",
2225 std::string(
2226 "/xyz/openbmc_project/network/") +
2227 ifaceId,
2228 "xyz.openbmc_project.Object.Delete",
2229 "Delete");
2230 }
2231 }
2232 else
2233 {
2234 // TODO(Pawel)consider distinguish between non
2235 // existing object, and other errors
2236 messages::resourceNotFound(asyncResp->res,
2237 "VLAN Network Interface",
2238 ifaceId);
2239 return;
2240 }
2241 });
2242 });
2243
2244 BMCWEB_ROUTE(
2245 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002246 // This privilege is incorrect, it should be ConfigureManager
2247 //.privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002248 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002249 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002250 [&app](const crow::Request& req,
2251 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2252 const std::string& parentIfaceId,
2253 const std::string& ifaceId) {
2254 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2255 {
2256 return;
2257 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002258 if (!verifyNames(parentIfaceId, ifaceId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002259 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002260 messages::resourceNotFound(
2261 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07002262 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002263 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002264
2265 // Get single eth interface data, and call the below callback
2266 // for JSON preparation
2267 getEthernetIfaceData(
2268 ifaceId,
2269 [asyncResp, parentIfaceId, ifaceId](
2270 const bool& success,
2271 const EthernetInterfaceData& ethData,
2272 const boost::container::flat_set<IPv4AddressData>&,
2273 const boost::container::flat_set<IPv6AddressData>&) {
2274 if (success && !ethData.vlan_id.empty())
2275 {
2276 auto callback =
2277 [asyncResp](
2278 const boost::system::error_code ec) {
2279 if (ec)
2280 {
2281 messages::internalError(asyncResp->res);
2282 }
2283 };
2284 crow::connections::systemBus->async_method_call(
2285 std::move(callback),
2286 "xyz.openbmc_project.Network",
2287 std::string("/xyz/openbmc_project/network/") +
2288 ifaceId,
2289 "xyz.openbmc_project.Object.Delete", "Delete");
2290 }
2291 else
2292 {
2293 // ... otherwise return error
2294 // TODO(Pawel)consider distinguish between non
2295 // existing object, and other errors
2296 messages::resourceNotFound(asyncResp->res,
2297 "VLAN Network Interface",
2298 ifaceId);
2299 }
2300 });
Jason M. Billsf12894f2018-10-09 12:45:45 -07002301 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002302
Ed Tanousbf648f72021-06-03 15:00:14 -07002303 BMCWEB_ROUTE(app,
2304 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002305
2306 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002307 .methods(
2308 boost::beast::http::verb::
Ed Tanous45ca1b82022-03-25 13:07:27 -07002309 get)([&app](const crow::Request& req,
2310 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2311 const std::string& rootInterfaceName) {
2312 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2313 {
2314 return;
2315 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002316 // Get eth interface list, and call the below callback for JSON
2317 // preparation
2318 getEthernetIfaceList([asyncResp, rootInterfaceName](
2319 const bool& success,
2320 const boost::container::flat_set<
2321 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002322 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002323 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002324 messages::internalError(asyncResp->res);
2325 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002326 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002327
Ed Tanous81ce6092020-12-17 16:54:55 +00002328 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002329 {
2330 messages::resourceNotFound(asyncResp->res,
2331 "VLanNetworkInterfaceCollection",
2332 rootInterfaceName);
2333 return;
2334 }
2335
Ed Tanous0f74e642018-11-12 15:17:05 -08002336 asyncResp->res.jsonValue["@odata.type"] =
2337 "#VLanNetworkInterfaceCollection."
2338 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002339 asyncResp->res.jsonValue["Name"] =
2340 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002341
Ed Tanous2c70f802020-09-28 14:29:23 -07002342 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002343
Ed Tanous81ce6092020-12-17 16:54:55 +00002344 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002345 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002346 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002347 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002348 std::string path =
2349 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2350 path += rootInterfaceName;
2351 path += "/VLANs/";
2352 path += ifaceItem;
2353 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002354 }
2355 }
2356
Jason M. Billsf12894f2018-10-09 12:45:45 -07002357 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002358 ifaceArray.size();
2359 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002360 asyncResp->res.jsonValue["@odata.id"] =
2361 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2362 rootInterfaceName + "/VLANs";
2363 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002364 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002365
Ed Tanousbf648f72021-06-03 15:00:14 -07002366 BMCWEB_ROUTE(app,
2367 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002368 // This privilege is wrong, it should be ConfigureManager
2369 //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002370 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002371 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002372 [&app](const crow::Request& req,
2373 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2374 const std::string& rootInterfaceName) {
2375 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2376 {
2377 return;
2378 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002379 bool vlanEnable = false;
2380 uint32_t vlanId = 0;
Willy Tu15ed6782021-12-14 11:03:16 -08002381 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId",
2382 vlanId, "VLANEnable", vlanEnable))
Ed Tanousbf648f72021-06-03 15:00:14 -07002383 {
2384 return;
2385 }
2386 // Need both vlanId and vlanEnable to service this request
Ed Tanousdbb59d42022-01-25 11:09:55 -08002387 if (vlanId == 0U)
Ed Tanousbf648f72021-06-03 15:00:14 -07002388 {
2389 messages::propertyMissing(asyncResp->res, "VLANId");
2390 }
2391 if (!vlanEnable)
2392 {
2393 messages::propertyMissing(asyncResp->res, "VLANEnable");
2394 }
2395 if (static_cast<bool>(vlanId) ^ vlanEnable)
2396 {
2397 return;
2398 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002399
Ed Tanousbf648f72021-06-03 15:00:14 -07002400 auto callback =
2401 [asyncResp](const boost::system::error_code ec) {
2402 if (ec)
2403 {
2404 // TODO(ed) make more consistent error messages
2405 // based on phosphor-network responses
2406 messages::internalError(asyncResp->res);
2407 return;
2408 }
2409 messages::created(asyncResp->res);
2410 };
2411 crow::connections::systemBus->async_method_call(
2412 std::move(callback), "xyz.openbmc_project.Network",
2413 "/xyz/openbmc_project/network",
2414 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2415 rootInterfaceName, vlanId);
2416 });
2417}
2418
Ed Tanous1abe55e2018-09-05 08:30:59 -07002419} // namespace redfish