blob: 7986c6f3c753cc72e07cc2233d2698999fbd4a7c [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;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080086 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070087 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070088 std::string DHCPEnabled;
89 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -070090 std::string hostname;
91 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -050092 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -070093 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -050094 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -050095 std::vector<std::string> nameServers;
96 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -080097 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010098};
99
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700100struct DHCPParameters
101{
102 std::optional<bool> dhcpv4Enabled;
103 std::optional<bool> useDNSServers;
104 std::optional<bool> useNTPServers;
105 std::optional<bool> useUseDomainName;
106 std::optional<std::string> dhcpv6OperatingMode;
107};
108
Ed Tanous4a0cb852018-10-15 07:55:04 -0700109// Helper function that changes bits netmask notation (i.e. /24)
110// into full dot notation
111inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700113 uint32_t value = 0xffffffff << (32 - bits);
114 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
115 std::to_string((value >> 16) & 0xff) + "." +
116 std::to_string((value >> 8) & 0xff) + "." +
117 std::to_string(value & 0xff);
118 return netmask;
119}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100120
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500121inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700122 bool isIPv4)
123{
124 if (isIPv4)
125 {
126 return (
127 (inputDHCP ==
128 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
129 (inputDHCP ==
130 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
131 }
132 return ((inputDHCP ==
133 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
134 (inputDHCP ==
135 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
136}
137
Ed Tanous2c70f802020-09-28 14:29:23 -0700138inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700139{
140 if (isIPv4 && isIPv6)
141 {
142 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
143 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700144 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700145 {
146 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
147 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700148 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700149 {
150 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
151 }
152 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
153}
154
Ed Tanous4a0cb852018-10-15 07:55:04 -0700155inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500156 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700157 bool isIPv4)
158{
159 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700161 return "Static";
162 }
163 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
164 {
165 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700167 return "IPv4LinkLocal";
168 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700169 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700170 }
171 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
172 {
173 if (isIPv4)
174 {
175 return "DHCP";
176 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700177 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 }
179 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
180 {
181 return "SLAAC";
182 }
183 return "";
184}
185
Ed Tanous711ac7a2021-12-20 09:34:41 -0800186inline bool
187 extractEthernetInterfaceData(const std::string& ethifaceId,
188 dbus::utility::ManagedObjectType& dbusData,
189 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700190{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700191 bool idFound = false;
Ed Tanous81ce6092020-12-17 16:54:55 +0000192 for (auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700193 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700194 for (auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700195 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000196 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700198 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700199 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500201 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700203 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800206 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700207 if (mac != nullptr)
208 {
209 ethData.mac_address = *mac;
210 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700211 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700212 }
213 }
214 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
215 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500216 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700217 {
218 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500220 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800221 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700222 if (id != nullptr)
223 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500224 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700225 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700227 }
228 }
229 else if (ifacePair.first ==
230 "xyz.openbmc_project.Network.EthernetInterface")
231 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500232 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 {
234 if (propertyPair.first == "AutoNeg")
235 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700236 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800237 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700238 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700240 ethData.auto_neg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 }
242 }
243 else if (propertyPair.first == "Speed")
244 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500245 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800246 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 if (speed != nullptr)
248 {
249 ethData.speed = *speed;
250 }
251 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530252 else if (propertyPair.first == "MTU")
253 {
254 const uint32_t* mtuSize =
255 std::get_if<uint32_t>(&propertyPair.second);
256 if (mtuSize != nullptr)
257 {
258 ethData.mtuSize = *mtuSize;
259 }
260 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800261 else if (propertyPair.first == "LinkUp")
262 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500263 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800264 std::get_if<bool>(&propertyPair.second);
265 if (linkUp != nullptr)
266 {
267 ethData.linkUp = *linkUp;
268 }
269 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700270 else if (propertyPair.first == "NICEnabled")
271 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500272 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700273 std::get_if<bool>(&propertyPair.second);
274 if (nicEnabled != nullptr)
275 {
276 ethData.nicEnabled = *nicEnabled;
277 }
278 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500279 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700280 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500281 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500282 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800283 &propertyPair.second);
284 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700285 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700286 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500287 }
288 }
289 else if (propertyPair.first == "StaticNameServers")
290 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500291 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500292 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500293 &propertyPair.second);
294 if (staticNameServers != nullptr)
295 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700296 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700297 }
298 }
manojkiraneda2a133282019-02-19 13:09:43 +0530299 else if (propertyPair.first == "DHCPEnabled")
300 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700301 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700302 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700303 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530304 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700305 ethData.DHCPEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530306 }
307 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800308 else if (propertyPair.first == "DomainName")
309 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500310 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500311 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800312 &propertyPair.second);
313 if (domainNames != nullptr)
314 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700315 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800316 }
317 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500318 else if (propertyPair.first == "DefaultGateway")
319 {
320 const std::string* defaultGateway =
321 std::get_if<std::string>(&propertyPair.second);
322 if (defaultGateway != nullptr)
323 {
324 std::string defaultGatewayStr = *defaultGateway;
325 if (defaultGatewayStr.empty())
326 {
327 ethData.default_gateway = "0.0.0.0";
328 }
329 else
330 {
331 ethData.default_gateway = defaultGatewayStr;
332 }
333 }
334 }
335 else if (propertyPair.first == "DefaultGateway6")
336 {
337 const std::string* defaultGateway6 =
338 std::get_if<std::string>(&propertyPair.second);
339 if (defaultGateway6 != nullptr)
340 {
341 std::string defaultGateway6Str =
342 *defaultGateway6;
343 if (defaultGateway6Str.empty())
344 {
345 ethData.ipv6_default_gateway =
346 "0:0:0:0:0:0:0:0";
347 }
348 else
349 {
350 ethData.ipv6_default_gateway =
351 defaultGateway6Str;
352 }
353 }
354 }
Ed Tanous029573d2019-02-01 10:57:49 -0800355 }
356 }
357 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700358
359 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
360 {
361 if (ifacePair.first ==
362 "xyz.openbmc_project.Network.DHCPConfiguration")
363 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500364 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700365 {
366 if (propertyPair.first == "DNSEnabled")
367 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700368 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700369 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700370 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700371 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700372 ethData.DNSEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700373 }
374 }
375 else if (propertyPair.first == "NTPEnabled")
376 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700377 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700378 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700379 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700380 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700381 ethData.NTPEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700382 }
383 }
384 else if (propertyPair.first == "HostNameEnabled")
385 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700386 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700387 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700388 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700389 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700390 ethData.HostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700391 }
392 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700393 }
394 }
395 }
Ed Tanous029573d2019-02-01 10:57:49 -0800396 // System configuration shows up in the global namespace, so no need
397 // to check eth number
398 if (ifacePair.first ==
399 "xyz.openbmc_project.Network.SystemConfiguration")
400 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500401 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800402 {
403 if (propertyPair.first == "HostName")
404 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500405 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500406 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800407 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700408 {
Ed Tanous029573d2019-02-01 10:57:49 -0800409 ethData.hostname = *hostname;
410 }
411 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700412 }
413 }
414 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700415 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700416 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700417}
418
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500419// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700420inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000421 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800422 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000423 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500424{
425 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000426 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500427
428 // Since there might be several IPv6 configurations aligned with
429 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000430 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500431 {
432 // Check if proper pattern for object path appears
433 if (boost::starts_with(objpath.first.str, ipv6PathStart))
434 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800435 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500436 {
437 if (interface.first == "xyz.openbmc_project.Network.IP")
438 {
439 // Instance IPv6AddressData structure, and set as
440 // appropriate
441 std::pair<
442 boost::container::flat_set<IPv6AddressData>::iterator,
443 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000444 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700445 IPv6AddressData& ipv6Address = *it.first;
446 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700447 objpath.first.str.substr(ipv6PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800448 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500449 {
450 if (property.first == "Address")
451 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500452 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500453 std::get_if<std::string>(&property.second);
454 if (address != nullptr)
455 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700456 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500457 }
458 }
459 else if (property.first == "Origin")
460 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500461 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500462 std::get_if<std::string>(&property.second);
463 if (origin != nullptr)
464 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700465 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500466 translateAddressOriginDbusToRedfish(*origin,
467 false);
468 }
469 }
470 else if (property.first == "PrefixLength")
471 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500472 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500473 std::get_if<uint8_t>(&property.second);
474 if (prefix != nullptr)
475 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700476 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500477 }
478 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600479 else if (property.first == "Type" ||
480 property.first == "Gateway")
481 {
482 // Type & Gateway is not used
483 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500484 else
485 {
486 BMCWEB_LOG_ERROR
487 << "Got extra property: " << property.first
488 << " on the " << objpath.first.str << " object";
489 }
490 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500491 }
492 }
493 }
494 }
495}
496
Ed Tanous4a0cb852018-10-15 07:55:04 -0700497// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700498inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000499 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800500 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000501 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700502{
503 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000504 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700505
506 // Since there might be several IPv4 configurations aligned with
507 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000508 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700509 {
510 // Check if proper pattern for object path appears
511 if (boost::starts_with(objpath.first.str, ipv4PathStart))
512 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800513 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700514 {
515 if (interface.first == "xyz.openbmc_project.Network.IP")
516 {
517 // Instance IPv4AddressData structure, and set as
518 // appropriate
519 std::pair<
520 boost::container::flat_set<IPv4AddressData>::iterator,
521 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000522 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700523 IPv4AddressData& ipv4Address = *it.first;
524 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700525 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800526 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700527 {
528 if (property.first == "Address")
529 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500530 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800531 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700532 if (address != nullptr)
533 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700534 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700535 }
536 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700537 else if (property.first == "Origin")
538 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500539 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800540 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700541 if (origin != nullptr)
542 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700543 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700544 translateAddressOriginDbusToRedfish(*origin,
545 true);
546 }
547 }
548 else if (property.first == "PrefixLength")
549 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500550 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800551 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700552 if (mask != nullptr)
553 {
554 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700555 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700556 }
557 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600558 else if (property.first == "Type" ||
559 property.first == "Gateway")
560 {
561 // Type & Gateway is not used
562 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700563 else
564 {
565 BMCWEB_LOG_ERROR
566 << "Got extra property: " << property.first
567 << " on the " << objpath.first.str << " object";
568 }
569 }
570 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700571 ipv4Address.linktype =
572 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700573 ? LinkType::Local
574 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700575 }
576 }
577 }
578 }
579}
580
581/**
Ed Tanous4a0cb852018-10-15 07:55:04 -0700582 * @brief Helper function that verifies IP address to check if it is in
583 * proper format. If bits pointer is provided, also calculates active
584 * bit count for Subnet Mask.
585 *
586 * @param[in] ip IP that will be verified
587 * @param[out] bits Calculated mask in bits notation
588 *
589 * @return true in case of success, false otherwise
590 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500591inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
592 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700593{
594 std::vector<std::string> bytesInMask;
595
596 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700597
598 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700599 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700600 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700601 return false;
602 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700603
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700606 *bits = 0;
607 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608
Ed Tanous543f4402022-01-06 13:12:53 -0800609 char* endPtr = nullptr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700610 long previousValue = 255;
Ed Tanous543f4402022-01-06 13:12:53 -0800611 bool firstZeroInByteHit = false;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500612 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700614 if (byte.empty())
615 {
616 return false;
617 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700618
Ed Tanous4a0cb852018-10-15 07:55:04 -0700619 // Use strtol instead of stroi to avoid exceptions
620 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621
Ed Tanous4a0cb852018-10-15 07:55:04 -0700622 // endPtr should point to the end of the string, otherwise given string
623 // is not 100% number
624 if (*endPtr != '\0')
625 {
626 return false;
627 }
628
629 // Value should be contained in byte
630 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631 {
632 return false;
633 }
634
635 if (bits != nullptr)
636 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700637 // Mask has to be continuous between bytes
638 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639 {
640 return false;
641 }
642
Ed Tanous4a0cb852018-10-15 07:55:04 -0700643 // Mask has to be continuous inside bytes
644 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700645
Ed Tanous4a0cb852018-10-15 07:55:04 -0700646 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000647 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700648 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800649 if ((value & (1L << bitIdx)) != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700651 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700652 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 // Continuity not preserved
654 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700656 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700658 else
659 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700660 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700661 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 }
663 }
664
665 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666 }
667
Ed Tanous4a0cb852018-10-15 07:55:04 -0700668 return true;
669}
670
671/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700672 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700673 *
674 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700675 * @param[in] ipHash DBus Hash id of IP that should be deleted
676 * @param[io] asyncResp Response object that will be returned to client
677 *
678 * @return None
679 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500680inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800681 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700682{
683 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700684 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 if (ec)
686 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800687 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100688 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700689 },
690 "xyz.openbmc_project.Network",
691 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
692 "xyz.openbmc_project.Object.Delete", "Delete");
693}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700694
Gunnar Mills244b6d52021-04-12 15:44:23 -0500695inline void updateIPv4DefaultGateway(
696 const std::string& ifaceId, const std::string& gateway,
697 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500698{
699 crow::connections::systemBus->async_method_call(
700 [asyncResp](const boost::system::error_code ec) {
701 if (ec)
702 {
703 messages::internalError(asyncResp->res);
704 return;
705 }
706 asyncResp->res.result(boost::beast::http::status::no_content);
707 },
708 "xyz.openbmc_project.Network",
709 "/xyz/openbmc_project/network/" + ifaceId,
710 "org.freedesktop.DBus.Properties", "Set",
711 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800712 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500713}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700714/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700715 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700716 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700717 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
718 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
719 * @param[in] gateway IPv4 address of this interfaces gateway
720 * @param[in] address IPv4 address to assign to this interface
721 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700722 *
723 * @return None
724 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000725inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
726 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800727 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700728{
Ravi Teja9010ec22019-08-01 23:30:25 -0500729 auto createIpHandler = [asyncResp, ifaceId,
730 gateway](const boost::system::error_code ec) {
731 if (ec)
732 {
733 messages::internalError(asyncResp->res);
734 return;
735 }
736 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
737 };
738
Ed Tanous4a0cb852018-10-15 07:55:04 -0700739 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500740 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700741 "/xyz/openbmc_project/network/" + ifaceId,
742 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700743 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700744 gateway);
745}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500746
747/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700748 * @brief Deletes the IPv4 entry for this interface and creates a replacement
749 * static IPv4 entry
750 *
751 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
752 * @param[in] id The unique hash entry identifying the DBus entry
753 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
754 * @param[in] gateway IPv4 address of this interfaces gateway
755 * @param[in] address IPv4 address to assign to this interface
756 * @param[io] asyncResp Response object that will be returned to client
757 *
758 * @return None
759 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800760inline void
761 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
762 uint8_t prefixLength, const std::string& gateway,
763 const std::string& address,
764 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700765{
766 crow::connections::systemBus->async_method_call(
767 [asyncResp, ifaceId, address, prefixLength,
768 gateway](const boost::system::error_code ec) {
769 if (ec)
770 {
771 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500772 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700773 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500774
Johnathan Mantey01784822019-06-18 12:44:21 -0700775 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500776 [asyncResp, ifaceId,
777 gateway](const boost::system::error_code ec2) {
Ed Tanous23a21a12020-07-25 04:45:05 +0000778 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700779 {
780 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500781 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700782 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500783 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -0700784 },
785 "xyz.openbmc_project.Network",
786 "/xyz/openbmc_project/network/" + ifaceId,
787 "xyz.openbmc_project.Network.IP.Create", "IP",
788 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
789 prefixLength, gateway);
790 },
791 "xyz.openbmc_project.Network",
792 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
793 "xyz.openbmc_project.Object.Delete", "Delete");
794}
795
796/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500797 * @brief Deletes given IPv6
798 *
799 * @param[in] ifaceId Id of interface whose IP should be deleted
800 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500801 * @param[io] asyncResp Response object that will be returned to client
802 *
803 * @return None
804 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500805inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800806 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500807{
808 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700809 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500810 if (ec)
811 {
812 messages::internalError(asyncResp->res);
813 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500814 },
815 "xyz.openbmc_project.Network",
816 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
817 "xyz.openbmc_project.Object.Delete", "Delete");
818}
819
820/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700821 * @brief Deletes the IPv6 entry for this interface and creates a replacement
822 * static IPv6 entry
823 *
824 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
825 * @param[in] id The unique hash entry identifying the DBus entry
826 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
827 * @param[in] address IPv6 address to assign to this interface
828 * @param[io] asyncResp Response object that will be returned to client
829 *
830 * @return None
831 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800832inline void
833 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
834 uint8_t prefixLength, const std::string& address,
835 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700836{
837 crow::connections::systemBus->async_method_call(
838 [asyncResp, ifaceId, address,
839 prefixLength](const boost::system::error_code ec) {
840 if (ec)
841 {
842 messages::internalError(asyncResp->res);
843 }
844 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000845 [asyncResp](const boost::system::error_code ec2) {
846 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700847 {
848 messages::internalError(asyncResp->res);
849 }
850 },
851 "xyz.openbmc_project.Network",
852 "/xyz/openbmc_project/network/" + ifaceId,
853 "xyz.openbmc_project.Network.IP.Create", "IP",
854 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
855 prefixLength, "");
856 },
857 "xyz.openbmc_project.Network",
858 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
859 "xyz.openbmc_project.Object.Delete", "Delete");
860}
861
862/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500863 * @brief Creates IPv6 with given data
864 *
865 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500866 * @param[in] prefixLength Prefix length that needs to be added
867 * @param[in] address IP address that needs to be added
868 * @param[io] asyncResp Response object that will be returned to client
869 *
870 * @return None
871 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500872inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
873 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800874 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500875{
876 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
877 if (ec)
878 {
879 messages::internalError(asyncResp->res);
880 }
881 };
882 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500883 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500884 crow::connections::systemBus->async_method_call(
885 std::move(createIpHandler), "xyz.openbmc_project.Network",
886 "/xyz/openbmc_project/network/" + ifaceId,
887 "xyz.openbmc_project.Network.IP.Create", "IP",
888 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
889 "");
890}
891
Ed Tanous4a0cb852018-10-15 07:55:04 -0700892/**
893 * Function that retrieves all properties for given Ethernet Interface
894 * Object
895 * from EntityManager Network Manager
896 * @param ethiface_id a eth interface id to query on DBus
897 * @param callback a function that shall be called to convert Dbus output
898 * into JSON
899 */
900template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000901void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500902 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700903{
904 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800905 [ethifaceId{std::string{ethifaceId}},
906 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000907 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800908 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700909 EthernetInterfaceData ethData{};
910 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500911 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700912
Ed Tanous81ce6092020-12-17 16:54:55 +0000913 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700914 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700915 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700916 return;
917 }
918
Ed Tanous4c9afe42019-05-03 16:59:57 -0700919 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700920 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700921 if (!found)
922 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700923 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700924 return;
925 }
926
Ed Tanous2c70f802020-09-28 14:29:23 -0700927 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700928 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500929 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700930 {
Ravi Tejac6191412019-07-30 00:53:50 -0500931 if (((ipv4.linktype == LinkType::Global) &&
932 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500933 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700935 ipv4.gateway = ethData.default_gateway;
936 }
937 }
938
Ed Tanous2c70f802020-09-28 14:29:23 -0700939 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500940 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700941 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700942 },
943 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
944 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700945}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700946
947/**
948 * Function that retrieves all Ethernet Interfaces available through Network
949 * Manager
950 * @param callback a function that shall be called to convert Dbus output
951 * into JSON.
952 */
953template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500954void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700955{
956 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800957 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000958 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800959 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700960 // Callback requires vector<string> to retrieve all available
961 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -0700962 boost::container::flat_set<std::string> ifaceList;
963 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +0000964 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700965 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700966 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700967 return;
968 }
969
970 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500971 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700972 {
973 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500974 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700975 {
976 // If interface is
977 // xyz.openbmc_project.Network.EthernetInterface, this is
978 // what we're looking for.
979 if (interface.first ==
980 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000982 std::string ifaceId = objpath.first.filename();
983 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700984 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000985 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000987 // and put it into output vector.
988 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 }
990 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700991 }
992 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -0700993 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700994 },
995 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
996 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700997}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100998
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700999inline void
1000 handleHostnamePatch(const std::string& hostname,
1001 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002{
Ed Tanousbf648f72021-06-03 15:00:14 -07001003 // SHOULD handle host names of up to 255 characters(RFC 1123)
1004 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001005 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001006 messages::propertyValueFormatError(asyncResp->res, hostname,
1007 "HostName");
1008 return;
1009 }
1010 crow::connections::systemBus->async_method_call(
1011 [asyncResp](const boost::system::error_code ec) {
1012 if (ec)
1013 {
1014 messages::internalError(asyncResp->res);
1015 }
1016 },
1017 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1018 "org.freedesktop.DBus.Properties", "Set",
1019 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001020 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001021}
1022
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001023inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301024 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1025 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1026{
1027 sdbusplus::message::object_path objPath =
1028 "/xyz/openbmc_project/network/" + ifaceId;
1029 crow::connections::systemBus->async_method_call(
1030 [asyncResp](const boost::system::error_code ec) {
1031 if (ec)
1032 {
1033 messages::internalError(asyncResp->res);
1034 }
1035 },
1036 "xyz.openbmc_project.Network", objPath,
1037 "org.freedesktop.DBus.Properties", "Set",
1038 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1039 std::variant<size_t>(mtuSize));
1040}
1041
1042inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001043 handleDomainnamePatch(const std::string& ifaceId,
1044 const std::string& domainname,
1045 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001046{
1047 std::vector<std::string> vectorDomainname = {domainname};
1048 crow::connections::systemBus->async_method_call(
1049 [asyncResp](const boost::system::error_code ec) {
1050 if (ec)
1051 {
1052 messages::internalError(asyncResp->res);
1053 }
1054 },
1055 "xyz.openbmc_project.Network",
1056 "/xyz/openbmc_project/network/" + ifaceId,
1057 "org.freedesktop.DBus.Properties", "Set",
1058 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001059 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001060}
1061
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001062inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001063{
1064 // A valid host name can never have the dotted-decimal form (RFC 1123)
1065 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1066 {
1067 return false;
1068 }
1069 // Each label(hostname/subdomains) within a valid FQDN
1070 // MUST handle host names of up to 63 characters (RFC 1123)
1071 // labels cannot start or end with hyphens (RFC 952)
1072 // labels can start with numbers (RFC 1123)
1073 const std::regex pattern(
1074 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1075
1076 return std::regex_match(hostname, pattern);
1077}
1078
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001079inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001080{
1081 // Can have multiple subdomains
1082 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001083 const std::regex pattern(
1084 "^([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 -07001085
1086 return std::regex_match(domainname, pattern);
1087}
1088
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001089inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1090 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001091{
1092 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1093 if (fqdn.length() > 255)
1094 {
1095 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1096 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097 }
1098
Ed Tanousbf648f72021-06-03 15:00:14 -07001099 size_t pos = fqdn.find('.');
1100 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001101 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001102 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1103 return;
1104 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001105
Ed Tanousbf648f72021-06-03 15:00:14 -07001106 std::string hostname;
1107 std::string domainname;
1108 domainname = (fqdn).substr(pos + 1);
1109 hostname = (fqdn).substr(0, pos);
1110
1111 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1112 {
1113 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1114 return;
1115 }
1116
1117 handleHostnamePatch(hostname, asyncResp);
1118 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1119}
1120
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001121inline void
1122 handleMACAddressPatch(const std::string& ifaceId,
1123 const std::string& macAddress,
1124 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001125{
1126 crow::connections::systemBus->async_method_call(
1127 [asyncResp, macAddress](const boost::system::error_code ec) {
1128 if (ec)
1129 {
1130 messages::internalError(asyncResp->res);
1131 return;
1132 }
1133 },
1134 "xyz.openbmc_project.Network",
1135 "/xyz/openbmc_project/network/" + ifaceId,
1136 "org.freedesktop.DBus.Properties", "Set",
1137 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001138 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001139}
1140
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001141inline void setDHCPEnabled(const std::string& ifaceId,
1142 const std::string& propertyName, const bool v4Value,
1143 const bool v6Value,
1144 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001145{
1146 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1147 crow::connections::systemBus->async_method_call(
1148 [asyncResp](const boost::system::error_code ec) {
1149 if (ec)
1150 {
1151 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1152 messages::internalError(asyncResp->res);
1153 return;
1154 }
1155 messages::success(asyncResp->res);
1156 },
1157 "xyz.openbmc_project.Network",
1158 "/xyz/openbmc_project/network/" + ifaceId,
1159 "org.freedesktop.DBus.Properties", "Set",
1160 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001161 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001162}
1163
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001164inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001165 const std::string& ifaceId, const std::string& propertyName,
1166 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1167{
1168 crow::connections::systemBus->async_method_call(
1169 [asyncResp](const boost::system::error_code ec) {
1170 if (ec)
1171 {
1172 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1173 messages::internalError(asyncResp->res);
1174 return;
1175 }
1176 },
1177 "xyz.openbmc_project.Network",
1178 "/xyz/openbmc_project/network/" + ifaceId,
1179 "org.freedesktop.DBus.Properties", "Set",
1180 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001181 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001182}
1183
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001184inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1185 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001186{
1187 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1188 crow::connections::systemBus->async_method_call(
1189 [asyncResp](const boost::system::error_code ec) {
1190 if (ec)
1191 {
1192 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1193 messages::internalError(asyncResp->res);
1194 return;
1195 }
1196 },
1197 "xyz.openbmc_project.Network",
1198 "/xyz/openbmc_project/network/config/dhcp",
1199 "org.freedesktop.DBus.Properties", "Set",
1200 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001201 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001202}
1203
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001204inline void handleDHCPPatch(const std::string& ifaceId,
1205 const EthernetInterfaceData& ethData,
1206 const DHCPParameters& v4dhcpParms,
1207 const DHCPParameters& v6dhcpParms,
1208 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001209{
1210 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1211 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1212
1213 bool nextv4DHCPState =
1214 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1215
1216 bool nextv6DHCPState{};
1217 if (v6dhcpParms.dhcpv6OperatingMode)
1218 {
1219 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1220 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1221 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1222 {
1223 messages::propertyValueFormatError(asyncResp->res,
1224 *v6dhcpParms.dhcpv6OperatingMode,
1225 "OperatingMode");
1226 return;
1227 }
1228 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1229 }
1230 else
1231 {
1232 nextv6DHCPState = ipv6Active;
1233 }
1234
1235 bool nextDNS{};
1236 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1237 {
1238 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1239 {
1240 messages::generalError(asyncResp->res);
1241 return;
1242 }
1243 nextDNS = *v4dhcpParms.useDNSServers;
1244 }
1245 else if (v4dhcpParms.useDNSServers)
1246 {
1247 nextDNS = *v4dhcpParms.useDNSServers;
1248 }
1249 else if (v6dhcpParms.useDNSServers)
1250 {
1251 nextDNS = *v6dhcpParms.useDNSServers;
1252 }
1253 else
1254 {
1255 nextDNS = ethData.DNSEnabled;
1256 }
1257
1258 bool nextNTP{};
1259 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1260 {
1261 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1262 {
1263 messages::generalError(asyncResp->res);
1264 return;
1265 }
1266 nextNTP = *v4dhcpParms.useNTPServers;
1267 }
1268 else if (v4dhcpParms.useNTPServers)
1269 {
1270 nextNTP = *v4dhcpParms.useNTPServers;
1271 }
1272 else if (v6dhcpParms.useNTPServers)
1273 {
1274 nextNTP = *v6dhcpParms.useNTPServers;
1275 }
1276 else
1277 {
1278 nextNTP = ethData.NTPEnabled;
1279 }
1280
1281 bool nextUseDomain{};
1282 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1283 {
1284 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1285 {
1286 messages::generalError(asyncResp->res);
1287 return;
1288 }
1289 nextUseDomain = *v4dhcpParms.useUseDomainName;
1290 }
1291 else if (v4dhcpParms.useUseDomainName)
1292 {
1293 nextUseDomain = *v4dhcpParms.useUseDomainName;
1294 }
1295 else if (v6dhcpParms.useUseDomainName)
1296 {
1297 nextUseDomain = *v6dhcpParms.useUseDomainName;
1298 }
1299 else
1300 {
1301 nextUseDomain = ethData.HostNameEnabled;
1302 }
1303
1304 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1305 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1306 asyncResp);
1307 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1308 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1309 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1310 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1311 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1312 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1313}
1314
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001315inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001316 getNextStaticIpEntry(
1317 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1318 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1319{
1320 return std::find_if(head, end, [](const IPv4AddressData& value) {
1321 return value.origin == "Static";
1322 });
1323}
1324
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001325inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001326 getNextStaticIpEntry(
1327 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1328 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1329{
1330 return std::find_if(head, end, [](const IPv6AddressData& value) {
1331 return value.origin == "Static";
1332 });
1333}
1334
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001335inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001336 const std::string& ifaceId, nlohmann::json& input,
1337 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1338 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1339{
1340 if ((!input.is_array()) || input.empty())
1341 {
1342 messages::propertyValueTypeError(
1343 asyncResp->res,
1344 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1345 "IPv4StaticAddresses");
1346 return;
1347 }
1348
1349 unsigned entryIdx = 1;
1350 // Find the first static IP address currently active on the NIC and
1351 // match it to the first JSON element in the IPv4StaticAddresses array.
1352 // Match each subsequent JSON element to the next static IP programmed
1353 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001354 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001355 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1356
1357 for (nlohmann::json& thisJson : input)
1358 {
1359 std::string pathString =
1360 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1361
1362 if (!thisJson.is_null() && !thisJson.empty())
1363 {
1364 std::optional<std::string> address;
1365 std::optional<std::string> subnetMask;
1366 std::optional<std::string> gateway;
1367
1368 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1369 address, "SubnetMask", subnetMask,
1370 "Gateway", gateway))
1371 {
1372 messages::propertyValueFormatError(
1373 asyncResp->res,
1374 thisJson.dump(2, ' ', true,
1375 nlohmann::json::error_handler_t::replace),
1376 pathString);
1377 return;
1378 }
1379
1380 // Find the address/subnet/gateway values. Any values that are
1381 // not explicitly provided are assumed to be unmodified from the
1382 // current state of the interface. Merge existing state into the
1383 // current request.
1384 const std::string* addr = nullptr;
1385 const std::string* gw = nullptr;
1386 uint8_t prefixLength = 0;
1387 bool errorInEntry = false;
1388 if (address)
1389 {
1390 if (ipv4VerifyIpAndGetBitcount(*address))
1391 {
1392 addr = &(*address);
1393 }
1394 else
1395 {
1396 messages::propertyValueFormatError(asyncResp->res, *address,
1397 pathString + "/Address");
1398 errorInEntry = true;
1399 }
1400 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001401 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001402 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001403 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001404 }
1405 else
1406 {
1407 messages::propertyMissing(asyncResp->res,
1408 pathString + "/Address");
1409 errorInEntry = true;
1410 }
1411
1412 if (subnetMask)
1413 {
1414 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1415 {
1416 messages::propertyValueFormatError(
1417 asyncResp->res, *subnetMask,
1418 pathString + "/SubnetMask");
1419 errorInEntry = true;
1420 }
1421 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001422 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001423 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001424 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001425 &prefixLength))
1426 {
1427 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001428 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001429 pathString + "/SubnetMask");
1430 errorInEntry = true;
1431 }
1432 }
1433 else
1434 {
1435 messages::propertyMissing(asyncResp->res,
1436 pathString + "/SubnetMask");
1437 errorInEntry = true;
1438 }
1439
1440 if (gateway)
1441 {
1442 if (ipv4VerifyIpAndGetBitcount(*gateway))
1443 {
1444 gw = &(*gateway);
1445 }
1446 else
1447 {
1448 messages::propertyValueFormatError(asyncResp->res, *gateway,
1449 pathString + "/Gateway");
1450 errorInEntry = true;
1451 }
1452 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001453 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001454 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001455 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001456 }
1457 else
1458 {
1459 messages::propertyMissing(asyncResp->res,
1460 pathString + "/Gateway");
1461 errorInEntry = true;
1462 }
1463
1464 if (errorInEntry)
1465 {
1466 return;
1467 }
1468
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001469 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001470 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001471 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001472 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001473 nicIpEntry =
1474 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001475 }
1476 else
1477 {
1478 createIPv4(ifaceId, prefixLength, *gateway, *address,
1479 asyncResp);
1480 }
1481 entryIdx++;
1482 }
1483 else
1484 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001485 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001486 {
1487 // Requesting a DELETE/DO NOT MODIFY action for an item
1488 // that isn't present on the eth(n) interface. Input JSON is
1489 // in error, so bail out.
1490 if (thisJson.is_null())
1491 {
1492 messages::resourceCannotBeDeleted(asyncResp->res);
1493 return;
1494 }
1495 messages::propertyValueFormatError(
1496 asyncResp->res,
1497 thisJson.dump(2, ' ', true,
1498 nlohmann::json::error_handler_t::replace),
1499 pathString);
1500 return;
1501 }
1502
1503 if (thisJson.is_null())
1504 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001505 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001506 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001507 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001508 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001509 nicIpEntry =
1510 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001511 }
1512 entryIdx++;
1513 }
1514 }
1515}
1516
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001517inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001518 const std::string& ifaceId,
1519 const std::vector<std::string>& updatedStaticNameServers,
1520 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1521{
1522 crow::connections::systemBus->async_method_call(
1523 [asyncResp](const boost::system::error_code ec) {
1524 if (ec)
1525 {
1526 messages::internalError(asyncResp->res);
1527 return;
1528 }
1529 },
1530 "xyz.openbmc_project.Network",
1531 "/xyz/openbmc_project/network/" + ifaceId,
1532 "org.freedesktop.DBus.Properties", "Set",
1533 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001534 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001535}
1536
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001537inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001538 const std::string& ifaceId, const nlohmann::json& input,
1539 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1540 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1541{
1542 if (!input.is_array() || input.empty())
1543 {
1544 messages::propertyValueTypeError(
1545 asyncResp->res,
1546 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1547 "IPv6StaticAddresses");
1548 return;
1549 }
1550 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001551 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001552 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1553 for (const nlohmann::json& thisJson : input)
1554 {
1555 std::string pathString =
1556 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1557
1558 if (!thisJson.is_null() && !thisJson.empty())
1559 {
1560 std::optional<std::string> address;
1561 std::optional<uint8_t> prefixLength;
1562 nlohmann::json thisJsonCopy = thisJson;
1563 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1564 address, "PrefixLength", prefixLength))
1565 {
1566 messages::propertyValueFormatError(
1567 asyncResp->res,
1568 thisJson.dump(2, ' ', true,
1569 nlohmann::json::error_handler_t::replace),
1570 pathString);
1571 return;
1572 }
1573
Ed Tanous543f4402022-01-06 13:12:53 -08001574 const std::string* addr = nullptr;
1575 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001576
1577 // Find the address and prefixLength values. Any values that are
1578 // not explicitly provided are assumed to be unmodified from the
1579 // current state of the interface. Merge existing state into the
1580 // current request.
1581 if (address)
1582 {
1583 addr = &(*address);
1584 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001585 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001586 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001587 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001588 }
1589 else
1590 {
1591 messages::propertyMissing(asyncResp->res,
1592 pathString + "/Address");
1593 return;
1594 }
1595
1596 if (prefixLength)
1597 {
1598 prefix = *prefixLength;
1599 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001600 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001601 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001602 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001603 }
1604 else
1605 {
1606 messages::propertyMissing(asyncResp->res,
1607 pathString + "/PrefixLength");
1608 return;
1609 }
1610
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001611 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001612 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001613 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001614 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001615 nicIpEntry =
1616 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001617 }
1618 else
1619 {
1620 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1621 }
1622 entryIdx++;
1623 }
1624 else
1625 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001626 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001627 {
1628 // Requesting a DELETE/DO NOT MODIFY action for an item
1629 // that isn't present on the eth(n) interface. Input JSON is
1630 // in error, so bail out.
1631 if (thisJson.is_null())
1632 {
1633 messages::resourceCannotBeDeleted(asyncResp->res);
1634 return;
1635 }
1636 messages::propertyValueFormatError(
1637 asyncResp->res,
1638 thisJson.dump(2, ' ', true,
1639 nlohmann::json::error_handler_t::replace),
1640 pathString);
1641 return;
1642 }
1643
1644 if (thisJson.is_null())
1645 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001646 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001647 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001648 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001649 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001650 nicIpEntry =
1651 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001652 }
1653 entryIdx++;
1654 }
1655 }
1656}
1657
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001658inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001659 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1660 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1661 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1662 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1663{
1664 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1665 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1666
1667 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1668 jsonResponse["Id"] = ifaceId;
1669 jsonResponse["@odata.id"] =
1670 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1671 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1672
1673 auto health = std::make_shared<HealthPopulate>(asyncResp);
1674
1675 crow::connections::systemBus->async_method_call(
1676 [health](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001677 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanousbf648f72021-06-03 15:00:14 -07001678 if (ec)
1679 {
1680 return;
1681 }
1682
Ed Tanous914e2d52022-01-07 11:38:34 -08001683 health->inventory = resp;
Ed Tanousbf648f72021-06-03 15:00:14 -07001684 },
1685 "xyz.openbmc_project.ObjectMapper",
1686 "/xyz/openbmc_project/object_mapper",
1687 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1688 inventoryForEthernet);
1689
1690 health->populate();
1691
1692 if (ethData.nicEnabled)
1693 {
1694 jsonResponse["LinkStatus"] = "LinkUp";
1695 jsonResponse["Status"]["State"] = "Enabled";
1696 }
1697 else
1698 {
1699 jsonResponse["LinkStatus"] = "NoLink";
1700 jsonResponse["Status"]["State"] = "Disabled";
1701 }
1702
1703 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1704 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301705 jsonResponse["MTUSize"] = ethData.mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001706 jsonResponse["MACAddress"] = ethData.mac_address;
1707 jsonResponse["DHCPv4"]["DHCPEnabled"] =
1708 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1709 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1710 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1711 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1712
1713 jsonResponse["DHCPv6"]["OperatingMode"] =
1714 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1715 : "Disabled";
1716 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1717 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1718 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1719
1720 if (!ethData.hostname.empty())
1721 {
1722 jsonResponse["HostName"] = ethData.hostname;
1723
1724 // When domain name is empty then it means, that it is a network
1725 // without domain names, and the host name itself must be treated as
1726 // FQDN
1727 std::string fqdn = ethData.hostname;
1728 if (!ethData.domainnames.empty())
1729 {
1730 fqdn += "." + ethData.domainnames[0];
1731 }
1732 jsonResponse["FQDN"] = fqdn;
1733 }
1734
1735 jsonResponse["VLANs"] = {
1736 {"@odata.id",
1737 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1738
1739 jsonResponse["NameServers"] = ethData.nameServers;
1740 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1741
1742 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1743 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1744 ipv4Array = nlohmann::json::array();
1745 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001746 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001747 {
1748
1749 std::string gatewayStr = ipv4Config.gateway;
1750 if (gatewayStr.empty())
1751 {
1752 gatewayStr = "0.0.0.0";
1753 }
1754
1755 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1756 {"SubnetMask", ipv4Config.netmask},
1757 {"Address", ipv4Config.address},
1758 {"Gateway", gatewayStr}});
1759 if (ipv4Config.origin == "Static")
1760 {
1761 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1762 {"SubnetMask", ipv4Config.netmask},
1763 {"Address", ipv4Config.address},
1764 {"Gateway", gatewayStr}});
1765 }
1766 }
1767
1768 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1769 if (ipv6GatewayStr.empty())
1770 {
1771 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1772 }
1773
1774 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1775
1776 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1777 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1778 ipv6Array = nlohmann::json::array();
1779 ipv6StaticArray = nlohmann::json::array();
1780 nlohmann::json& ipv6AddrPolicyTable =
1781 jsonResponse["IPv6AddressPolicyTable"];
1782 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001783 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001784 {
1785 ipv6Array.push_back({{"Address", ipv6Config.address},
1786 {"PrefixLength", ipv6Config.prefixLength},
1787 {"AddressOrigin", ipv6Config.origin},
1788 {"AddressState", nullptr}});
1789 if (ipv6Config.origin == "Static")
1790 {
1791 ipv6StaticArray.push_back(
1792 {{"Address", ipv6Config.address},
Jiaqing Zhao7a474a52021-12-03 17:44:36 +08001793 {"PrefixLength", ipv6Config.prefixLength}});
Ed Tanousbf648f72021-06-03 15:00:14 -07001794 }
1795 }
1796}
1797
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001798inline void parseInterfaceData(nlohmann::json& jsonResponse,
1799 const std::string& parentIfaceId,
1800 const std::string& ifaceId,
1801 const EthernetInterfaceData& ethData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001802{
1803 // Fill out obvious data...
1804 jsonResponse["Id"] = ifaceId;
1805 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1806 parentIfaceId + "/VLANs/" + ifaceId;
1807
1808 jsonResponse["VLANEnable"] = true;
1809 if (!ethData.vlan_id.empty())
1810 {
1811 jsonResponse["VLANId"] = ethData.vlan_id.back();
1812 }
1813}
1814
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001815inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001816{
Ed Tanousdcf2ebc2022-01-25 10:07:45 -08001817 return boost::starts_with(iface, parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001818}
1819
1820inline void requestEthernetInterfacesRoutes(App& app)
1821{
1822 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001823 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001824 .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1825 const std::shared_ptr<
1826 bmcweb::AsyncResp>&
1827 asyncResp) {
1828 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1829 {
1830 return;
1831 }
1832
Ed Tanousbf648f72021-06-03 15:00:14 -07001833 asyncResp->res.jsonValue["@odata.type"] =
1834 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1835 asyncResp->res.jsonValue["@odata.id"] =
1836 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1837 asyncResp->res.jsonValue["Name"] =
1838 "Ethernet Network Interface Collection";
1839 asyncResp->res.jsonValue["Description"] =
1840 "Collection of EthernetInterfaces for this Manager";
1841
1842 // Get eth interface list, and call the below callback for JSON
1843 // preparation
1844 getEthernetIfaceList([asyncResp](const bool& success,
1845 const boost::container::flat_set<
1846 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001847 if (!success)
1848 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001849 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001850 return;
1851 }
1852
Ed Tanous2c70f802020-09-28 14:29:23 -07001853 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001854 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001855 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001856 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001857 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001858 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001859 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001860 if (found == std::string::npos)
1861 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001862 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001863 {{"@odata.id",
1864 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001865 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001866 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001867 }
1868
Ed Tanous4c9afe42019-05-03 16:59:57 -07001869 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001870 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001871 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001872 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001873 });
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301874 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001875
Ed Tanousbf648f72021-06-03 15:00:14 -07001876 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001877 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001878 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001879 [&app](const crow::Request& req,
1880 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1881 const std::string& ifaceId) {
1882 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1883 {
1884 return;
1885 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001886 getEthernetIfaceData(
1887 ifaceId,
1888 [asyncResp,
1889 ifaceId](const bool& success,
1890 const EthernetInterfaceData& ethData,
1891 const boost::container::flat_set<IPv4AddressData>&
1892 ipv4Data,
1893 const boost::container::flat_set<IPv6AddressData>&
1894 ipv6Data) {
1895 if (!success)
1896 {
1897 // TODO(Pawel)consider distinguish between non
1898 // existing object, and other errors
1899 messages::resourceNotFound(
1900 asyncResp->res, "EthernetInterface", ifaceId);
1901 return;
1902 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001903
Ed Tanousbf648f72021-06-03 15:00:14 -07001904 asyncResp->res.jsonValue["@odata.type"] =
1905 "#EthernetInterface.v1_4_1.EthernetInterface";
1906 asyncResp->res.jsonValue["Name"] =
1907 "Manager Ethernet Interface";
1908 asyncResp->res.jsonValue["Description"] =
1909 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301910
Ed Tanousbf648f72021-06-03 15:00:14 -07001911 parseInterfaceData(asyncResp, ifaceId, ethData,
1912 ipv4Data, ipv6Data);
1913 });
1914 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001915
Ed Tanousbf648f72021-06-03 15:00:14 -07001916 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001917 .privileges(redfish::privileges::patchEthernetInterface)
1918
Ed Tanousbf648f72021-06-03 15:00:14 -07001919 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001920 [&app](const crow::Request& req,
1921 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1922 const std::string& ifaceId) {
1923 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1924 {
1925 return;
1926 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001927 std::optional<std::string> hostname;
1928 std::optional<std::string> fqdn;
1929 std::optional<std::string> macAddress;
1930 std::optional<std::string> ipv6DefaultGateway;
1931 std::optional<nlohmann::json> ipv4StaticAddresses;
1932 std::optional<nlohmann::json> ipv6StaticAddresses;
1933 std::optional<std::vector<std::string>> staticNameServers;
1934 std::optional<nlohmann::json> dhcpv4;
1935 std::optional<nlohmann::json> dhcpv6;
1936 std::optional<bool> interfaceEnabled;
Tejas Patil35fb5312021-09-20 15:35:20 +05301937 std::optional<size_t> mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001938 DHCPParameters v4dhcpParms;
1939 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001940
Willy Tu15ed6782021-12-14 11:03:16 -08001941 if (!json_util::readJsonPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001942 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1943 "IPv4StaticAddresses", ipv4StaticAddresses,
1944 "MACAddress", macAddress, "StaticNameServers",
1945 staticNameServers, "IPv6DefaultGateway",
1946 ipv6DefaultGateway, "IPv6StaticAddresses",
1947 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
Tejas Patil35fb5312021-09-20 15:35:20 +05301948 "MTUSize", mtuSize, "InterfaceEnabled",
1949 interfaceEnabled))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001950 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001951 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001952 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001953 if (dhcpv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001954 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001955 if (!json_util::readJson(
1956 *dhcpv4, asyncResp->res, "DHCPEnabled",
1957 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1958 v4dhcpParms.useDNSServers, "UseNTPServers",
1959 v4dhcpParms.useNTPServers, "UseDomainName",
1960 v4dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001961 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001962 return;
1963 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001964 }
1965
Ed Tanousbf648f72021-06-03 15:00:14 -07001966 if (dhcpv6)
Johnathan Mantey01784822019-06-18 12:44:21 -07001967 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001968 if (!json_util::readJson(
1969 *dhcpv6, asyncResp->res, "OperatingMode",
1970 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1971 v6dhcpParms.useDNSServers, "UseNTPServers",
1972 v6dhcpParms.useNTPServers, "UseDomainName",
1973 v6dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001974 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001975 return;
1976 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001977 }
1978
Ed Tanousbf648f72021-06-03 15:00:14 -07001979 // Get single eth interface data, and call the below callback
1980 // for JSON preparation
1981 getEthernetIfaceData(
1982 ifaceId,
1983 [asyncResp, ifaceId, hostname = std::move(hostname),
1984 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1985 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1986 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1987 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1988 staticNameServers = std::move(staticNameServers),
1989 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
Tejas Patil35fb5312021-09-20 15:35:20 +05301990 mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanousbf648f72021-06-03 15:00:14 -07001991 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1992 const bool& success,
1993 const EthernetInterfaceData& ethData,
1994 const boost::container::flat_set<IPv4AddressData>&
1995 ipv4Data,
1996 const boost::container::flat_set<IPv6AddressData>&
1997 ipv6Data) {
1998 if (!success)
1999 {
2000 // ... otherwise return error
2001 // TODO(Pawel)consider distinguish between non
2002 // existing object, and other errors
2003 messages::resourceNotFound(
2004 asyncResp->res, "Ethernet Interface", ifaceId);
2005 return;
2006 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002007
Ed Tanousbf648f72021-06-03 15:00:14 -07002008 if (dhcpv4 || dhcpv6)
2009 {
2010 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2011 v6dhcpParms, asyncResp);
2012 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002013
Ed Tanousbf648f72021-06-03 15:00:14 -07002014 if (hostname)
2015 {
2016 handleHostnamePatch(*hostname, asyncResp);
2017 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002018
Ed Tanousbf648f72021-06-03 15:00:14 -07002019 if (fqdn)
2020 {
2021 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2022 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002023
Ed Tanousbf648f72021-06-03 15:00:14 -07002024 if (macAddress)
2025 {
2026 handleMACAddressPatch(ifaceId, *macAddress,
2027 asyncResp);
2028 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002029
Ed Tanousbf648f72021-06-03 15:00:14 -07002030 if (ipv4StaticAddresses)
2031 {
2032 // TODO(ed) for some reason the capture of
2033 // ipv4Addresses above is returning a const value,
2034 // not a non-const value. This doesn't really work
2035 // for us, as we need to be able to efficiently move
2036 // out the intermedia nlohmann::json objects. This
2037 // makes a copy of the structure, and operates on
2038 // that, but could be done more efficiently
2039 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2040 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2041 asyncResp);
2042 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002043
Ed Tanousbf648f72021-06-03 15:00:14 -07002044 if (staticNameServers)
2045 {
2046 handleStaticNameServersPatch(
2047 ifaceId, *staticNameServers, asyncResp);
2048 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002049
Ed Tanousbf648f72021-06-03 15:00:14 -07002050 if (ipv6DefaultGateway)
2051 {
2052 messages::propertyNotWritable(asyncResp->res,
2053 "IPv6DefaultGateway");
2054 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08002055
Ed Tanousbf648f72021-06-03 15:00:14 -07002056 if (ipv6StaticAddresses)
2057 {
Ed Tanousf5b191a2022-02-15 11:30:39 -08002058 const nlohmann::json& ipv6Static =
2059 *ipv6StaticAddresses;
Ed Tanousbf648f72021-06-03 15:00:14 -07002060 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2061 ipv6Data, asyncResp);
2062 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002063
Ed Tanousbf648f72021-06-03 15:00:14 -07002064 if (interfaceEnabled)
2065 {
2066 setEthernetInterfaceBoolProperty(
2067 ifaceId, "NICEnabled", *interfaceEnabled,
2068 asyncResp);
2069 }
Tejas Patil35fb5312021-09-20 15:35:20 +05302070
2071 if (mtuSize)
2072 {
2073 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2074 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002075 });
2076 });
manojkiraneda2a133282019-02-19 13:09:43 +05302077
Ed Tanousbf648f72021-06-03 15:00:14 -07002078 BMCWEB_ROUTE(
2079 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002080 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002081 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002082 [&app](const crow::Request& req,
2083 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2084 const std::string& parentIfaceId,
2085 const std::string& ifaceId) {
2086 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2087 {
2088 return;
2089 }
Ed Tanous0f74e642018-11-12 15:17:05 -08002090 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousbf648f72021-06-03 15:00:14 -07002091 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2092 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002093
Ed Tanousbf648f72021-06-03 15:00:14 -07002094 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002095 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002096 return;
2097 }
2098
Ed Tanousbf648f72021-06-03 15:00:14 -07002099 // Get single eth interface data, and call the below callback
2100 // for JSON preparation
2101 getEthernetIfaceData(
2102 ifaceId,
2103 [asyncResp, parentIfaceId, ifaceId](
2104 const bool& success,
2105 const EthernetInterfaceData& ethData,
2106 const boost::container::flat_set<IPv4AddressData>&,
2107 const boost::container::flat_set<IPv6AddressData>&) {
Ed Tanous26f69762022-01-25 09:49:11 -08002108 if (success && !ethData.vlan_id.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07002109 {
2110 parseInterfaceData(asyncResp->res.jsonValue,
2111 parentIfaceId, ifaceId, ethData);
2112 }
2113 else
2114 {
2115 // ... otherwise return error
2116 // TODO(Pawel)consider distinguish between non
2117 // existing object, and other errors
2118 messages::resourceNotFound(asyncResp->res,
2119 "VLAN Network Interface",
2120 ifaceId);
2121 }
2122 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002123 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002124
Ed Tanousbf648f72021-06-03 15:00:14 -07002125 BMCWEB_ROUTE(
2126 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002127 // This privilege is incorrect, it should be ConfigureManager
2128 //.privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002129 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002130 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002131 [&app](const crow::Request& req,
2132 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2133 const std::string& parentIfaceId,
2134 const std::string& ifaceId) {
2135 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2136 {
2137 return;
2138 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002139 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002140 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002141 messages::resourceNotFound(
2142 asyncResp->res, "VLAN Network Interface", ifaceId);
2143 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002144 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002145
Ed Tanousbf648f72021-06-03 15:00:14 -07002146 bool vlanEnable = false;
2147 uint32_t vlanId = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002148
Willy Tu15ed6782021-12-14 11:03:16 -08002149 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2150 vlanEnable, "VLANId", vlanId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002151 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002152 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002153 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002154
2155 // Get single eth interface data, and call the below callback
2156 // for JSON preparation
2157 getEthernetIfaceData(
2158 ifaceId,
2159 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2160 const bool& success,
2161 const EthernetInterfaceData& ethData,
2162 const boost::container::flat_set<IPv4AddressData>&,
2163 const boost::container::flat_set<IPv6AddressData>&) {
2164 if (success && !ethData.vlan_id.empty())
2165 {
2166 auto callback =
2167 [asyncResp](
2168 const boost::system::error_code ec) {
2169 if (ec)
2170 {
2171 messages::internalError(asyncResp->res);
2172 }
2173 };
2174
Ed Tanouse05aec52022-01-25 10:28:56 -08002175 if (vlanEnable)
Ed Tanousbf648f72021-06-03 15:00:14 -07002176 {
2177 crow::connections::systemBus->async_method_call(
2178 std::move(callback),
2179 "xyz.openbmc_project.Network",
2180 "/xyz/openbmc_project/network/" + ifaceId,
2181 "org.freedesktop.DBus.Properties", "Set",
2182 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanous168e20c2021-12-13 14:39:53 -08002183 dbus::utility::DbusVariantType(vlanId));
Ed Tanousbf648f72021-06-03 15:00:14 -07002184 }
2185 else
2186 {
2187 BMCWEB_LOG_DEBUG
2188 << "vlanEnable is false. Deleting the "
2189 "vlan interface";
2190 crow::connections::systemBus->async_method_call(
2191 std::move(callback),
2192 "xyz.openbmc_project.Network",
2193 std::string(
2194 "/xyz/openbmc_project/network/") +
2195 ifaceId,
2196 "xyz.openbmc_project.Object.Delete",
2197 "Delete");
2198 }
2199 }
2200 else
2201 {
2202 // TODO(Pawel)consider distinguish between non
2203 // existing object, and other errors
2204 messages::resourceNotFound(asyncResp->res,
2205 "VLAN Network Interface",
2206 ifaceId);
2207 return;
2208 }
2209 });
2210 });
2211
2212 BMCWEB_ROUTE(
2213 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002214 // This privilege is incorrect, it should be ConfigureManager
2215 //.privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002216 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002217 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002218 [&app](const crow::Request& req,
2219 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2220 const std::string& parentIfaceId,
2221 const std::string& ifaceId) {
2222 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2223 {
2224 return;
2225 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002226 if (!verifyNames(parentIfaceId, ifaceId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002227 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002228 messages::resourceNotFound(
2229 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07002230 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002231 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002232
2233 // Get single eth interface data, and call the below callback
2234 // for JSON preparation
2235 getEthernetIfaceData(
2236 ifaceId,
2237 [asyncResp, parentIfaceId, ifaceId](
2238 const bool& success,
2239 const EthernetInterfaceData& ethData,
2240 const boost::container::flat_set<IPv4AddressData>&,
2241 const boost::container::flat_set<IPv6AddressData>&) {
2242 if (success && !ethData.vlan_id.empty())
2243 {
2244 auto callback =
2245 [asyncResp](
2246 const boost::system::error_code ec) {
2247 if (ec)
2248 {
2249 messages::internalError(asyncResp->res);
2250 }
2251 };
2252 crow::connections::systemBus->async_method_call(
2253 std::move(callback),
2254 "xyz.openbmc_project.Network",
2255 std::string("/xyz/openbmc_project/network/") +
2256 ifaceId,
2257 "xyz.openbmc_project.Object.Delete", "Delete");
2258 }
2259 else
2260 {
2261 // ... otherwise return error
2262 // TODO(Pawel)consider distinguish between non
2263 // existing object, and other errors
2264 messages::resourceNotFound(asyncResp->res,
2265 "VLAN Network Interface",
2266 ifaceId);
2267 }
2268 });
Jason M. Billsf12894f2018-10-09 12:45:45 -07002269 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002270
Ed Tanousbf648f72021-06-03 15:00:14 -07002271 BMCWEB_ROUTE(app,
2272 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002273
2274 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002275 .methods(
2276 boost::beast::http::verb::
Ed Tanous45ca1b82022-03-25 13:07:27 -07002277 get)([&app](const crow::Request& req,
2278 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2279 const std::string& rootInterfaceName) {
2280 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2281 {
2282 return;
2283 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002284 // Get eth interface list, and call the below callback for JSON
2285 // preparation
2286 getEthernetIfaceList([asyncResp, rootInterfaceName](
2287 const bool& success,
2288 const boost::container::flat_set<
2289 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002290 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002291 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002292 messages::internalError(asyncResp->res);
2293 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002294 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002295
Ed Tanous81ce6092020-12-17 16:54:55 +00002296 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002297 {
2298 messages::resourceNotFound(asyncResp->res,
2299 "VLanNetworkInterfaceCollection",
2300 rootInterfaceName);
2301 return;
2302 }
2303
Ed Tanous0f74e642018-11-12 15:17:05 -08002304 asyncResp->res.jsonValue["@odata.type"] =
2305 "#VLanNetworkInterfaceCollection."
2306 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002307 asyncResp->res.jsonValue["Name"] =
2308 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002309
Ed Tanous2c70f802020-09-28 14:29:23 -07002310 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002311
Ed Tanous81ce6092020-12-17 16:54:55 +00002312 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002313 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002314 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002315 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002316 std::string path =
2317 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2318 path += rootInterfaceName;
2319 path += "/VLANs/";
2320 path += ifaceItem;
2321 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002322 }
2323 }
2324
Jason M. Billsf12894f2018-10-09 12:45:45 -07002325 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002326 ifaceArray.size();
2327 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002328 asyncResp->res.jsonValue["@odata.id"] =
2329 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2330 rootInterfaceName + "/VLANs";
2331 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002332 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002333
Ed Tanousbf648f72021-06-03 15:00:14 -07002334 BMCWEB_ROUTE(app,
2335 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002336 // This privilege is wrong, it should be ConfigureManager
2337 //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002338 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002339 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002340 [&app](const crow::Request& req,
2341 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2342 const std::string& rootInterfaceName) {
2343 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2344 {
2345 return;
2346 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002347 bool vlanEnable = false;
2348 uint32_t vlanId = 0;
Willy Tu15ed6782021-12-14 11:03:16 -08002349 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId",
2350 vlanId, "VLANEnable", vlanEnable))
Ed Tanousbf648f72021-06-03 15:00:14 -07002351 {
2352 return;
2353 }
2354 // Need both vlanId and vlanEnable to service this request
Ed Tanousdbb59d42022-01-25 11:09:55 -08002355 if (vlanId == 0U)
Ed Tanousbf648f72021-06-03 15:00:14 -07002356 {
2357 messages::propertyMissing(asyncResp->res, "VLANId");
2358 }
2359 if (!vlanEnable)
2360 {
2361 messages::propertyMissing(asyncResp->res, "VLANEnable");
2362 }
2363 if (static_cast<bool>(vlanId) ^ vlanEnable)
2364 {
2365 return;
2366 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002367
Ed Tanousbf648f72021-06-03 15:00:14 -07002368 auto callback =
2369 [asyncResp](const boost::system::error_code ec) {
2370 if (ec)
2371 {
2372 // TODO(ed) make more consistent error messages
2373 // based on phosphor-network responses
2374 messages::internalError(asyncResp->res);
2375 return;
2376 }
2377 messages::created(asyncResp->res);
2378 };
2379 crow::connections::systemBus->async_method_call(
2380 std::move(callback), "xyz.openbmc_project.Network",
2381 "/xyz/openbmc_project/network",
2382 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2383 rootInterfaceName, vlanId);
2384 });
2385}
2386
Ed Tanous1abe55e2018-09-05 08:30:59 -07002387} // namespace redfish