blob: 49aa3db3885a73b71aa578fad0b438ff14dde64e [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 Tanous1abe55e2018-09-05 08:30:59 -070019#include <boost/container/flat_map.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070020#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020021#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020022#include <error_messages.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023#include <utils/json_utils.hpp>
24
Ed Tanousa24526d2018-12-10 15:17:59 -080025#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053026#include <regex>
Ed Tanousabf2add2019-01-22 16:40:12 -080027#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029namespace redfish
30{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010031
32/**
33 * DBus types primitives for several generic DBus interfaces
34 * TODO(Pawel) consider move this to separate file into boost::dbus
35 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070036using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080037 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
38 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010039
Ed Tanous4a0cb852018-10-15 07:55:04 -070040using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070042 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070043 std::string,
44 boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -050045 std::string,
46 std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
47 uint32_t, int64_t, uint64_t, double,
48 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070049
50enum class LinkType
51{
52 Local,
53 Global
54};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010055
56/**
57 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010058 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070059struct IPv4AddressData
60{
61 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070062 std::string address;
63 std::string domain;
64 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 std::string netmask;
66 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070067 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050068 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070069
Gunnar Mills1214b7e2020-06-04 10:11:30 -050070 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070072 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010074};
75
76/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050077 * Structure for keeping IPv6 data required by Redfish
78 */
79struct IPv6AddressData
80{
81 std::string id;
82 std::string address;
83 std::string origin;
84 uint8_t prefixLength;
85
Gunnar Mills1214b7e2020-06-04 10:11:30 -050086 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050087 {
88 return id < obj.id;
89 }
90};
91/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010092 * Structure for keeping basic single Ethernet Interface information
93 * available from DBus
94 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070095struct EthernetInterfaceData
96{
Ed Tanous4a0cb852018-10-15 07:55:04 -070097 uint32_t speed;
98 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070099 bool DNSEnabled;
100 bool NTPEnabled;
101 bool HostNameEnabled;
102 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800103 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700104 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700105 std::string DHCPEnabled;
106 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700107 std::string hostname;
108 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500109 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700110 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500111 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500112 std::vector<std::string> nameServers;
113 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800114 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100115};
116
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700117struct DHCPParameters
118{
119 std::optional<bool> dhcpv4Enabled;
120 std::optional<bool> useDNSServers;
121 std::optional<bool> useNTPServers;
122 std::optional<bool> useUseDomainName;
123 std::optional<std::string> dhcpv6OperatingMode;
124};
125
Ed Tanous4a0cb852018-10-15 07:55:04 -0700126// Helper function that changes bits netmask notation (i.e. /24)
127// into full dot notation
128inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700130 uint32_t value = 0xffffffff << (32 - bits);
131 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
132 std::to_string((value >> 16) & 0xff) + "." +
133 std::to_string((value >> 8) & 0xff) + "." +
134 std::to_string(value & 0xff);
135 return netmask;
136}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100137
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500138inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700139 bool isIPv4)
140{
141 if (isIPv4)
142 {
143 return (
144 (inputDHCP ==
145 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
146 (inputDHCP ==
147 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
148 }
149 return ((inputDHCP ==
150 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
151 (inputDHCP ==
152 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
153}
154
Ed Tanous2c70f802020-09-28 14:29:23 -0700155inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700156{
157 if (isIPv4 && isIPv6)
158 {
159 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
160 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700161 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700162 {
163 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
164 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700165 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700166 {
167 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
168 }
169 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
170}
171
Ed Tanous4a0cb852018-10-15 07:55:04 -0700172inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500173 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700174 bool isIPv4)
175{
176 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 return "Static";
179 }
180 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
181 {
182 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700183 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700184 return "IPv4LinkLocal";
185 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700186 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700187 }
188 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
189 {
190 if (isIPv4)
191 {
192 return "DHCP";
193 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700194 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700195 }
196 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
197 {
198 return "SLAAC";
199 }
200 return "";
201}
202
Ed Tanous81ce6092020-12-17 16:54:55 +0000203inline bool extractEthernetInterfaceData(const std::string& ethifaceId,
204 GetManagedObjects& dbusData,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700206{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700207 bool idFound = false;
Ed Tanous81ce6092020-12-17 16:54:55 +0000208 for (auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700209 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700210 for (auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000212 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700214 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500217 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500221 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800222 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 if (mac != nullptr)
224 {
225 ethData.mac_address = *mac;
226 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700228 }
229 }
230 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
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 == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500236 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800237 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700238 if (id != nullptr)
239 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500240 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 }
244 }
245 else if (ifacePair.first ==
246 "xyz.openbmc_project.Network.EthernetInterface")
247 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500248 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700249 {
250 if (propertyPair.first == "AutoNeg")
251 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700252 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800253 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700254 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700255 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700256 ethData.auto_neg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700257 }
258 }
259 else if (propertyPair.first == "Speed")
260 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500261 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800262 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700263 if (speed != nullptr)
264 {
265 ethData.speed = *speed;
266 }
267 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800268 else if (propertyPair.first == "LinkUp")
269 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500270 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800271 std::get_if<bool>(&propertyPair.second);
272 if (linkUp != nullptr)
273 {
274 ethData.linkUp = *linkUp;
275 }
276 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700277 else if (propertyPair.first == "NICEnabled")
278 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500279 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700280 std::get_if<bool>(&propertyPair.second);
281 if (nicEnabled != nullptr)
282 {
283 ethData.nicEnabled = *nicEnabled;
284 }
285 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500286 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700287 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500289 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800290 &propertyPair.second);
291 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700292 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700293 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500294 }
295 }
296 else if (propertyPair.first == "StaticNameServers")
297 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500298 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500299 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500300 &propertyPair.second);
301 if (staticNameServers != nullptr)
302 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700303 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700304 }
305 }
manojkiraneda2a133282019-02-19 13:09:43 +0530306 else if (propertyPair.first == "DHCPEnabled")
307 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700308 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700309 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700310 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530311 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700312 ethData.DHCPEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530313 }
314 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800315 else if (propertyPair.first == "DomainName")
316 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500318 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800319 &propertyPair.second);
320 if (domainNames != nullptr)
321 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700322 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800323 }
324 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500325 else if (propertyPair.first == "DefaultGateway")
326 {
327 const std::string* defaultGateway =
328 std::get_if<std::string>(&propertyPair.second);
329 if (defaultGateway != nullptr)
330 {
331 std::string defaultGatewayStr = *defaultGateway;
332 if (defaultGatewayStr.empty())
333 {
334 ethData.default_gateway = "0.0.0.0";
335 }
336 else
337 {
338 ethData.default_gateway = defaultGatewayStr;
339 }
340 }
341 }
342 else if (propertyPair.first == "DefaultGateway6")
343 {
344 const std::string* defaultGateway6 =
345 std::get_if<std::string>(&propertyPair.second);
346 if (defaultGateway6 != nullptr)
347 {
348 std::string defaultGateway6Str =
349 *defaultGateway6;
350 if (defaultGateway6Str.empty())
351 {
352 ethData.ipv6_default_gateway =
353 "0:0:0:0:0:0:0:0";
354 }
355 else
356 {
357 ethData.ipv6_default_gateway =
358 defaultGateway6Str;
359 }
360 }
361 }
Ed Tanous029573d2019-02-01 10:57:49 -0800362 }
363 }
364 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700365
366 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
367 {
368 if (ifacePair.first ==
369 "xyz.openbmc_project.Network.DHCPConfiguration")
370 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500371 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700372 {
373 if (propertyPair.first == "DNSEnabled")
374 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700375 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700376 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700377 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700378 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700379 ethData.DNSEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700380 }
381 }
382 else if (propertyPair.first == "NTPEnabled")
383 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700384 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700385 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700386 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700387 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700388 ethData.NTPEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700389 }
390 }
391 else if (propertyPair.first == "HostNameEnabled")
392 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700393 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700394 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700395 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700396 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700397 ethData.HostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700398 }
399 }
400 else if (propertyPair.first == "SendHostNameEnabled")
401 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700402 const bool* sendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700403 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700404 if (sendHostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700405 {
406 ethData.SendHostNameEnabled =
Ed Tanous2c70f802020-09-28 14:29:23 -0700407 *sendHostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700408 }
409 }
410 }
411 }
412 }
Ed Tanous029573d2019-02-01 10:57:49 -0800413 // System configuration shows up in the global namespace, so no need
414 // to check eth number
415 if (ifacePair.first ==
416 "xyz.openbmc_project.Network.SystemConfiguration")
417 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500418 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800419 {
420 if (propertyPair.first == "HostName")
421 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500422 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500423 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800424 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700425 {
Ed Tanous029573d2019-02-01 10:57:49 -0800426 ethData.hostname = *hostname;
427 }
428 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700429 }
430 }
431 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700432 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700433 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700434}
435
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500436// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700437inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000438 extractIPV6Data(const std::string& ethifaceId,
439 const GetManagedObjects& dbusData,
440 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500441{
442 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000443 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500444
445 // Since there might be several IPv6 configurations aligned with
446 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000447 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500448 {
449 // Check if proper pattern for object path appears
450 if (boost::starts_with(objpath.first.str, ipv6PathStart))
451 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500452 for (auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500453 {
454 if (interface.first == "xyz.openbmc_project.Network.IP")
455 {
456 // Instance IPv6AddressData structure, and set as
457 // appropriate
458 std::pair<
459 boost::container::flat_set<IPv6AddressData>::iterator,
460 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000461 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700462 IPv6AddressData& ipv6Address = *it.first;
463 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700464 objpath.first.str.substr(ipv6PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500465 for (auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500466 {
467 if (property.first == "Address")
468 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500469 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500470 std::get_if<std::string>(&property.second);
471 if (address != nullptr)
472 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700473 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500474 }
475 }
476 else if (property.first == "Origin")
477 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500478 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500479 std::get_if<std::string>(&property.second);
480 if (origin != nullptr)
481 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700482 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500483 translateAddressOriginDbusToRedfish(*origin,
484 false);
485 }
486 }
487 else if (property.first == "PrefixLength")
488 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500489 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500490 std::get_if<uint8_t>(&property.second);
491 if (prefix != nullptr)
492 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700493 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500494 }
495 }
496 else
497 {
498 BMCWEB_LOG_ERROR
499 << "Got extra property: " << property.first
500 << " on the " << objpath.first.str << " object";
501 }
502 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500503 }
504 }
505 }
506 }
507}
508
Ed Tanous4a0cb852018-10-15 07:55:04 -0700509// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700510inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000511 extractIPData(const std::string& ethifaceId,
512 const GetManagedObjects& dbusData,
513 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700514{
515 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000516 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700517
518 // Since there might be several IPv4 configurations aligned with
519 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000520 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700521 {
522 // Check if proper pattern for object path appears
523 if (boost::starts_with(objpath.first.str, ipv4PathStart))
524 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500525 for (auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700526 {
527 if (interface.first == "xyz.openbmc_project.Network.IP")
528 {
529 // Instance IPv4AddressData structure, and set as
530 // appropriate
531 std::pair<
532 boost::container::flat_set<IPv4AddressData>::iterator,
533 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000534 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700535 IPv4AddressData& ipv4Address = *it.first;
536 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700537 objpath.first.str.substr(ipv4PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500538 for (auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700539 {
540 if (property.first == "Address")
541 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500542 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800543 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700544 if (address != nullptr)
545 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700546 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700547 }
548 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700549 else if (property.first == "Origin")
550 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500551 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800552 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700553 if (origin != nullptr)
554 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700555 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700556 translateAddressOriginDbusToRedfish(*origin,
557 true);
558 }
559 }
560 else if (property.first == "PrefixLength")
561 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500562 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800563 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700564 if (mask != nullptr)
565 {
566 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700567 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700568 }
569 }
570 else
571 {
572 BMCWEB_LOG_ERROR
573 << "Got extra property: " << property.first
574 << " on the " << objpath.first.str << " object";
575 }
576 }
577 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700578 ipv4Address.linktype =
579 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700580 ? LinkType::Local
581 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700582 }
583 }
584 }
585 }
586}
587
588/**
589 * @brief Sets given Id on the given VLAN interface through D-Bus
590 *
591 * @param[in] ifaceId Id of VLAN interface that should be modified
592 * @param[in] inputVlanId New ID of the VLAN
593 * @param[in] callback Function that will be called after the operation
594 *
595 * @return None.
596 */
597template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500598void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
599 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700600{
601 crow::connections::systemBus->async_method_call(
602 callback, "xyz.openbmc_project.Network",
603 std::string("/xyz/openbmc_project/network/") + ifaceId,
604 "org.freedesktop.DBus.Properties", "Set",
605 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800606 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700607}
608
609/**
610 * @brief Helper function that verifies IP address to check if it is in
611 * proper format. If bits pointer is provided, also calculates active
612 * bit count for Subnet Mask.
613 *
614 * @param[in] ip IP that will be verified
615 * @param[out] bits Calculated mask in bits notation
616 *
617 * @return true in case of success, false otherwise
618 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500619inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
620 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700621{
622 std::vector<std::string> bytesInMask;
623
624 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625
626 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700627 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700629 return false;
630 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700633 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700634 *bits = 0;
635 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500637 char* endPtr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 long previousValue = 255;
639 bool firstZeroInByteHit;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500640 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700641 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700642 if (byte.empty())
643 {
644 return false;
645 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646
Ed Tanous4a0cb852018-10-15 07:55:04 -0700647 // Use strtol instead of stroi to avoid exceptions
648 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649
Ed Tanous4a0cb852018-10-15 07:55:04 -0700650 // endPtr should point to the end of the string, otherwise given string
651 // is not 100% number
652 if (*endPtr != '\0')
653 {
654 return false;
655 }
656
657 // Value should be contained in byte
658 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 {
660 return false;
661 }
662
663 if (bits != nullptr)
664 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700665 // Mask has to be continuous between bytes
666 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700667 {
668 return false;
669 }
670
Ed Tanous4a0cb852018-10-15 07:55:04 -0700671 // Mask has to be continuous inside bytes
672 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700673
Ed Tanous4a0cb852018-10-15 07:55:04 -0700674 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000675 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000677 if (value & (1L << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700678 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700680 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700681 // Continuity not preserved
682 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700684 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686 else
687 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700688 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700690 }
691 }
692
693 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700694 }
695
Ed Tanous4a0cb852018-10-15 07:55:04 -0700696 return true;
697}
698
699/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700700 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700701 *
702 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700703 * @param[in] ipHash DBus Hash id of IP that should be deleted
704 * @param[io] asyncResp Response object that will be returned to client
705 *
706 * @return None
707 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500708inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800709 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700710{
711 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700712 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700713 if (ec)
714 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800715 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100716 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700717 },
718 "xyz.openbmc_project.Network",
719 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
720 "xyz.openbmc_project.Object.Delete", "Delete");
721}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700722
Gunnar Mills244b6d52021-04-12 15:44:23 -0500723inline void updateIPv4DefaultGateway(
724 const std::string& ifaceId, const std::string& gateway,
725 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500726{
727 crow::connections::systemBus->async_method_call(
728 [asyncResp](const boost::system::error_code ec) {
729 if (ec)
730 {
731 messages::internalError(asyncResp->res);
732 return;
733 }
734 asyncResp->res.result(boost::beast::http::status::no_content);
735 },
736 "xyz.openbmc_project.Network",
737 "/xyz/openbmc_project/network/" + ifaceId,
738 "org.freedesktop.DBus.Properties", "Set",
739 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
740 std::variant<std::string>(gateway));
741}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700742/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700743 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700744 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700745 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
746 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
747 * @param[in] gateway IPv4 address of this interfaces gateway
748 * @param[in] address IPv4 address to assign to this interface
749 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700750 *
751 * @return None
752 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000753inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
754 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800755 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700756{
Ravi Teja9010ec22019-08-01 23:30:25 -0500757 auto createIpHandler = [asyncResp, ifaceId,
758 gateway](const boost::system::error_code ec) {
759 if (ec)
760 {
761 messages::internalError(asyncResp->res);
762 return;
763 }
764 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
765 };
766
Ed Tanous4a0cb852018-10-15 07:55:04 -0700767 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500768 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700769 "/xyz/openbmc_project/network/" + ifaceId,
770 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700771 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700772 gateway);
773}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500774
775/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700776 * @brief Deletes the IPv4 entry for this interface and creates a replacement
777 * static IPv4 entry
778 *
779 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
780 * @param[in] id The unique hash entry identifying the DBus entry
781 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
782 * @param[in] gateway IPv4 address of this interfaces gateway
783 * @param[in] address IPv4 address to assign to this interface
784 * @param[io] asyncResp Response object that will be returned to client
785 *
786 * @return None
787 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800788inline void
789 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
790 uint8_t prefixLength, const std::string& gateway,
791 const std::string& address,
792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700793{
794 crow::connections::systemBus->async_method_call(
795 [asyncResp, ifaceId, address, prefixLength,
796 gateway](const boost::system::error_code ec) {
797 if (ec)
798 {
799 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500800 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700801 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500802
Johnathan Mantey01784822019-06-18 12:44:21 -0700803 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500804 [asyncResp, ifaceId,
805 gateway](const boost::system::error_code ec2) {
Ed Tanous23a21a12020-07-25 04:45:05 +0000806 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700807 {
808 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500809 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700810 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500811 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -0700812 },
813 "xyz.openbmc_project.Network",
814 "/xyz/openbmc_project/network/" + ifaceId,
815 "xyz.openbmc_project.Network.IP.Create", "IP",
816 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
817 prefixLength, gateway);
818 },
819 "xyz.openbmc_project.Network",
820 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
821 "xyz.openbmc_project.Object.Delete", "Delete");
822}
823
824/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500825 * @brief Deletes given IPv6
826 *
827 * @param[in] ifaceId Id of interface whose IP should be deleted
828 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500829 * @param[io] asyncResp Response object that will be returned to client
830 *
831 * @return None
832 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500833inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800834 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500835{
836 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700837 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500838 if (ec)
839 {
840 messages::internalError(asyncResp->res);
841 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500842 },
843 "xyz.openbmc_project.Network",
844 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
845 "xyz.openbmc_project.Object.Delete", "Delete");
846}
847
848/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700849 * @brief Deletes the IPv6 entry for this interface and creates a replacement
850 * static IPv6 entry
851 *
852 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
853 * @param[in] id The unique hash entry identifying the DBus entry
854 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
855 * @param[in] address IPv6 address to assign to this interface
856 * @param[io] asyncResp Response object that will be returned to client
857 *
858 * @return None
859 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800860inline void
861 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
862 uint8_t prefixLength, const std::string& address,
863 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700864{
865 crow::connections::systemBus->async_method_call(
866 [asyncResp, ifaceId, address,
867 prefixLength](const boost::system::error_code ec) {
868 if (ec)
869 {
870 messages::internalError(asyncResp->res);
871 }
872 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000873 [asyncResp](const boost::system::error_code ec2) {
874 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700875 {
876 messages::internalError(asyncResp->res);
877 }
878 },
879 "xyz.openbmc_project.Network",
880 "/xyz/openbmc_project/network/" + ifaceId,
881 "xyz.openbmc_project.Network.IP.Create", "IP",
882 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
883 prefixLength, "");
884 },
885 "xyz.openbmc_project.Network",
886 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
887 "xyz.openbmc_project.Object.Delete", "Delete");
888}
889
890/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500891 * @brief Creates IPv6 with given data
892 *
893 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500894 * @param[in] prefixLength Prefix length that needs to be added
895 * @param[in] address IP address that needs to be added
896 * @param[io] asyncResp Response object that will be returned to client
897 *
898 * @return None
899 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500900inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
901 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800902 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500903{
904 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
905 if (ec)
906 {
907 messages::internalError(asyncResp->res);
908 }
909 };
910 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500911 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500912 crow::connections::systemBus->async_method_call(
913 std::move(createIpHandler), "xyz.openbmc_project.Network",
914 "/xyz/openbmc_project/network/" + ifaceId,
915 "xyz.openbmc_project.Network.IP.Create", "IP",
916 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
917 "");
918}
919
Ed Tanous4a0cb852018-10-15 07:55:04 -0700920/**
921 * Function that retrieves all properties for given Ethernet Interface
922 * Object
923 * from EntityManager Network Manager
924 * @param ethiface_id a eth interface id to query on DBus
925 * @param callback a function that shall be called to convert Dbus output
926 * into JSON
927 */
928template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000929void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500930 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700931{
932 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +0000933 [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
934 const boost::system::error_code errorCode,
Ed Tanousf23b7292020-10-15 09:41:17 -0700935 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700936 EthernetInterfaceData ethData{};
937 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500938 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700939
Ed Tanous81ce6092020-12-17 16:54:55 +0000940 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700941 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700942 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700943 return;
944 }
945
Ed Tanous4c9afe42019-05-03 16:59:57 -0700946 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700947 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700948 if (!found)
949 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700950 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700951 return;
952 }
953
Ed Tanous2c70f802020-09-28 14:29:23 -0700954 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700955 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500956 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700957 {
Ravi Tejac6191412019-07-30 00:53:50 -0500958 if (((ipv4.linktype == LinkType::Global) &&
959 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500960 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 ipv4.gateway = ethData.default_gateway;
963 }
964 }
965
Ed Tanous2c70f802020-09-28 14:29:23 -0700966 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500967 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700968 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700969 },
970 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
971 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700972}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700973
974/**
975 * Function that retrieves all Ethernet Interfaces available through Network
976 * Manager
977 * @param callback a function that shall be called to convert Dbus output
978 * into JSON.
979 */
980template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500981void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700982{
983 crow::connections::systemBus->async_method_call(
984 [callback{std::move(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000985 const boost::system::error_code errorCode,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500986 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700987 // Callback requires vector<string> to retrieve all available
988 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -0700989 boost::container::flat_set<std::string> ifaceList;
990 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +0000991 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700992 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700993 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700994 return;
995 }
996
997 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500998 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700999 {
1000 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001001 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001002 {
1003 // If interface is
1004 // xyz.openbmc_project.Network.EthernetInterface, this is
1005 // what we're looking for.
1006 if (interface.first ==
1007 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001009 std::string ifaceId = objpath.first.filename();
1010 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001012 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001013 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001014 // and put it into output vector.
1015 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001016 }
1017 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001018 }
1019 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -07001020 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001021 },
1022 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1023 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001024}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001025
Ed Tanousbf648f72021-06-03 15:00:14 -07001026void handleHostnamePatch(const std::string& hostname,
1027 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001028{
Ed Tanousbf648f72021-06-03 15:00:14 -07001029 // SHOULD handle host names of up to 255 characters(RFC 1123)
1030 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001031 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001032 messages::propertyValueFormatError(asyncResp->res, hostname,
1033 "HostName");
1034 return;
1035 }
1036 crow::connections::systemBus->async_method_call(
1037 [asyncResp](const boost::system::error_code ec) {
1038 if (ec)
1039 {
1040 messages::internalError(asyncResp->res);
1041 }
1042 },
1043 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1044 "org.freedesktop.DBus.Properties", "Set",
1045 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1046 std::variant<std::string>(hostname));
1047}
1048
1049void handleDomainnamePatch(const std::string& ifaceId,
1050 const std::string& domainname,
1051 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1052{
1053 std::vector<std::string> vectorDomainname = {domainname};
1054 crow::connections::systemBus->async_method_call(
1055 [asyncResp](const boost::system::error_code ec) {
1056 if (ec)
1057 {
1058 messages::internalError(asyncResp->res);
1059 }
1060 },
1061 "xyz.openbmc_project.Network",
1062 "/xyz/openbmc_project/network/" + ifaceId,
1063 "org.freedesktop.DBus.Properties", "Set",
1064 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1065 std::variant<std::vector<std::string>>(vectorDomainname));
1066}
1067
1068bool isHostnameValid(const std::string& hostname)
1069{
1070 // A valid host name can never have the dotted-decimal form (RFC 1123)
1071 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1072 {
1073 return false;
1074 }
1075 // Each label(hostname/subdomains) within a valid FQDN
1076 // MUST handle host names of up to 63 characters (RFC 1123)
1077 // labels cannot start or end with hyphens (RFC 952)
1078 // labels can start with numbers (RFC 1123)
1079 const std::regex pattern(
1080 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1081
1082 return std::regex_match(hostname, pattern);
1083}
1084
1085bool isDomainnameValid(const std::string& domainname)
1086{
1087 // Can have multiple subdomains
1088 // Top Level Domain's min length is 2 character
1089 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1090 "{1,30}\\.)*[a-zA-Z]{2,}$");
1091
1092 return std::regex_match(domainname, pattern);
1093}
1094
1095void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1096 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1097{
1098 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1099 if (fqdn.length() > 255)
1100 {
1101 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1102 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001103 }
1104
Ed Tanousbf648f72021-06-03 15:00:14 -07001105 size_t pos = fqdn.find('.');
1106 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001107 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001108 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1109 return;
1110 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001111
Ed Tanousbf648f72021-06-03 15:00:14 -07001112 std::string hostname;
1113 std::string domainname;
1114 domainname = (fqdn).substr(pos + 1);
1115 hostname = (fqdn).substr(0, pos);
1116
1117 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1118 {
1119 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1120 return;
1121 }
1122
1123 handleHostnamePatch(hostname, asyncResp);
1124 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1125}
1126
1127void handleMACAddressPatch(const std::string& ifaceId,
1128 const std::string& macAddress,
1129 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1130{
1131 crow::connections::systemBus->async_method_call(
1132 [asyncResp, macAddress](const boost::system::error_code ec) {
1133 if (ec)
1134 {
1135 messages::internalError(asyncResp->res);
1136 return;
1137 }
1138 },
1139 "xyz.openbmc_project.Network",
1140 "/xyz/openbmc_project/network/" + ifaceId,
1141 "org.freedesktop.DBus.Properties", "Set",
1142 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1143 std::variant<std::string>(macAddress));
1144}
1145
1146void setDHCPEnabled(const std::string& ifaceId, const std::string& propertyName,
1147 const bool v4Value, const bool v6Value,
1148 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1149{
1150 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1151 crow::connections::systemBus->async_method_call(
1152 [asyncResp](const boost::system::error_code ec) {
1153 if (ec)
1154 {
1155 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1156 messages::internalError(asyncResp->res);
1157 return;
1158 }
1159 messages::success(asyncResp->res);
1160 },
1161 "xyz.openbmc_project.Network",
1162 "/xyz/openbmc_project/network/" + ifaceId,
1163 "org.freedesktop.DBus.Properties", "Set",
1164 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1165 std::variant<std::string>{dhcp});
1166}
1167
1168void setEthernetInterfaceBoolProperty(
1169 const std::string& ifaceId, const std::string& propertyName,
1170 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1171{
1172 crow::connections::systemBus->async_method_call(
1173 [asyncResp](const boost::system::error_code ec) {
1174 if (ec)
1175 {
1176 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1177 messages::internalError(asyncResp->res);
1178 return;
1179 }
1180 },
1181 "xyz.openbmc_project.Network",
1182 "/xyz/openbmc_project/network/" + ifaceId,
1183 "org.freedesktop.DBus.Properties", "Set",
1184 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1185 std::variant<bool>{value});
1186}
1187
1188void setDHCPv4Config(const std::string& propertyName, const bool& value,
1189 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1190{
1191 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1192 crow::connections::systemBus->async_method_call(
1193 [asyncResp](const boost::system::error_code ec) {
1194 if (ec)
1195 {
1196 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1197 messages::internalError(asyncResp->res);
1198 return;
1199 }
1200 },
1201 "xyz.openbmc_project.Network",
1202 "/xyz/openbmc_project/network/config/dhcp",
1203 "org.freedesktop.DBus.Properties", "Set",
1204 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1205 std::variant<bool>{value});
1206}
1207
1208void handleDHCPPatch(const std::string& ifaceId,
1209 const EthernetInterfaceData& ethData,
1210 const DHCPParameters& v4dhcpParms,
1211 const DHCPParameters& v6dhcpParms,
1212 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1213{
1214 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1215 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1216
1217 bool nextv4DHCPState =
1218 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1219
1220 bool nextv6DHCPState{};
1221 if (v6dhcpParms.dhcpv6OperatingMode)
1222 {
1223 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1224 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1225 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1226 {
1227 messages::propertyValueFormatError(asyncResp->res,
1228 *v6dhcpParms.dhcpv6OperatingMode,
1229 "OperatingMode");
1230 return;
1231 }
1232 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1233 }
1234 else
1235 {
1236 nextv6DHCPState = ipv6Active;
1237 }
1238
1239 bool nextDNS{};
1240 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1241 {
1242 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1243 {
1244 messages::generalError(asyncResp->res);
1245 return;
1246 }
1247 nextDNS = *v4dhcpParms.useDNSServers;
1248 }
1249 else if (v4dhcpParms.useDNSServers)
1250 {
1251 nextDNS = *v4dhcpParms.useDNSServers;
1252 }
1253 else if (v6dhcpParms.useDNSServers)
1254 {
1255 nextDNS = *v6dhcpParms.useDNSServers;
1256 }
1257 else
1258 {
1259 nextDNS = ethData.DNSEnabled;
1260 }
1261
1262 bool nextNTP{};
1263 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1264 {
1265 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1266 {
1267 messages::generalError(asyncResp->res);
1268 return;
1269 }
1270 nextNTP = *v4dhcpParms.useNTPServers;
1271 }
1272 else if (v4dhcpParms.useNTPServers)
1273 {
1274 nextNTP = *v4dhcpParms.useNTPServers;
1275 }
1276 else if (v6dhcpParms.useNTPServers)
1277 {
1278 nextNTP = *v6dhcpParms.useNTPServers;
1279 }
1280 else
1281 {
1282 nextNTP = ethData.NTPEnabled;
1283 }
1284
1285 bool nextUseDomain{};
1286 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1287 {
1288 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1289 {
1290 messages::generalError(asyncResp->res);
1291 return;
1292 }
1293 nextUseDomain = *v4dhcpParms.useUseDomainName;
1294 }
1295 else if (v4dhcpParms.useUseDomainName)
1296 {
1297 nextUseDomain = *v4dhcpParms.useUseDomainName;
1298 }
1299 else if (v6dhcpParms.useUseDomainName)
1300 {
1301 nextUseDomain = *v6dhcpParms.useUseDomainName;
1302 }
1303 else
1304 {
1305 nextUseDomain = ethData.HostNameEnabled;
1306 }
1307
1308 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1309 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1310 asyncResp);
1311 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1312 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1313 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1314 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1315 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1316 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1317}
1318
1319boost::container::flat_set<IPv4AddressData>::const_iterator
1320 getNextStaticIpEntry(
1321 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1322 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1323{
1324 return std::find_if(head, end, [](const IPv4AddressData& value) {
1325 return value.origin == "Static";
1326 });
1327}
1328
1329boost::container::flat_set<IPv6AddressData>::const_iterator
1330 getNextStaticIpEntry(
1331 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1332 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1333{
1334 return std::find_if(head, end, [](const IPv6AddressData& value) {
1335 return value.origin == "Static";
1336 });
1337}
1338
1339void handleIPv4StaticPatch(
1340 const std::string& ifaceId, nlohmann::json& input,
1341 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1342 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1343{
1344 if ((!input.is_array()) || input.empty())
1345 {
1346 messages::propertyValueTypeError(
1347 asyncResp->res,
1348 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1349 "IPv4StaticAddresses");
1350 return;
1351 }
1352
1353 unsigned entryIdx = 1;
1354 // Find the first static IP address currently active on the NIC and
1355 // match it to the first JSON element in the IPv4StaticAddresses array.
1356 // Match each subsequent JSON element to the next static IP programmed
1357 // into the NIC.
1358 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
1359 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1360
1361 for (nlohmann::json& thisJson : input)
1362 {
1363 std::string pathString =
1364 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1365
1366 if (!thisJson.is_null() && !thisJson.empty())
1367 {
1368 std::optional<std::string> address;
1369 std::optional<std::string> subnetMask;
1370 std::optional<std::string> gateway;
1371
1372 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1373 address, "SubnetMask", subnetMask,
1374 "Gateway", gateway))
1375 {
1376 messages::propertyValueFormatError(
1377 asyncResp->res,
1378 thisJson.dump(2, ' ', true,
1379 nlohmann::json::error_handler_t::replace),
1380 pathString);
1381 return;
1382 }
1383
1384 // Find the address/subnet/gateway values. Any values that are
1385 // not explicitly provided are assumed to be unmodified from the
1386 // current state of the interface. Merge existing state into the
1387 // current request.
1388 const std::string* addr = nullptr;
1389 const std::string* gw = nullptr;
1390 uint8_t prefixLength = 0;
1391 bool errorInEntry = false;
1392 if (address)
1393 {
1394 if (ipv4VerifyIpAndGetBitcount(*address))
1395 {
1396 addr = &(*address);
1397 }
1398 else
1399 {
1400 messages::propertyValueFormatError(asyncResp->res, *address,
1401 pathString + "/Address");
1402 errorInEntry = true;
1403 }
1404 }
1405 else if (niciPentry != ipv4Data.cend())
1406 {
1407 addr = &(niciPentry->address);
1408 }
1409 else
1410 {
1411 messages::propertyMissing(asyncResp->res,
1412 pathString + "/Address");
1413 errorInEntry = true;
1414 }
1415
1416 if (subnetMask)
1417 {
1418 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1419 {
1420 messages::propertyValueFormatError(
1421 asyncResp->res, *subnetMask,
1422 pathString + "/SubnetMask");
1423 errorInEntry = true;
1424 }
1425 }
1426 else if (niciPentry != ipv4Data.cend())
1427 {
1428 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
1429 &prefixLength))
1430 {
1431 messages::propertyValueFormatError(
1432 asyncResp->res, niciPentry->netmask,
1433 pathString + "/SubnetMask");
1434 errorInEntry = true;
1435 }
1436 }
1437 else
1438 {
1439 messages::propertyMissing(asyncResp->res,
1440 pathString + "/SubnetMask");
1441 errorInEntry = true;
1442 }
1443
1444 if (gateway)
1445 {
1446 if (ipv4VerifyIpAndGetBitcount(*gateway))
1447 {
1448 gw = &(*gateway);
1449 }
1450 else
1451 {
1452 messages::propertyValueFormatError(asyncResp->res, *gateway,
1453 pathString + "/Gateway");
1454 errorInEntry = true;
1455 }
1456 }
1457 else if (niciPentry != ipv4Data.cend())
1458 {
1459 gw = &niciPentry->gateway;
1460 }
1461 else
1462 {
1463 messages::propertyMissing(asyncResp->res,
1464 pathString + "/Gateway");
1465 errorInEntry = true;
1466 }
1467
1468 if (errorInEntry)
1469 {
1470 return;
1471 }
1472
1473 if (niciPentry != ipv4Data.cend())
1474 {
1475 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, *gw,
1476 *addr, asyncResp);
1477 niciPentry =
1478 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1479 }
1480 else
1481 {
1482 createIPv4(ifaceId, prefixLength, *gateway, *address,
1483 asyncResp);
1484 }
1485 entryIdx++;
1486 }
1487 else
1488 {
1489 if (niciPentry == ipv4Data.cend())
1490 {
1491 // Requesting a DELETE/DO NOT MODIFY action for an item
1492 // that isn't present on the eth(n) interface. Input JSON is
1493 // in error, so bail out.
1494 if (thisJson.is_null())
1495 {
1496 messages::resourceCannotBeDeleted(asyncResp->res);
1497 return;
1498 }
1499 messages::propertyValueFormatError(
1500 asyncResp->res,
1501 thisJson.dump(2, ' ', true,
1502 nlohmann::json::error_handler_t::replace),
1503 pathString);
1504 return;
1505 }
1506
1507 if (thisJson.is_null())
1508 {
1509 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
1510 }
1511 if (niciPentry != ipv4Data.cend())
1512 {
1513 niciPentry =
1514 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1515 }
1516 entryIdx++;
1517 }
1518 }
1519}
1520
1521void handleStaticNameServersPatch(
1522 const std::string& ifaceId,
1523 const std::vector<std::string>& updatedStaticNameServers,
1524 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1525{
1526 crow::connections::systemBus->async_method_call(
1527 [asyncResp](const boost::system::error_code ec) {
1528 if (ec)
1529 {
1530 messages::internalError(asyncResp->res);
1531 return;
1532 }
1533 },
1534 "xyz.openbmc_project.Network",
1535 "/xyz/openbmc_project/network/" + ifaceId,
1536 "org.freedesktop.DBus.Properties", "Set",
1537 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1538 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1539}
1540
1541void handleIPv6StaticAddressesPatch(
1542 const std::string& ifaceId, const nlohmann::json& input,
1543 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1544 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1545{
1546 if (!input.is_array() || input.empty())
1547 {
1548 messages::propertyValueTypeError(
1549 asyncResp->res,
1550 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1551 "IPv6StaticAddresses");
1552 return;
1553 }
1554 size_t entryIdx = 1;
1555 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
1556 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1557 for (const nlohmann::json& thisJson : input)
1558 {
1559 std::string pathString =
1560 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1561
1562 if (!thisJson.is_null() && !thisJson.empty())
1563 {
1564 std::optional<std::string> address;
1565 std::optional<uint8_t> prefixLength;
1566 nlohmann::json thisJsonCopy = thisJson;
1567 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1568 address, "PrefixLength", prefixLength))
1569 {
1570 messages::propertyValueFormatError(
1571 asyncResp->res,
1572 thisJson.dump(2, ' ', true,
1573 nlohmann::json::error_handler_t::replace),
1574 pathString);
1575 return;
1576 }
1577
1578 const std::string* addr;
1579 uint8_t prefix;
1580
1581 // Find the address and prefixLength values. Any values that are
1582 // not explicitly provided are assumed to be unmodified from the
1583 // current state of the interface. Merge existing state into the
1584 // current request.
1585 if (address)
1586 {
1587 addr = &(*address);
1588 }
1589 else if (niciPentry != ipv6Data.end())
1590 {
1591 addr = &(niciPentry->address);
1592 }
1593 else
1594 {
1595 messages::propertyMissing(asyncResp->res,
1596 pathString + "/Address");
1597 return;
1598 }
1599
1600 if (prefixLength)
1601 {
1602 prefix = *prefixLength;
1603 }
1604 else if (niciPentry != ipv6Data.end())
1605 {
1606 prefix = niciPentry->prefixLength;
1607 }
1608 else
1609 {
1610 messages::propertyMissing(asyncResp->res,
1611 pathString + "/PrefixLength");
1612 return;
1613 }
1614
1615 if (niciPentry != ipv6Data.end())
1616 {
1617 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1618 asyncResp);
1619 niciPentry =
1620 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1621 }
1622 else
1623 {
1624 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1625 }
1626 entryIdx++;
1627 }
1628 else
1629 {
1630 if (niciPentry == ipv6Data.end())
1631 {
1632 // Requesting a DELETE/DO NOT MODIFY action for an item
1633 // that isn't present on the eth(n) interface. Input JSON is
1634 // in error, so bail out.
1635 if (thisJson.is_null())
1636 {
1637 messages::resourceCannotBeDeleted(asyncResp->res);
1638 return;
1639 }
1640 messages::propertyValueFormatError(
1641 asyncResp->res,
1642 thisJson.dump(2, ' ', true,
1643 nlohmann::json::error_handler_t::replace),
1644 pathString);
1645 return;
1646 }
1647
1648 if (thisJson.is_null())
1649 {
1650 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
1651 }
1652 if (niciPentry != ipv6Data.cend())
1653 {
1654 niciPentry =
1655 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1656 }
1657 entryIdx++;
1658 }
1659 }
1660}
1661
1662void parseInterfaceData(
1663 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1664 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1665 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1666 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1667{
1668 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1669 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1670
1671 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1672 jsonResponse["Id"] = ifaceId;
1673 jsonResponse["@odata.id"] =
1674 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1675 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1676
1677 auto health = std::make_shared<HealthPopulate>(asyncResp);
1678
1679 crow::connections::systemBus->async_method_call(
1680 [health](const boost::system::error_code ec,
1681 std::vector<std::string>& resp) {
1682 if (ec)
1683 {
1684 return;
1685 }
1686
1687 health->inventory = std::move(resp);
1688 },
1689 "xyz.openbmc_project.ObjectMapper",
1690 "/xyz/openbmc_project/object_mapper",
1691 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1692 inventoryForEthernet);
1693
1694 health->populate();
1695
1696 if (ethData.nicEnabled)
1697 {
1698 jsonResponse["LinkStatus"] = "LinkUp";
1699 jsonResponse["Status"]["State"] = "Enabled";
1700 }
1701 else
1702 {
1703 jsonResponse["LinkStatus"] = "NoLink";
1704 jsonResponse["Status"]["State"] = "Disabled";
1705 }
1706
1707 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1708 jsonResponse["SpeedMbps"] = ethData.speed;
1709 jsonResponse["MACAddress"] = ethData.mac_address;
1710 jsonResponse["DHCPv4"]["DHCPEnabled"] =
1711 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1712 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1713 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1714 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1715
1716 jsonResponse["DHCPv6"]["OperatingMode"] =
1717 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1718 : "Disabled";
1719 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1720 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1721 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1722
1723 if (!ethData.hostname.empty())
1724 {
1725 jsonResponse["HostName"] = ethData.hostname;
1726
1727 // When domain name is empty then it means, that it is a network
1728 // without domain names, and the host name itself must be treated as
1729 // FQDN
1730 std::string fqdn = ethData.hostname;
1731 if (!ethData.domainnames.empty())
1732 {
1733 fqdn += "." + ethData.domainnames[0];
1734 }
1735 jsonResponse["FQDN"] = fqdn;
1736 }
1737
1738 jsonResponse["VLANs"] = {
1739 {"@odata.id",
1740 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1741
1742 jsonResponse["NameServers"] = ethData.nameServers;
1743 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1744
1745 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1746 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1747 ipv4Array = nlohmann::json::array();
1748 ipv4StaticArray = nlohmann::json::array();
1749 for (auto& ipv4Config : ipv4Data)
1750 {
1751
1752 std::string gatewayStr = ipv4Config.gateway;
1753 if (gatewayStr.empty())
1754 {
1755 gatewayStr = "0.0.0.0";
1756 }
1757
1758 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1759 {"SubnetMask", ipv4Config.netmask},
1760 {"Address", ipv4Config.address},
1761 {"Gateway", gatewayStr}});
1762 if (ipv4Config.origin == "Static")
1763 {
1764 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1765 {"SubnetMask", ipv4Config.netmask},
1766 {"Address", ipv4Config.address},
1767 {"Gateway", gatewayStr}});
1768 }
1769 }
1770
1771 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1772 if (ipv6GatewayStr.empty())
1773 {
1774 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1775 }
1776
1777 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1778
1779 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1780 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1781 ipv6Array = nlohmann::json::array();
1782 ipv6StaticArray = nlohmann::json::array();
1783 nlohmann::json& ipv6AddrPolicyTable =
1784 jsonResponse["IPv6AddressPolicyTable"];
1785 ipv6AddrPolicyTable = nlohmann::json::array();
1786 for (auto& ipv6Config : ipv6Data)
1787 {
1788 ipv6Array.push_back({{"Address", ipv6Config.address},
1789 {"PrefixLength", ipv6Config.prefixLength},
1790 {"AddressOrigin", ipv6Config.origin},
1791 {"AddressState", nullptr}});
1792 if (ipv6Config.origin == "Static")
1793 {
1794 ipv6StaticArray.push_back(
1795 {{"Address", ipv6Config.address},
1796 {"PrefixLength", ipv6Config.prefixLength},
1797 {"AddressOrigin", ipv6Config.origin},
1798 {"AddressState", nullptr}});
1799 }
1800 }
1801}
1802
1803void parseInterfaceData(nlohmann::json& jsonResponse,
1804 const std::string& parentIfaceId,
1805 const std::string& ifaceId,
1806 const EthernetInterfaceData& ethData)
1807{
1808 // Fill out obvious data...
1809 jsonResponse["Id"] = ifaceId;
1810 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1811 parentIfaceId + "/VLANs/" + ifaceId;
1812
1813 jsonResponse["VLANEnable"] = true;
1814 if (!ethData.vlan_id.empty())
1815 {
1816 jsonResponse["VLANId"] = ethData.vlan_id.back();
1817 }
1818}
1819
1820bool verifyNames(const std::string& parent, const std::string& iface)
1821{
1822 if (!boost::starts_with(iface, parent + "_"))
1823 {
1824 return false;
1825 }
1826 return true;
1827}
1828
1829inline void requestEthernetInterfacesRoutes(App& app)
1830{
1831 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous432a8902021-06-14 15:28:56 -07001832 .privileges({{"Login"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07001833 .methods(
1834 boost::beast::http::verb::
1835 get)([](const crow::Request&,
1836 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1837 asyncResp->res.jsonValue["@odata.type"] =
1838 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1839 asyncResp->res.jsonValue["@odata.id"] =
1840 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1841 asyncResp->res.jsonValue["Name"] =
1842 "Ethernet Network Interface Collection";
1843 asyncResp->res.jsonValue["Description"] =
1844 "Collection of EthernetInterfaces for this Manager";
1845
1846 // Get eth interface list, and call the below callback for JSON
1847 // preparation
1848 getEthernetIfaceList([asyncResp](const bool& success,
1849 const boost::container::flat_set<
1850 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001851 if (!success)
1852 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001853 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001854 return;
1855 }
1856
Ed Tanous2c70f802020-09-28 14:29:23 -07001857 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001858 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001859 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001860 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001861 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001862 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001863 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001864 if (found == std::string::npos)
1865 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001866 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001867 {{"@odata.id",
1868 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001869 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001870 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001871 }
1872
Ed Tanous4c9afe42019-05-03 16:59:57 -07001873 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001874 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001875 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001876 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001877 });
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301878 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001879
Ed Tanousbf648f72021-06-03 15:00:14 -07001880 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanous432a8902021-06-14 15:28:56 -07001881 .privileges({{"Login"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07001882 .methods(boost::beast::http::verb::get)(
1883 [](const crow::Request&,
1884 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1885 const std::string& ifaceId) {
1886 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 Tanous432a8902021-06-14 15:28:56 -07001917 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07001918 .methods(boost::beast::http::verb::patch)(
1919 [](const crow::Request& req,
1920 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1921 const std::string& ifaceId) {
1922 std::optional<std::string> hostname;
1923 std::optional<std::string> fqdn;
1924 std::optional<std::string> macAddress;
1925 std::optional<std::string> ipv6DefaultGateway;
1926 std::optional<nlohmann::json> ipv4StaticAddresses;
1927 std::optional<nlohmann::json> ipv6StaticAddresses;
1928 std::optional<std::vector<std::string>> staticNameServers;
1929 std::optional<nlohmann::json> dhcpv4;
1930 std::optional<nlohmann::json> dhcpv6;
1931 std::optional<bool> interfaceEnabled;
1932 DHCPParameters v4dhcpParms;
1933 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001934
Ed Tanousbf648f72021-06-03 15:00:14 -07001935 if (!json_util::readJson(
1936 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1937 "IPv4StaticAddresses", ipv4StaticAddresses,
1938 "MACAddress", macAddress, "StaticNameServers",
1939 staticNameServers, "IPv6DefaultGateway",
1940 ipv6DefaultGateway, "IPv6StaticAddresses",
1941 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1942 "InterfaceEnabled", interfaceEnabled))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001943 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001944 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001945 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001946 if (dhcpv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001947 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001948 if (!json_util::readJson(
1949 *dhcpv4, asyncResp->res, "DHCPEnabled",
1950 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1951 v4dhcpParms.useDNSServers, "UseNTPServers",
1952 v4dhcpParms.useNTPServers, "UseDomainName",
1953 v4dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001954 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001955 return;
1956 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001957 }
1958
Ed Tanousbf648f72021-06-03 15:00:14 -07001959 if (dhcpv6)
Johnathan Mantey01784822019-06-18 12:44:21 -07001960 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001961 if (!json_util::readJson(
1962 *dhcpv6, asyncResp->res, "OperatingMode",
1963 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1964 v6dhcpParms.useDNSServers, "UseNTPServers",
1965 v6dhcpParms.useNTPServers, "UseDomainName",
1966 v6dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001967 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001968 return;
1969 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001970 }
1971
Ed Tanousbf648f72021-06-03 15:00:14 -07001972 // Get single eth interface data, and call the below callback
1973 // for JSON preparation
1974 getEthernetIfaceData(
1975 ifaceId,
1976 [asyncResp, ifaceId, hostname = std::move(hostname),
1977 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1978 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1979 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1980 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1981 staticNameServers = std::move(staticNameServers),
1982 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1983 v4dhcpParms = std::move(v4dhcpParms),
1984 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1985 const bool& success,
1986 const EthernetInterfaceData& ethData,
1987 const boost::container::flat_set<IPv4AddressData>&
1988 ipv4Data,
1989 const boost::container::flat_set<IPv6AddressData>&
1990 ipv6Data) {
1991 if (!success)
1992 {
1993 // ... otherwise return error
1994 // TODO(Pawel)consider distinguish between non
1995 // existing object, and other errors
1996 messages::resourceNotFound(
1997 asyncResp->res, "Ethernet Interface", ifaceId);
1998 return;
1999 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002000
Ed Tanousbf648f72021-06-03 15:00:14 -07002001 if (dhcpv4 || dhcpv6)
2002 {
2003 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2004 v6dhcpParms, asyncResp);
2005 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002006
Ed Tanousbf648f72021-06-03 15:00:14 -07002007 if (hostname)
2008 {
2009 handleHostnamePatch(*hostname, asyncResp);
2010 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002011
Ed Tanousbf648f72021-06-03 15:00:14 -07002012 if (fqdn)
2013 {
2014 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2015 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002016
Ed Tanousbf648f72021-06-03 15:00:14 -07002017 if (macAddress)
2018 {
2019 handleMACAddressPatch(ifaceId, *macAddress,
2020 asyncResp);
2021 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002022
Ed Tanousbf648f72021-06-03 15:00:14 -07002023 if (ipv4StaticAddresses)
2024 {
2025 // TODO(ed) for some reason the capture of
2026 // ipv4Addresses above is returning a const value,
2027 // not a non-const value. This doesn't really work
2028 // for us, as we need to be able to efficiently move
2029 // out the intermedia nlohmann::json objects. This
2030 // makes a copy of the structure, and operates on
2031 // that, but could be done more efficiently
2032 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2033 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2034 asyncResp);
2035 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002036
Ed Tanousbf648f72021-06-03 15:00:14 -07002037 if (staticNameServers)
2038 {
2039 handleStaticNameServersPatch(
2040 ifaceId, *staticNameServers, asyncResp);
2041 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002042
Ed Tanousbf648f72021-06-03 15:00:14 -07002043 if (ipv6DefaultGateway)
2044 {
2045 messages::propertyNotWritable(asyncResp->res,
2046 "IPv6DefaultGateway");
2047 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08002048
Ed Tanousbf648f72021-06-03 15:00:14 -07002049 if (ipv6StaticAddresses)
2050 {
2051 nlohmann::json ipv6Static = *ipv6StaticAddresses;
2052 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2053 ipv6Data, asyncResp);
2054 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002055
Ed Tanousbf648f72021-06-03 15:00:14 -07002056 if (interfaceEnabled)
2057 {
2058 setEthernetInterfaceBoolProperty(
2059 ifaceId, "NICEnabled", *interfaceEnabled,
2060 asyncResp);
2061 }
2062 });
2063 });
manojkiraneda2a133282019-02-19 13:09:43 +05302064
Ed Tanousbf648f72021-06-03 15:00:14 -07002065 BMCWEB_ROUTE(
2066 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanous432a8902021-06-14 15:28:56 -07002067 .privileges({{"Login"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002068 .methods(boost::beast::http::verb::get)(
2069 [](const crow::Request& /* req */,
2070 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2071 const std::string& parentIfaceId, const std::string& ifaceId) {
Ed Tanous0f74e642018-11-12 15:17:05 -08002072 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousbf648f72021-06-03 15:00:14 -07002073 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2074 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002075
Ed Tanousbf648f72021-06-03 15:00:14 -07002076 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002077 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002078 return;
2079 }
2080
Ed Tanousbf648f72021-06-03 15:00:14 -07002081 // Get single eth interface data, and call the below callback
2082 // for JSON preparation
2083 getEthernetIfaceData(
2084 ifaceId,
2085 [asyncResp, parentIfaceId, ifaceId](
2086 const bool& success,
2087 const EthernetInterfaceData& ethData,
2088 const boost::container::flat_set<IPv4AddressData>&,
2089 const boost::container::flat_set<IPv6AddressData>&) {
2090 if (success && ethData.vlan_id.size() != 0)
2091 {
2092 parseInterfaceData(asyncResp->res.jsonValue,
2093 parentIfaceId, ifaceId, ethData);
2094 }
2095 else
2096 {
2097 // ... otherwise return error
2098 // TODO(Pawel)consider distinguish between non
2099 // existing object, and other errors
2100 messages::resourceNotFound(asyncResp->res,
2101 "VLAN Network Interface",
2102 ifaceId);
2103 }
2104 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002105 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002106
Ed Tanousbf648f72021-06-03 15:00:14 -07002107 BMCWEB_ROUTE(
2108 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanous432a8902021-06-14 15:28:56 -07002109 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002110 .methods(boost::beast::http::verb::patch)(
2111 [](const crow::Request& req,
2112 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2113 const std::string& parentIfaceId, const std::string& ifaceId) {
2114 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002115 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002116 messages::resourceNotFound(
2117 asyncResp->res, "VLAN Network Interface", ifaceId);
2118 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002119 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002120
Ed Tanousbf648f72021-06-03 15:00:14 -07002121 bool vlanEnable = false;
2122 uint32_t vlanId = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002123
Ed Tanousbf648f72021-06-03 15:00:14 -07002124 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2125 vlanEnable, "VLANId", vlanId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002126 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002127 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002128 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002129
2130 // Get single eth interface data, and call the below callback
2131 // for JSON preparation
2132 getEthernetIfaceData(
2133 ifaceId,
2134 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2135 const bool& success,
2136 const EthernetInterfaceData& ethData,
2137 const boost::container::flat_set<IPv4AddressData>&,
2138 const boost::container::flat_set<IPv6AddressData>&) {
2139 if (success && !ethData.vlan_id.empty())
2140 {
2141 auto callback =
2142 [asyncResp](
2143 const boost::system::error_code ec) {
2144 if (ec)
2145 {
2146 messages::internalError(asyncResp->res);
2147 }
2148 };
2149
2150 if (vlanEnable == true)
2151 {
2152 crow::connections::systemBus->async_method_call(
2153 std::move(callback),
2154 "xyz.openbmc_project.Network",
2155 "/xyz/openbmc_project/network/" + ifaceId,
2156 "org.freedesktop.DBus.Properties", "Set",
2157 "xyz.openbmc_project.Network.VLAN", "Id",
2158 std::variant<uint32_t>(vlanId));
2159 }
2160 else
2161 {
2162 BMCWEB_LOG_DEBUG
2163 << "vlanEnable is false. Deleting the "
2164 "vlan interface";
2165 crow::connections::systemBus->async_method_call(
2166 std::move(callback),
2167 "xyz.openbmc_project.Network",
2168 std::string(
2169 "/xyz/openbmc_project/network/") +
2170 ifaceId,
2171 "xyz.openbmc_project.Object.Delete",
2172 "Delete");
2173 }
2174 }
2175 else
2176 {
2177 // TODO(Pawel)consider distinguish between non
2178 // existing object, and other errors
2179 messages::resourceNotFound(asyncResp->res,
2180 "VLAN Network Interface",
2181 ifaceId);
2182 return;
2183 }
2184 });
2185 });
2186
2187 BMCWEB_ROUTE(
2188 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanous432a8902021-06-14 15:28:56 -07002189 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002190 .methods(boost::beast::http::verb::delete_)(
2191 [](const crow::Request& /* req */,
2192 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2193 const std::string& parentIfaceId, const std::string& ifaceId) {
2194 if (!verifyNames(parentIfaceId, ifaceId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002195 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002196 messages::resourceNotFound(
2197 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07002198 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002199 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002200
2201 // Get single eth interface data, and call the below callback
2202 // for JSON preparation
2203 getEthernetIfaceData(
2204 ifaceId,
2205 [asyncResp, parentIfaceId, ifaceId](
2206 const bool& success,
2207 const EthernetInterfaceData& ethData,
2208 const boost::container::flat_set<IPv4AddressData>&,
2209 const boost::container::flat_set<IPv6AddressData>&) {
2210 if (success && !ethData.vlan_id.empty())
2211 {
2212 auto callback =
2213 [asyncResp](
2214 const boost::system::error_code ec) {
2215 if (ec)
2216 {
2217 messages::internalError(asyncResp->res);
2218 }
2219 };
2220 crow::connections::systemBus->async_method_call(
2221 std::move(callback),
2222 "xyz.openbmc_project.Network",
2223 std::string("/xyz/openbmc_project/network/") +
2224 ifaceId,
2225 "xyz.openbmc_project.Object.Delete", "Delete");
2226 }
2227 else
2228 {
2229 // ... otherwise return error
2230 // TODO(Pawel)consider distinguish between non
2231 // existing object, and other errors
2232 messages::resourceNotFound(asyncResp->res,
2233 "VLAN Network Interface",
2234 ifaceId);
2235 }
2236 });
Jason M. Billsf12894f2018-10-09 12:45:45 -07002237 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002238
Ed Tanousbf648f72021-06-03 15:00:14 -07002239 BMCWEB_ROUTE(app,
2240 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanous432a8902021-06-14 15:28:56 -07002241 .privileges({{"Login"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002242 .methods(
2243 boost::beast::http::verb::
2244 get)([](const crow::Request& /* req */,
2245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2246 const std::string& rootInterfaceName) {
2247 // Get eth interface list, and call the below callback for JSON
2248 // preparation
2249 getEthernetIfaceList([asyncResp, rootInterfaceName](
2250 const bool& success,
2251 const boost::container::flat_set<
2252 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002253 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002254 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002255 messages::internalError(asyncResp->res);
2256 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002257 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002258
Ed Tanous81ce6092020-12-17 16:54:55 +00002259 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002260 {
2261 messages::resourceNotFound(asyncResp->res,
2262 "VLanNetworkInterfaceCollection",
2263 rootInterfaceName);
2264 return;
2265 }
2266
Ed Tanous0f74e642018-11-12 15:17:05 -08002267 asyncResp->res.jsonValue["@odata.type"] =
2268 "#VLanNetworkInterfaceCollection."
2269 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002270 asyncResp->res.jsonValue["Name"] =
2271 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002272
Ed Tanous2c70f802020-09-28 14:29:23 -07002273 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002274
Ed Tanous81ce6092020-12-17 16:54:55 +00002275 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002276 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002277 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002278 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002279 std::string path =
2280 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2281 path += rootInterfaceName;
2282 path += "/VLANs/";
2283 path += ifaceItem;
2284 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002285 }
2286 }
2287
Jason M. Billsf12894f2018-10-09 12:45:45 -07002288 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002289 ifaceArray.size();
2290 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002291 asyncResp->res.jsonValue["@odata.id"] =
2292 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2293 rootInterfaceName + "/VLANs";
2294 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002295 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002296
Ed Tanousbf648f72021-06-03 15:00:14 -07002297 BMCWEB_ROUTE(app,
2298 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanous432a8902021-06-14 15:28:56 -07002299 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002300 .methods(boost::beast::http::verb::post)(
2301 [](const crow::Request& req,
2302 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2303 const std::string& rootInterfaceName) {
2304 bool vlanEnable = false;
2305 uint32_t vlanId = 0;
2306 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2307 "VLANEnable", vlanEnable))
2308 {
2309 return;
2310 }
2311 // Need both vlanId and vlanEnable to service this request
2312 if (!vlanId)
2313 {
2314 messages::propertyMissing(asyncResp->res, "VLANId");
2315 }
2316 if (!vlanEnable)
2317 {
2318 messages::propertyMissing(asyncResp->res, "VLANEnable");
2319 }
2320 if (static_cast<bool>(vlanId) ^ vlanEnable)
2321 {
2322 return;
2323 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002324
Ed Tanousbf648f72021-06-03 15:00:14 -07002325 auto callback =
2326 [asyncResp](const boost::system::error_code ec) {
2327 if (ec)
2328 {
2329 // TODO(ed) make more consistent error messages
2330 // based on phosphor-network responses
2331 messages::internalError(asyncResp->res);
2332 return;
2333 }
2334 messages::created(asyncResp->res);
2335 };
2336 crow::connections::systemBus->async_method_call(
2337 std::move(callback), "xyz.openbmc_project.Network",
2338 "/xyz/openbmc_project/network",
2339 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2340 rootInterfaceName, vlanId);
2341 });
2342}
2343
Ed Tanous1abe55e2018-09-05 08:30:59 -07002344} // namespace redfish