blob: 85fa633330a626202b21921b9d743bd9b296c484 [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070019#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020020#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080021#include <dbus_utility.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020022#include <error_messages.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070023#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050024#include <utils/json_utils.hpp>
25
Ed Tanousa24526d2018-12-10 15:17:59 -080026#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053027#include <regex>
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
Ed Tanous4a0cb852018-10-15 07:55:04 -070032enum class LinkType
33{
34 Local,
35 Global
36};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010037
38/**
39 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010040 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070041struct IPv4AddressData
42{
43 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070044 std::string address;
45 std::string domain;
46 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 std::string netmask;
48 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070049 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050050 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070051
Gunnar Mills1214b7e2020-06-04 10:11:30 -050052 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070054 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010056};
57
58/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050059 * Structure for keeping IPv6 data required by Redfish
60 */
61struct IPv6AddressData
62{
63 std::string id;
64 std::string address;
65 std::string origin;
66 uint8_t prefixLength;
67
Gunnar Mills1214b7e2020-06-04 10:11:30 -050068 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050069 {
70 return id < obj.id;
71 }
72};
73/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010074 * Structure for keeping basic single Ethernet Interface information
75 * available from DBus
76 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070077struct EthernetInterfaceData
78{
Ed Tanous4a0cb852018-10-15 07:55:04 -070079 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053080 size_t mtuSize;
Ed Tanous4a0cb852018-10-15 07:55:04 -070081 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070082 bool DNSEnabled;
83 bool NTPEnabled;
84 bool HostNameEnabled;
85 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080086 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070087 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070088 std::string DHCPEnabled;
89 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -070090 std::string hostname;
91 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -050092 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -070093 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -050094 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -050095 std::vector<std::string> nameServers;
96 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -080097 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010098};
99
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700100struct DHCPParameters
101{
102 std::optional<bool> dhcpv4Enabled;
103 std::optional<bool> useDNSServers;
104 std::optional<bool> useNTPServers;
105 std::optional<bool> useUseDomainName;
106 std::optional<std::string> dhcpv6OperatingMode;
107};
108
Ed Tanous4a0cb852018-10-15 07:55:04 -0700109// Helper function that changes bits netmask notation (i.e. /24)
110// into full dot notation
111inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700113 uint32_t value = 0xffffffff << (32 - bits);
114 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
115 std::to_string((value >> 16) & 0xff) + "." +
116 std::to_string((value >> 8) & 0xff) + "." +
117 std::to_string(value & 0xff);
118 return netmask;
119}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100120
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500121inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700122 bool isIPv4)
123{
124 if (isIPv4)
125 {
126 return (
127 (inputDHCP ==
128 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
129 (inputDHCP ==
130 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
131 }
132 return ((inputDHCP ==
133 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
134 (inputDHCP ==
135 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
136}
137
Ed Tanous2c70f802020-09-28 14:29:23 -0700138inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700139{
140 if (isIPv4 && isIPv6)
141 {
142 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
143 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700144 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700145 {
146 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
147 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700148 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700149 {
150 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
151 }
152 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
153}
154
Ed Tanous4a0cb852018-10-15 07:55:04 -0700155inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500156 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700157 bool isIPv4)
158{
159 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700161 return "Static";
162 }
163 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
164 {
165 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700167 return "IPv4LinkLocal";
168 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700169 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700170 }
171 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
172 {
173 if (isIPv4)
174 {
175 return "DHCP";
176 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700177 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 }
179 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
180 {
181 return "SLAAC";
182 }
183 return "";
184}
185
Ed Tanous711ac7a2021-12-20 09:34:41 -0800186inline bool
187 extractEthernetInterfaceData(const std::string& ethifaceId,
188 dbus::utility::ManagedObjectType& dbusData,
189 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700190{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700191 bool idFound = false;
Ed Tanous81ce6092020-12-17 16:54:55 +0000192 for (auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700193 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700194 for (auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700195 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000196 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700198 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700199 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500201 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700203 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800206 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700207 if (mac != nullptr)
208 {
209 ethData.mac_address = *mac;
210 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700211 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700212 }
213 }
214 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
215 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500216 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700217 {
218 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500220 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800221 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700222 if (id != nullptr)
223 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500224 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700225 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700227 }
228 }
229 else if (ifacePair.first ==
230 "xyz.openbmc_project.Network.EthernetInterface")
231 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500232 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 {
234 if (propertyPair.first == "AutoNeg")
235 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700236 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800237 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700238 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700240 ethData.auto_neg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 }
242 }
243 else if (propertyPair.first == "Speed")
244 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500245 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800246 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 if (speed != nullptr)
248 {
249 ethData.speed = *speed;
250 }
251 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530252 else if (propertyPair.first == "MTU")
253 {
254 const uint32_t* mtuSize =
255 std::get_if<uint32_t>(&propertyPair.second);
256 if (mtuSize != nullptr)
257 {
258 ethData.mtuSize = *mtuSize;
259 }
260 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800261 else if (propertyPair.first == "LinkUp")
262 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500263 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800264 std::get_if<bool>(&propertyPair.second);
265 if (linkUp != nullptr)
266 {
267 ethData.linkUp = *linkUp;
268 }
269 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700270 else if (propertyPair.first == "NICEnabled")
271 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500272 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700273 std::get_if<bool>(&propertyPair.second);
274 if (nicEnabled != nullptr)
275 {
276 ethData.nicEnabled = *nicEnabled;
277 }
278 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500279 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700280 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500281 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500282 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800283 &propertyPair.second);
284 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700285 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700286 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500287 }
288 }
289 else if (propertyPair.first == "StaticNameServers")
290 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500291 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500292 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500293 &propertyPair.second);
294 if (staticNameServers != nullptr)
295 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700296 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700297 }
298 }
manojkiraneda2a133282019-02-19 13:09:43 +0530299 else if (propertyPair.first == "DHCPEnabled")
300 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700301 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700302 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700303 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530304 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700305 ethData.DHCPEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530306 }
307 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800308 else if (propertyPair.first == "DomainName")
309 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500310 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500311 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800312 &propertyPair.second);
313 if (domainNames != nullptr)
314 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700315 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800316 }
317 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500318 else if (propertyPair.first == "DefaultGateway")
319 {
320 const std::string* defaultGateway =
321 std::get_if<std::string>(&propertyPair.second);
322 if (defaultGateway != nullptr)
323 {
324 std::string defaultGatewayStr = *defaultGateway;
325 if (defaultGatewayStr.empty())
326 {
327 ethData.default_gateway = "0.0.0.0";
328 }
329 else
330 {
331 ethData.default_gateway = defaultGatewayStr;
332 }
333 }
334 }
335 else if (propertyPair.first == "DefaultGateway6")
336 {
337 const std::string* defaultGateway6 =
338 std::get_if<std::string>(&propertyPair.second);
339 if (defaultGateway6 != nullptr)
340 {
341 std::string defaultGateway6Str =
342 *defaultGateway6;
343 if (defaultGateway6Str.empty())
344 {
345 ethData.ipv6_default_gateway =
346 "0:0:0:0:0:0:0:0";
347 }
348 else
349 {
350 ethData.ipv6_default_gateway =
351 defaultGateway6Str;
352 }
353 }
354 }
Ed Tanous029573d2019-02-01 10:57:49 -0800355 }
356 }
357 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700358
359 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
360 {
361 if (ifacePair.first ==
362 "xyz.openbmc_project.Network.DHCPConfiguration")
363 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500364 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700365 {
366 if (propertyPair.first == "DNSEnabled")
367 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700368 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700369 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700370 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700371 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700372 ethData.DNSEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700373 }
374 }
375 else if (propertyPair.first == "NTPEnabled")
376 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700377 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700378 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700379 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700380 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700381 ethData.NTPEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700382 }
383 }
384 else if (propertyPair.first == "HostNameEnabled")
385 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700386 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700387 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700388 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700389 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700390 ethData.HostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700391 }
392 }
393 else if (propertyPair.first == "SendHostNameEnabled")
394 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700395 const bool* sendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700396 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700397 if (sendHostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700398 {
399 ethData.SendHostNameEnabled =
Ed Tanous2c70f802020-09-28 14:29:23 -0700400 *sendHostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700401 }
402 }
403 }
404 }
405 }
Ed Tanous029573d2019-02-01 10:57:49 -0800406 // System configuration shows up in the global namespace, so no need
407 // to check eth number
408 if (ifacePair.first ==
409 "xyz.openbmc_project.Network.SystemConfiguration")
410 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500411 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800412 {
413 if (propertyPair.first == "HostName")
414 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500415 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500416 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800417 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700418 {
Ed Tanous029573d2019-02-01 10:57:49 -0800419 ethData.hostname = *hostname;
420 }
421 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700422 }
423 }
424 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700425 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700426 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700427}
428
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500429// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700430inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000431 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800432 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000433 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500434{
435 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000436 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500437
438 // Since there might be several IPv6 configurations aligned with
439 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000440 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500441 {
442 // Check if proper pattern for object path appears
443 if (boost::starts_with(objpath.first.str, ipv6PathStart))
444 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800445 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500446 {
447 if (interface.first == "xyz.openbmc_project.Network.IP")
448 {
449 // Instance IPv6AddressData structure, and set as
450 // appropriate
451 std::pair<
452 boost::container::flat_set<IPv6AddressData>::iterator,
453 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000454 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700455 IPv6AddressData& ipv6Address = *it.first;
456 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700457 objpath.first.str.substr(ipv6PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800458 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500459 {
460 if (property.first == "Address")
461 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500462 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500463 std::get_if<std::string>(&property.second);
464 if (address != nullptr)
465 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700466 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500467 }
468 }
469 else if (property.first == "Origin")
470 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500471 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500472 std::get_if<std::string>(&property.second);
473 if (origin != nullptr)
474 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700475 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500476 translateAddressOriginDbusToRedfish(*origin,
477 false);
478 }
479 }
480 else if (property.first == "PrefixLength")
481 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500482 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500483 std::get_if<uint8_t>(&property.second);
484 if (prefix != nullptr)
485 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700486 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500487 }
488 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600489 else if (property.first == "Type" ||
490 property.first == "Gateway")
491 {
492 // Type & Gateway is not used
493 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500494 else
495 {
496 BMCWEB_LOG_ERROR
497 << "Got extra property: " << property.first
498 << " on the " << objpath.first.str << " object";
499 }
500 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500501 }
502 }
503 }
504 }
505}
506
Ed Tanous4a0cb852018-10-15 07:55:04 -0700507// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700508inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000509 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800510 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000511 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700512{
513 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000514 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700515
516 // Since there might be several IPv4 configurations aligned with
517 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000518 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700519 {
520 // Check if proper pattern for object path appears
521 if (boost::starts_with(objpath.first.str, ipv4PathStart))
522 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800523 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700524 {
525 if (interface.first == "xyz.openbmc_project.Network.IP")
526 {
527 // Instance IPv4AddressData structure, and set as
528 // appropriate
529 std::pair<
530 boost::container::flat_set<IPv4AddressData>::iterator,
531 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000532 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700533 IPv4AddressData& ipv4Address = *it.first;
534 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700535 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800536 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700537 {
538 if (property.first == "Address")
539 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500540 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800541 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700542 if (address != nullptr)
543 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700544 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700545 }
546 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700547 else if (property.first == "Origin")
548 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500549 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800550 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700551 if (origin != nullptr)
552 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700553 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700554 translateAddressOriginDbusToRedfish(*origin,
555 true);
556 }
557 }
558 else if (property.first == "PrefixLength")
559 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500560 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800561 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700562 if (mask != nullptr)
563 {
564 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700565 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700566 }
567 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600568 else if (property.first == "Type" ||
569 property.first == "Gateway")
570 {
571 // Type & Gateway is not used
572 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700573 else
574 {
575 BMCWEB_LOG_ERROR
576 << "Got extra property: " << property.first
577 << " on the " << objpath.first.str << " object";
578 }
579 }
580 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700581 ipv4Address.linktype =
582 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700583 ? LinkType::Local
584 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700585 }
586 }
587 }
588 }
589}
590
591/**
592 * @brief Sets given Id on the given VLAN interface through D-Bus
593 *
594 * @param[in] ifaceId Id of VLAN interface that should be modified
595 * @param[in] inputVlanId New ID of the VLAN
596 * @param[in] callback Function that will be called after the operation
597 *
598 * @return None.
599 */
600template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500601void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
602 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700603{
604 crow::connections::systemBus->async_method_call(
605 callback, "xyz.openbmc_project.Network",
606 std::string("/xyz/openbmc_project/network/") + ifaceId,
607 "org.freedesktop.DBus.Properties", "Set",
608 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanous168e20c2021-12-13 14:39:53 -0800609 dbus::utility::DbusVariantType(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700610}
611
612/**
613 * @brief Helper function that verifies IP address to check if it is in
614 * proper format. If bits pointer is provided, also calculates active
615 * bit count for Subnet Mask.
616 *
617 * @param[in] ip IP that will be verified
618 * @param[out] bits Calculated mask in bits notation
619 *
620 * @return true in case of success, false otherwise
621 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500622inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
623 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700624{
625 std::vector<std::string> bytesInMask;
626
627 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628
629 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 return false;
633 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634
Ed Tanous4a0cb852018-10-15 07:55:04 -0700635 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700637 *bits = 0;
638 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639
Ed Tanous543f4402022-01-06 13:12:53 -0800640 char* endPtr = nullptr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700641 long previousValue = 255;
Ed Tanous543f4402022-01-06 13:12:53 -0800642 bool firstZeroInByteHit = false;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500643 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700645 if (byte.empty())
646 {
647 return false;
648 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649
Ed Tanous4a0cb852018-10-15 07:55:04 -0700650 // Use strtol instead of stroi to avoid exceptions
651 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700652
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 // endPtr should point to the end of the string, otherwise given string
654 // is not 100% number
655 if (*endPtr != '\0')
656 {
657 return false;
658 }
659
660 // Value should be contained in byte
661 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662 {
663 return false;
664 }
665
666 if (bits != nullptr)
667 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700668 // Mask has to be continuous between bytes
669 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670 {
671 return false;
672 }
673
Ed Tanous4a0cb852018-10-15 07:55:04 -0700674 // Mask has to be continuous inside bytes
675 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676
Ed Tanous4a0cb852018-10-15 07:55:04 -0700677 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000678 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800680 if ((value & (1L << bitIdx)) != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700682 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700684 // Continuity not preserved
685 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700687 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 else
690 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700691 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700692 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700693 }
694 }
695
696 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700697 }
698
Ed Tanous4a0cb852018-10-15 07:55:04 -0700699 return true;
700}
701
702/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700703 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700704 *
705 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700706 * @param[in] ipHash DBus Hash id of IP that should be deleted
707 * @param[io] asyncResp Response object that will be returned to client
708 *
709 * @return None
710 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500711inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800712 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700713{
714 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700715 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700716 if (ec)
717 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800718 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100719 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700720 },
721 "xyz.openbmc_project.Network",
722 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
723 "xyz.openbmc_project.Object.Delete", "Delete");
724}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700725
Gunnar Mills244b6d52021-04-12 15:44:23 -0500726inline void updateIPv4DefaultGateway(
727 const std::string& ifaceId, const std::string& gateway,
728 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500729{
730 crow::connections::systemBus->async_method_call(
731 [asyncResp](const boost::system::error_code ec) {
732 if (ec)
733 {
734 messages::internalError(asyncResp->res);
735 return;
736 }
737 asyncResp->res.result(boost::beast::http::status::no_content);
738 },
739 "xyz.openbmc_project.Network",
740 "/xyz/openbmc_project/network/" + ifaceId,
741 "org.freedesktop.DBus.Properties", "Set",
742 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800743 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500744}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700745/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700746 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700747 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700748 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
749 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
750 * @param[in] gateway IPv4 address of this interfaces gateway
751 * @param[in] address IPv4 address to assign to this interface
752 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700753 *
754 * @return None
755 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000756inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
757 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800758 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700759{
Ravi Teja9010ec22019-08-01 23:30:25 -0500760 auto createIpHandler = [asyncResp, ifaceId,
761 gateway](const boost::system::error_code ec) {
762 if (ec)
763 {
764 messages::internalError(asyncResp->res);
765 return;
766 }
767 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
768 };
769
Ed Tanous4a0cb852018-10-15 07:55:04 -0700770 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500771 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700772 "/xyz/openbmc_project/network/" + ifaceId,
773 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700774 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700775 gateway);
776}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500777
778/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700779 * @brief Deletes the IPv4 entry for this interface and creates a replacement
780 * static IPv4 entry
781 *
782 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
783 * @param[in] id The unique hash entry identifying the DBus entry
784 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
785 * @param[in] gateway IPv4 address of this interfaces gateway
786 * @param[in] address IPv4 address to assign to this interface
787 * @param[io] asyncResp Response object that will be returned to client
788 *
789 * @return None
790 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800791inline void
792 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
793 uint8_t prefixLength, const std::string& gateway,
794 const std::string& address,
795 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700796{
797 crow::connections::systemBus->async_method_call(
798 [asyncResp, ifaceId, address, prefixLength,
799 gateway](const boost::system::error_code ec) {
800 if (ec)
801 {
802 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500803 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700804 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500805
Johnathan Mantey01784822019-06-18 12:44:21 -0700806 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500807 [asyncResp, ifaceId,
808 gateway](const boost::system::error_code ec2) {
Ed Tanous23a21a12020-07-25 04:45:05 +0000809 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700810 {
811 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500812 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700813 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500814 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -0700815 },
816 "xyz.openbmc_project.Network",
817 "/xyz/openbmc_project/network/" + ifaceId,
818 "xyz.openbmc_project.Network.IP.Create", "IP",
819 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
820 prefixLength, gateway);
821 },
822 "xyz.openbmc_project.Network",
823 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
824 "xyz.openbmc_project.Object.Delete", "Delete");
825}
826
827/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500828 * @brief Deletes given IPv6
829 *
830 * @param[in] ifaceId Id of interface whose IP should be deleted
831 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500832 * @param[io] asyncResp Response object that will be returned to client
833 *
834 * @return None
835 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500836inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800837 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500838{
839 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700840 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500841 if (ec)
842 {
843 messages::internalError(asyncResp->res);
844 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500845 },
846 "xyz.openbmc_project.Network",
847 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
848 "xyz.openbmc_project.Object.Delete", "Delete");
849}
850
851/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700852 * @brief Deletes the IPv6 entry for this interface and creates a replacement
853 * static IPv6 entry
854 *
855 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
856 * @param[in] id The unique hash entry identifying the DBus entry
857 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
858 * @param[in] address IPv6 address to assign to this interface
859 * @param[io] asyncResp Response object that will be returned to client
860 *
861 * @return None
862 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800863inline void
864 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
865 uint8_t prefixLength, const std::string& address,
866 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700867{
868 crow::connections::systemBus->async_method_call(
869 [asyncResp, ifaceId, address,
870 prefixLength](const boost::system::error_code ec) {
871 if (ec)
872 {
873 messages::internalError(asyncResp->res);
874 }
875 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000876 [asyncResp](const boost::system::error_code ec2) {
877 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700878 {
879 messages::internalError(asyncResp->res);
880 }
881 },
882 "xyz.openbmc_project.Network",
883 "/xyz/openbmc_project/network/" + ifaceId,
884 "xyz.openbmc_project.Network.IP.Create", "IP",
885 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
886 prefixLength, "");
887 },
888 "xyz.openbmc_project.Network",
889 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
890 "xyz.openbmc_project.Object.Delete", "Delete");
891}
892
893/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500894 * @brief Creates IPv6 with given data
895 *
896 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500897 * @param[in] prefixLength Prefix length that needs to be added
898 * @param[in] address IP address that needs to be added
899 * @param[io] asyncResp Response object that will be returned to client
900 *
901 * @return None
902 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500903inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
904 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800905 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500906{
907 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
908 if (ec)
909 {
910 messages::internalError(asyncResp->res);
911 }
912 };
913 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500914 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500915 crow::connections::systemBus->async_method_call(
916 std::move(createIpHandler), "xyz.openbmc_project.Network",
917 "/xyz/openbmc_project/network/" + ifaceId,
918 "xyz.openbmc_project.Network.IP.Create", "IP",
919 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
920 "");
921}
922
Ed Tanous4a0cb852018-10-15 07:55:04 -0700923/**
924 * Function that retrieves all properties for given Ethernet Interface
925 * Object
926 * from EntityManager Network Manager
927 * @param ethiface_id a eth interface id to query on DBus
928 * @param callback a function that shall be called to convert Dbus output
929 * into JSON
930 */
931template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000932void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500933 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700934{
935 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800936 [ethifaceId{std::string{ethifaceId}},
937 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000938 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800939 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700940 EthernetInterfaceData ethData{};
941 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500942 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700943
Ed Tanous81ce6092020-12-17 16:54:55 +0000944 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700945 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700946 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700947 return;
948 }
949
Ed Tanous4c9afe42019-05-03 16:59:57 -0700950 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700951 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700952 if (!found)
953 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700954 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700955 return;
956 }
957
Ed Tanous2c70f802020-09-28 14:29:23 -0700958 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700959 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500960 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700961 {
Ravi Tejac6191412019-07-30 00:53:50 -0500962 if (((ipv4.linktype == LinkType::Global) &&
963 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500964 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700965 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700966 ipv4.gateway = ethData.default_gateway;
967 }
968 }
969
Ed Tanous2c70f802020-09-28 14:29:23 -0700970 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500971 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700972 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700973 },
974 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
975 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700976}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700977
978/**
979 * Function that retrieves all Ethernet Interfaces available through Network
980 * Manager
981 * @param callback a function that shall be called to convert Dbus output
982 * into JSON.
983 */
984template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500985void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700986{
987 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800988 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000989 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800990 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700991 // Callback requires vector<string> to retrieve all available
992 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -0700993 boost::container::flat_set<std::string> ifaceList;
994 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +0000995 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700996 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700997 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700998 return;
999 }
1000
1001 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001002 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001003 {
1004 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001005 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001006 {
1007 // If interface is
1008 // xyz.openbmc_project.Network.EthernetInterface, this is
1009 // what we're looking for.
1010 if (interface.first ==
1011 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001012 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001013 std::string ifaceId = objpath.first.filename();
1014 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001015 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001016 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001017 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001018 // and put it into output vector.
1019 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001020 }
1021 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001022 }
1023 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -07001024 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001025 },
1026 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1027 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001028}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001029
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001030inline void
1031 handleHostnamePatch(const std::string& hostname,
1032 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001033{
Ed Tanousbf648f72021-06-03 15:00:14 -07001034 // SHOULD handle host names of up to 255 characters(RFC 1123)
1035 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001036 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001037 messages::propertyValueFormatError(asyncResp->res, hostname,
1038 "HostName");
1039 return;
1040 }
1041 crow::connections::systemBus->async_method_call(
1042 [asyncResp](const boost::system::error_code ec) {
1043 if (ec)
1044 {
1045 messages::internalError(asyncResp->res);
1046 }
1047 },
1048 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1049 "org.freedesktop.DBus.Properties", "Set",
1050 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001051 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001052}
1053
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001054inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301055 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1056 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1057{
1058 sdbusplus::message::object_path objPath =
1059 "/xyz/openbmc_project/network/" + ifaceId;
1060 crow::connections::systemBus->async_method_call(
1061 [asyncResp](const boost::system::error_code ec) {
1062 if (ec)
1063 {
1064 messages::internalError(asyncResp->res);
1065 }
1066 },
1067 "xyz.openbmc_project.Network", objPath,
1068 "org.freedesktop.DBus.Properties", "Set",
1069 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1070 std::variant<size_t>(mtuSize));
1071}
1072
1073inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001074 handleDomainnamePatch(const std::string& ifaceId,
1075 const std::string& domainname,
1076 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001077{
1078 std::vector<std::string> vectorDomainname = {domainname};
1079 crow::connections::systemBus->async_method_call(
1080 [asyncResp](const boost::system::error_code ec) {
1081 if (ec)
1082 {
1083 messages::internalError(asyncResp->res);
1084 }
1085 },
1086 "xyz.openbmc_project.Network",
1087 "/xyz/openbmc_project/network/" + ifaceId,
1088 "org.freedesktop.DBus.Properties", "Set",
1089 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001090 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001091}
1092
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001093inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001094{
1095 // A valid host name can never have the dotted-decimal form (RFC 1123)
1096 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1097 {
1098 return false;
1099 }
1100 // Each label(hostname/subdomains) within a valid FQDN
1101 // MUST handle host names of up to 63 characters (RFC 1123)
1102 // labels cannot start or end with hyphens (RFC 952)
1103 // labels can start with numbers (RFC 1123)
1104 const std::regex pattern(
1105 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1106
1107 return std::regex_match(hostname, pattern);
1108}
1109
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001110inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001111{
1112 // Can have multiple subdomains
1113 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001114 const std::regex pattern(
1115 "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
Ed Tanousbf648f72021-06-03 15:00:14 -07001116
1117 return std::regex_match(domainname, pattern);
1118}
1119
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001120inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1121 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001122{
1123 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1124 if (fqdn.length() > 255)
1125 {
1126 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1127 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001128 }
1129
Ed Tanousbf648f72021-06-03 15:00:14 -07001130 size_t pos = fqdn.find('.');
1131 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001132 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001133 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1134 return;
1135 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001136
Ed Tanousbf648f72021-06-03 15:00:14 -07001137 std::string hostname;
1138 std::string domainname;
1139 domainname = (fqdn).substr(pos + 1);
1140 hostname = (fqdn).substr(0, pos);
1141
1142 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1143 {
1144 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1145 return;
1146 }
1147
1148 handleHostnamePatch(hostname, asyncResp);
1149 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1150}
1151
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001152inline void
1153 handleMACAddressPatch(const std::string& ifaceId,
1154 const std::string& macAddress,
1155 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001156{
1157 crow::connections::systemBus->async_method_call(
1158 [asyncResp, macAddress](const boost::system::error_code ec) {
1159 if (ec)
1160 {
1161 messages::internalError(asyncResp->res);
1162 return;
1163 }
1164 },
1165 "xyz.openbmc_project.Network",
1166 "/xyz/openbmc_project/network/" + ifaceId,
1167 "org.freedesktop.DBus.Properties", "Set",
1168 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001169 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001170}
1171
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001172inline void setDHCPEnabled(const std::string& ifaceId,
1173 const std::string& propertyName, const bool v4Value,
1174 const bool v6Value,
1175 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001176{
1177 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1178 crow::connections::systemBus->async_method_call(
1179 [asyncResp](const boost::system::error_code ec) {
1180 if (ec)
1181 {
1182 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1183 messages::internalError(asyncResp->res);
1184 return;
1185 }
1186 messages::success(asyncResp->res);
1187 },
1188 "xyz.openbmc_project.Network",
1189 "/xyz/openbmc_project/network/" + ifaceId,
1190 "org.freedesktop.DBus.Properties", "Set",
1191 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001192 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001193}
1194
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001195inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001196 const std::string& ifaceId, const std::string& propertyName,
1197 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1198{
1199 crow::connections::systemBus->async_method_call(
1200 [asyncResp](const boost::system::error_code ec) {
1201 if (ec)
1202 {
1203 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1204 messages::internalError(asyncResp->res);
1205 return;
1206 }
1207 },
1208 "xyz.openbmc_project.Network",
1209 "/xyz/openbmc_project/network/" + ifaceId,
1210 "org.freedesktop.DBus.Properties", "Set",
1211 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001212 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001213}
1214
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001215inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1216 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001217{
1218 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1219 crow::connections::systemBus->async_method_call(
1220 [asyncResp](const boost::system::error_code ec) {
1221 if (ec)
1222 {
1223 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1224 messages::internalError(asyncResp->res);
1225 return;
1226 }
1227 },
1228 "xyz.openbmc_project.Network",
1229 "/xyz/openbmc_project/network/config/dhcp",
1230 "org.freedesktop.DBus.Properties", "Set",
1231 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001232 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001233}
1234
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001235inline void handleDHCPPatch(const std::string& ifaceId,
1236 const EthernetInterfaceData& ethData,
1237 const DHCPParameters& v4dhcpParms,
1238 const DHCPParameters& v6dhcpParms,
1239 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001240{
1241 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1242 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1243
1244 bool nextv4DHCPState =
1245 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1246
1247 bool nextv6DHCPState{};
1248 if (v6dhcpParms.dhcpv6OperatingMode)
1249 {
1250 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1251 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1252 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1253 {
1254 messages::propertyValueFormatError(asyncResp->res,
1255 *v6dhcpParms.dhcpv6OperatingMode,
1256 "OperatingMode");
1257 return;
1258 }
1259 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1260 }
1261 else
1262 {
1263 nextv6DHCPState = ipv6Active;
1264 }
1265
1266 bool nextDNS{};
1267 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1268 {
1269 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1270 {
1271 messages::generalError(asyncResp->res);
1272 return;
1273 }
1274 nextDNS = *v4dhcpParms.useDNSServers;
1275 }
1276 else if (v4dhcpParms.useDNSServers)
1277 {
1278 nextDNS = *v4dhcpParms.useDNSServers;
1279 }
1280 else if (v6dhcpParms.useDNSServers)
1281 {
1282 nextDNS = *v6dhcpParms.useDNSServers;
1283 }
1284 else
1285 {
1286 nextDNS = ethData.DNSEnabled;
1287 }
1288
1289 bool nextNTP{};
1290 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1291 {
1292 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1293 {
1294 messages::generalError(asyncResp->res);
1295 return;
1296 }
1297 nextNTP = *v4dhcpParms.useNTPServers;
1298 }
1299 else if (v4dhcpParms.useNTPServers)
1300 {
1301 nextNTP = *v4dhcpParms.useNTPServers;
1302 }
1303 else if (v6dhcpParms.useNTPServers)
1304 {
1305 nextNTP = *v6dhcpParms.useNTPServers;
1306 }
1307 else
1308 {
1309 nextNTP = ethData.NTPEnabled;
1310 }
1311
1312 bool nextUseDomain{};
1313 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1314 {
1315 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1316 {
1317 messages::generalError(asyncResp->res);
1318 return;
1319 }
1320 nextUseDomain = *v4dhcpParms.useUseDomainName;
1321 }
1322 else if (v4dhcpParms.useUseDomainName)
1323 {
1324 nextUseDomain = *v4dhcpParms.useUseDomainName;
1325 }
1326 else if (v6dhcpParms.useUseDomainName)
1327 {
1328 nextUseDomain = *v6dhcpParms.useUseDomainName;
1329 }
1330 else
1331 {
1332 nextUseDomain = ethData.HostNameEnabled;
1333 }
1334
1335 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1336 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1337 asyncResp);
1338 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1339 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1340 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1341 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1342 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1343 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1344}
1345
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001346inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001347 getNextStaticIpEntry(
1348 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1349 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1350{
1351 return std::find_if(head, end, [](const IPv4AddressData& value) {
1352 return value.origin == "Static";
1353 });
1354}
1355
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001356inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001357 getNextStaticIpEntry(
1358 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1359 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1360{
1361 return std::find_if(head, end, [](const IPv6AddressData& value) {
1362 return value.origin == "Static";
1363 });
1364}
1365
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001366inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001367 const std::string& ifaceId, nlohmann::json& input,
1368 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1369 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1370{
1371 if ((!input.is_array()) || input.empty())
1372 {
1373 messages::propertyValueTypeError(
1374 asyncResp->res,
1375 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1376 "IPv4StaticAddresses");
1377 return;
1378 }
1379
1380 unsigned entryIdx = 1;
1381 // Find the first static IP address currently active on the NIC and
1382 // match it to the first JSON element in the IPv4StaticAddresses array.
1383 // Match each subsequent JSON element to the next static IP programmed
1384 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001385 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001386 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1387
1388 for (nlohmann::json& thisJson : input)
1389 {
1390 std::string pathString =
1391 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1392
1393 if (!thisJson.is_null() && !thisJson.empty())
1394 {
1395 std::optional<std::string> address;
1396 std::optional<std::string> subnetMask;
1397 std::optional<std::string> gateway;
1398
1399 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1400 address, "SubnetMask", subnetMask,
1401 "Gateway", gateway))
1402 {
1403 messages::propertyValueFormatError(
1404 asyncResp->res,
1405 thisJson.dump(2, ' ', true,
1406 nlohmann::json::error_handler_t::replace),
1407 pathString);
1408 return;
1409 }
1410
1411 // Find the address/subnet/gateway values. Any values that are
1412 // not explicitly provided are assumed to be unmodified from the
1413 // current state of the interface. Merge existing state into the
1414 // current request.
1415 const std::string* addr = nullptr;
1416 const std::string* gw = nullptr;
1417 uint8_t prefixLength = 0;
1418 bool errorInEntry = false;
1419 if (address)
1420 {
1421 if (ipv4VerifyIpAndGetBitcount(*address))
1422 {
1423 addr = &(*address);
1424 }
1425 else
1426 {
1427 messages::propertyValueFormatError(asyncResp->res, *address,
1428 pathString + "/Address");
1429 errorInEntry = true;
1430 }
1431 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001432 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001433 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001434 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001435 }
1436 else
1437 {
1438 messages::propertyMissing(asyncResp->res,
1439 pathString + "/Address");
1440 errorInEntry = true;
1441 }
1442
1443 if (subnetMask)
1444 {
1445 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1446 {
1447 messages::propertyValueFormatError(
1448 asyncResp->res, *subnetMask,
1449 pathString + "/SubnetMask");
1450 errorInEntry = true;
1451 }
1452 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001453 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001454 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001455 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001456 &prefixLength))
1457 {
1458 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001459 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001460 pathString + "/SubnetMask");
1461 errorInEntry = true;
1462 }
1463 }
1464 else
1465 {
1466 messages::propertyMissing(asyncResp->res,
1467 pathString + "/SubnetMask");
1468 errorInEntry = true;
1469 }
1470
1471 if (gateway)
1472 {
1473 if (ipv4VerifyIpAndGetBitcount(*gateway))
1474 {
1475 gw = &(*gateway);
1476 }
1477 else
1478 {
1479 messages::propertyValueFormatError(asyncResp->res, *gateway,
1480 pathString + "/Gateway");
1481 errorInEntry = true;
1482 }
1483 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001484 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001485 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001486 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001487 }
1488 else
1489 {
1490 messages::propertyMissing(asyncResp->res,
1491 pathString + "/Gateway");
1492 errorInEntry = true;
1493 }
1494
1495 if (errorInEntry)
1496 {
1497 return;
1498 }
1499
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001500 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001501 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001502 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001503 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001504 nicIpEntry =
1505 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001506 }
1507 else
1508 {
1509 createIPv4(ifaceId, prefixLength, *gateway, *address,
1510 asyncResp);
1511 }
1512 entryIdx++;
1513 }
1514 else
1515 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001516 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001517 {
1518 // Requesting a DELETE/DO NOT MODIFY action for an item
1519 // that isn't present on the eth(n) interface. Input JSON is
1520 // in error, so bail out.
1521 if (thisJson.is_null())
1522 {
1523 messages::resourceCannotBeDeleted(asyncResp->res);
1524 return;
1525 }
1526 messages::propertyValueFormatError(
1527 asyncResp->res,
1528 thisJson.dump(2, ' ', true,
1529 nlohmann::json::error_handler_t::replace),
1530 pathString);
1531 return;
1532 }
1533
1534 if (thisJson.is_null())
1535 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001536 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001537 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001538 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001539 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001540 nicIpEntry =
1541 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001542 }
1543 entryIdx++;
1544 }
1545 }
1546}
1547
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001548inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001549 const std::string& ifaceId,
1550 const std::vector<std::string>& updatedStaticNameServers,
1551 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1552{
1553 crow::connections::systemBus->async_method_call(
1554 [asyncResp](const boost::system::error_code ec) {
1555 if (ec)
1556 {
1557 messages::internalError(asyncResp->res);
1558 return;
1559 }
1560 },
1561 "xyz.openbmc_project.Network",
1562 "/xyz/openbmc_project/network/" + ifaceId,
1563 "org.freedesktop.DBus.Properties", "Set",
1564 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001565 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001566}
1567
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001568inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001569 const std::string& ifaceId, const nlohmann::json& input,
1570 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1571 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1572{
1573 if (!input.is_array() || input.empty())
1574 {
1575 messages::propertyValueTypeError(
1576 asyncResp->res,
1577 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1578 "IPv6StaticAddresses");
1579 return;
1580 }
1581 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001582 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001583 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1584 for (const nlohmann::json& thisJson : input)
1585 {
1586 std::string pathString =
1587 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1588
1589 if (!thisJson.is_null() && !thisJson.empty())
1590 {
1591 std::optional<std::string> address;
1592 std::optional<uint8_t> prefixLength;
1593 nlohmann::json thisJsonCopy = thisJson;
1594 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1595 address, "PrefixLength", prefixLength))
1596 {
1597 messages::propertyValueFormatError(
1598 asyncResp->res,
1599 thisJson.dump(2, ' ', true,
1600 nlohmann::json::error_handler_t::replace),
1601 pathString);
1602 return;
1603 }
1604
Ed Tanous543f4402022-01-06 13:12:53 -08001605 const std::string* addr = nullptr;
1606 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001607
1608 // Find the address and prefixLength values. Any values that are
1609 // not explicitly provided are assumed to be unmodified from the
1610 // current state of the interface. Merge existing state into the
1611 // current request.
1612 if (address)
1613 {
1614 addr = &(*address);
1615 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001616 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001617 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001618 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001619 }
1620 else
1621 {
1622 messages::propertyMissing(asyncResp->res,
1623 pathString + "/Address");
1624 return;
1625 }
1626
1627 if (prefixLength)
1628 {
1629 prefix = *prefixLength;
1630 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001631 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001632 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001633 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001634 }
1635 else
1636 {
1637 messages::propertyMissing(asyncResp->res,
1638 pathString + "/PrefixLength");
1639 return;
1640 }
1641
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001642 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001643 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001644 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001645 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001646 nicIpEntry =
1647 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001648 }
1649 else
1650 {
1651 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1652 }
1653 entryIdx++;
1654 }
1655 else
1656 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001657 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001658 {
1659 // Requesting a DELETE/DO NOT MODIFY action for an item
1660 // that isn't present on the eth(n) interface. Input JSON is
1661 // in error, so bail out.
1662 if (thisJson.is_null())
1663 {
1664 messages::resourceCannotBeDeleted(asyncResp->res);
1665 return;
1666 }
1667 messages::propertyValueFormatError(
1668 asyncResp->res,
1669 thisJson.dump(2, ' ', true,
1670 nlohmann::json::error_handler_t::replace),
1671 pathString);
1672 return;
1673 }
1674
1675 if (thisJson.is_null())
1676 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001677 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001678 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001679 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001680 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001681 nicIpEntry =
1682 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001683 }
1684 entryIdx++;
1685 }
1686 }
1687}
1688
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001689inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001690 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1691 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1692 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1693 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1694{
1695 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1696 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1697
1698 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1699 jsonResponse["Id"] = ifaceId;
1700 jsonResponse["@odata.id"] =
1701 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1702 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1703
1704 auto health = std::make_shared<HealthPopulate>(asyncResp);
1705
1706 crow::connections::systemBus->async_method_call(
1707 [health](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001708 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanousbf648f72021-06-03 15:00:14 -07001709 if (ec)
1710 {
1711 return;
1712 }
1713
Ed Tanous914e2d52022-01-07 11:38:34 -08001714 health->inventory = resp;
Ed Tanousbf648f72021-06-03 15:00:14 -07001715 },
1716 "xyz.openbmc_project.ObjectMapper",
1717 "/xyz/openbmc_project/object_mapper",
1718 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1719 inventoryForEthernet);
1720
1721 health->populate();
1722
1723 if (ethData.nicEnabled)
1724 {
1725 jsonResponse["LinkStatus"] = "LinkUp";
1726 jsonResponse["Status"]["State"] = "Enabled";
1727 }
1728 else
1729 {
1730 jsonResponse["LinkStatus"] = "NoLink";
1731 jsonResponse["Status"]["State"] = "Disabled";
1732 }
1733
1734 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1735 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301736 jsonResponse["MTUSize"] = ethData.mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001737 jsonResponse["MACAddress"] = ethData.mac_address;
1738 jsonResponse["DHCPv4"]["DHCPEnabled"] =
1739 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1740 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1741 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1742 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1743
1744 jsonResponse["DHCPv6"]["OperatingMode"] =
1745 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1746 : "Disabled";
1747 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1748 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1749 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1750
1751 if (!ethData.hostname.empty())
1752 {
1753 jsonResponse["HostName"] = ethData.hostname;
1754
1755 // When domain name is empty then it means, that it is a network
1756 // without domain names, and the host name itself must be treated as
1757 // FQDN
1758 std::string fqdn = ethData.hostname;
1759 if (!ethData.domainnames.empty())
1760 {
1761 fqdn += "." + ethData.domainnames[0];
1762 }
1763 jsonResponse["FQDN"] = fqdn;
1764 }
1765
1766 jsonResponse["VLANs"] = {
1767 {"@odata.id",
1768 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1769
1770 jsonResponse["NameServers"] = ethData.nameServers;
1771 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1772
1773 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1774 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1775 ipv4Array = nlohmann::json::array();
1776 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001777 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001778 {
1779
1780 std::string gatewayStr = ipv4Config.gateway;
1781 if (gatewayStr.empty())
1782 {
1783 gatewayStr = "0.0.0.0";
1784 }
1785
1786 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1787 {"SubnetMask", ipv4Config.netmask},
1788 {"Address", ipv4Config.address},
1789 {"Gateway", gatewayStr}});
1790 if (ipv4Config.origin == "Static")
1791 {
1792 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1793 {"SubnetMask", ipv4Config.netmask},
1794 {"Address", ipv4Config.address},
1795 {"Gateway", gatewayStr}});
1796 }
1797 }
1798
1799 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1800 if (ipv6GatewayStr.empty())
1801 {
1802 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1803 }
1804
1805 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1806
1807 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1808 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1809 ipv6Array = nlohmann::json::array();
1810 ipv6StaticArray = nlohmann::json::array();
1811 nlohmann::json& ipv6AddrPolicyTable =
1812 jsonResponse["IPv6AddressPolicyTable"];
1813 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001814 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001815 {
1816 ipv6Array.push_back({{"Address", ipv6Config.address},
1817 {"PrefixLength", ipv6Config.prefixLength},
1818 {"AddressOrigin", ipv6Config.origin},
1819 {"AddressState", nullptr}});
1820 if (ipv6Config.origin == "Static")
1821 {
1822 ipv6StaticArray.push_back(
1823 {{"Address", ipv6Config.address},
Jiaqing Zhao7a474a52021-12-03 17:44:36 +08001824 {"PrefixLength", ipv6Config.prefixLength}});
Ed Tanousbf648f72021-06-03 15:00:14 -07001825 }
1826 }
1827}
1828
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001829inline void parseInterfaceData(nlohmann::json& jsonResponse,
1830 const std::string& parentIfaceId,
1831 const std::string& ifaceId,
1832 const EthernetInterfaceData& ethData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001833{
1834 // Fill out obvious data...
1835 jsonResponse["Id"] = ifaceId;
1836 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1837 parentIfaceId + "/VLANs/" + ifaceId;
1838
1839 jsonResponse["VLANEnable"] = true;
1840 if (!ethData.vlan_id.empty())
1841 {
1842 jsonResponse["VLANId"] = ethData.vlan_id.back();
1843 }
1844}
1845
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001846inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001847{
Ed Tanousdcf2ebc2022-01-25 10:07:45 -08001848 return boost::starts_with(iface, parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001849}
1850
1851inline void requestEthernetInterfacesRoutes(App& app)
1852{
1853 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001854 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07001855 .methods(
1856 boost::beast::http::verb::
1857 get)([](const crow::Request&,
1858 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1859 asyncResp->res.jsonValue["@odata.type"] =
1860 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1861 asyncResp->res.jsonValue["@odata.id"] =
1862 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1863 asyncResp->res.jsonValue["Name"] =
1864 "Ethernet Network Interface Collection";
1865 asyncResp->res.jsonValue["Description"] =
1866 "Collection of EthernetInterfaces for this Manager";
1867
1868 // Get eth interface list, and call the below callback for JSON
1869 // preparation
1870 getEthernetIfaceList([asyncResp](const bool& success,
1871 const boost::container::flat_set<
1872 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001873 if (!success)
1874 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001875 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001876 return;
1877 }
1878
Ed Tanous2c70f802020-09-28 14:29:23 -07001879 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001880 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001881 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001882 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001883 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001884 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001885 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001886 if (found == std::string::npos)
1887 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001888 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001889 {{"@odata.id",
1890 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001891 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001892 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001893 }
1894
Ed Tanous4c9afe42019-05-03 16:59:57 -07001895 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001896 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001897 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001898 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001899 });
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301900 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001901
Ed Tanousbf648f72021-06-03 15:00:14 -07001902 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001903 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001904 .methods(boost::beast::http::verb::get)(
1905 [](const crow::Request&,
1906 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1907 const std::string& ifaceId) {
1908 getEthernetIfaceData(
1909 ifaceId,
1910 [asyncResp,
1911 ifaceId](const bool& success,
1912 const EthernetInterfaceData& ethData,
1913 const boost::container::flat_set<IPv4AddressData>&
1914 ipv4Data,
1915 const boost::container::flat_set<IPv6AddressData>&
1916 ipv6Data) {
1917 if (!success)
1918 {
1919 // TODO(Pawel)consider distinguish between non
1920 // existing object, and other errors
1921 messages::resourceNotFound(
1922 asyncResp->res, "EthernetInterface", ifaceId);
1923 return;
1924 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001925
Ed Tanousbf648f72021-06-03 15:00:14 -07001926 asyncResp->res.jsonValue["@odata.type"] =
1927 "#EthernetInterface.v1_4_1.EthernetInterface";
1928 asyncResp->res.jsonValue["Name"] =
1929 "Manager Ethernet Interface";
1930 asyncResp->res.jsonValue["Description"] =
1931 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301932
Ed Tanousbf648f72021-06-03 15:00:14 -07001933 parseInterfaceData(asyncResp, ifaceId, ethData,
1934 ipv4Data, ipv6Data);
1935 });
1936 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001937
Ed Tanousbf648f72021-06-03 15:00:14 -07001938 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001939 .privileges(redfish::privileges::patchEthernetInterface)
1940
Ed Tanousbf648f72021-06-03 15:00:14 -07001941 .methods(boost::beast::http::verb::patch)(
1942 [](const crow::Request& req,
1943 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1944 const std::string& ifaceId) {
1945 std::optional<std::string> hostname;
1946 std::optional<std::string> fqdn;
1947 std::optional<std::string> macAddress;
1948 std::optional<std::string> ipv6DefaultGateway;
1949 std::optional<nlohmann::json> ipv4StaticAddresses;
1950 std::optional<nlohmann::json> ipv6StaticAddresses;
1951 std::optional<std::vector<std::string>> staticNameServers;
1952 std::optional<nlohmann::json> dhcpv4;
1953 std::optional<nlohmann::json> dhcpv6;
1954 std::optional<bool> interfaceEnabled;
Tejas Patil35fb5312021-09-20 15:35:20 +05301955 std::optional<size_t> mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001956 DHCPParameters v4dhcpParms;
1957 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001958
Willy Tu15ed6782021-12-14 11:03:16 -08001959 if (!json_util::readJsonPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001960 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1961 "IPv4StaticAddresses", ipv4StaticAddresses,
1962 "MACAddress", macAddress, "StaticNameServers",
1963 staticNameServers, "IPv6DefaultGateway",
1964 ipv6DefaultGateway, "IPv6StaticAddresses",
1965 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
Tejas Patil35fb5312021-09-20 15:35:20 +05301966 "MTUSize", mtuSize, "InterfaceEnabled",
1967 interfaceEnabled))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001968 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001969 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001970 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001971 if (dhcpv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001972 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001973 if (!json_util::readJson(
1974 *dhcpv4, asyncResp->res, "DHCPEnabled",
1975 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1976 v4dhcpParms.useDNSServers, "UseNTPServers",
1977 v4dhcpParms.useNTPServers, "UseDomainName",
1978 v4dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001979 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001980 return;
1981 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001982 }
1983
Ed Tanousbf648f72021-06-03 15:00:14 -07001984 if (dhcpv6)
Johnathan Mantey01784822019-06-18 12:44:21 -07001985 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001986 if (!json_util::readJson(
1987 *dhcpv6, asyncResp->res, "OperatingMode",
1988 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1989 v6dhcpParms.useDNSServers, "UseNTPServers",
1990 v6dhcpParms.useNTPServers, "UseDomainName",
1991 v6dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001992 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001993 return;
1994 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001995 }
1996
Ed Tanousbf648f72021-06-03 15:00:14 -07001997 // Get single eth interface data, and call the below callback
1998 // for JSON preparation
1999 getEthernetIfaceData(
2000 ifaceId,
2001 [asyncResp, ifaceId, hostname = std::move(hostname),
2002 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2003 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2004 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2005 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
2006 staticNameServers = std::move(staticNameServers),
2007 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
Tejas Patil35fb5312021-09-20 15:35:20 +05302008 mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanousbf648f72021-06-03 15:00:14 -07002009 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2010 const bool& success,
2011 const EthernetInterfaceData& ethData,
2012 const boost::container::flat_set<IPv4AddressData>&
2013 ipv4Data,
2014 const boost::container::flat_set<IPv6AddressData>&
2015 ipv6Data) {
2016 if (!success)
2017 {
2018 // ... otherwise return error
2019 // TODO(Pawel)consider distinguish between non
2020 // existing object, and other errors
2021 messages::resourceNotFound(
2022 asyncResp->res, "Ethernet Interface", ifaceId);
2023 return;
2024 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002025
Ed Tanousbf648f72021-06-03 15:00:14 -07002026 if (dhcpv4 || dhcpv6)
2027 {
2028 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2029 v6dhcpParms, asyncResp);
2030 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002031
Ed Tanousbf648f72021-06-03 15:00:14 -07002032 if (hostname)
2033 {
2034 handleHostnamePatch(*hostname, asyncResp);
2035 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002036
Ed Tanousbf648f72021-06-03 15:00:14 -07002037 if (fqdn)
2038 {
2039 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2040 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002041
Ed Tanousbf648f72021-06-03 15:00:14 -07002042 if (macAddress)
2043 {
2044 handleMACAddressPatch(ifaceId, *macAddress,
2045 asyncResp);
2046 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002047
Ed Tanousbf648f72021-06-03 15:00:14 -07002048 if (ipv4StaticAddresses)
2049 {
2050 // TODO(ed) for some reason the capture of
2051 // ipv4Addresses above is returning a const value,
2052 // not a non-const value. This doesn't really work
2053 // for us, as we need to be able to efficiently move
2054 // out the intermedia nlohmann::json objects. This
2055 // makes a copy of the structure, and operates on
2056 // that, but could be done more efficiently
2057 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2058 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2059 asyncResp);
2060 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002061
Ed Tanousbf648f72021-06-03 15:00:14 -07002062 if (staticNameServers)
2063 {
2064 handleStaticNameServersPatch(
2065 ifaceId, *staticNameServers, asyncResp);
2066 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002067
Ed Tanousbf648f72021-06-03 15:00:14 -07002068 if (ipv6DefaultGateway)
2069 {
2070 messages::propertyNotWritable(asyncResp->res,
2071 "IPv6DefaultGateway");
2072 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08002073
Ed Tanousbf648f72021-06-03 15:00:14 -07002074 if (ipv6StaticAddresses)
2075 {
Ed Tanousf5b191a2022-02-15 11:30:39 -08002076 const nlohmann::json& ipv6Static =
2077 *ipv6StaticAddresses;
Ed Tanousbf648f72021-06-03 15:00:14 -07002078 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2079 ipv6Data, asyncResp);
2080 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002081
Ed Tanousbf648f72021-06-03 15:00:14 -07002082 if (interfaceEnabled)
2083 {
2084 setEthernetInterfaceBoolProperty(
2085 ifaceId, "NICEnabled", *interfaceEnabled,
2086 asyncResp);
2087 }
Tejas Patil35fb5312021-09-20 15:35:20 +05302088
2089 if (mtuSize)
2090 {
2091 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2092 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002093 });
2094 });
manojkiraneda2a133282019-02-19 13:09:43 +05302095
Ed Tanousbf648f72021-06-03 15:00:14 -07002096 BMCWEB_ROUTE(
2097 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002098 .privileges(redfish::privileges::getVLanNetworkInterface)
2099
Ed Tanousbf648f72021-06-03 15:00:14 -07002100 .methods(boost::beast::http::verb::get)(
2101 [](const crow::Request& /* req */,
2102 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2103 const std::string& parentIfaceId, const std::string& ifaceId) {
Ed Tanous0f74e642018-11-12 15:17:05 -08002104 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousbf648f72021-06-03 15:00:14 -07002105 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2106 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002107
Ed Tanousbf648f72021-06-03 15:00:14 -07002108 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002109 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002110 return;
2111 }
2112
Ed Tanousbf648f72021-06-03 15:00:14 -07002113 // Get single eth interface data, and call the below callback
2114 // for JSON preparation
2115 getEthernetIfaceData(
2116 ifaceId,
2117 [asyncResp, parentIfaceId, ifaceId](
2118 const bool& success,
2119 const EthernetInterfaceData& ethData,
2120 const boost::container::flat_set<IPv4AddressData>&,
2121 const boost::container::flat_set<IPv6AddressData>&) {
Ed Tanous26f69762022-01-25 09:49:11 -08002122 if (success && !ethData.vlan_id.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07002123 {
2124 parseInterfaceData(asyncResp->res.jsonValue,
2125 parentIfaceId, ifaceId, ethData);
2126 }
2127 else
2128 {
2129 // ... otherwise return error
2130 // TODO(Pawel)consider distinguish between non
2131 // existing object, and other errors
2132 messages::resourceNotFound(asyncResp->res,
2133 "VLAN Network Interface",
2134 ifaceId);
2135 }
2136 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002137 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002138
Ed Tanousbf648f72021-06-03 15:00:14 -07002139 BMCWEB_ROUTE(
2140 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002141 // This privilege is incorrect, it should be ConfigureManager
2142 //.privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002143 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002144 .methods(boost::beast::http::verb::patch)(
2145 [](const crow::Request& req,
2146 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2147 const std::string& parentIfaceId, const std::string& ifaceId) {
2148 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002149 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002150 messages::resourceNotFound(
2151 asyncResp->res, "VLAN Network Interface", ifaceId);
2152 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002153 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002154
Ed Tanousbf648f72021-06-03 15:00:14 -07002155 bool vlanEnable = false;
2156 uint32_t vlanId = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002157
Willy Tu15ed6782021-12-14 11:03:16 -08002158 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2159 vlanEnable, "VLANId", vlanId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002160 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002161 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002162 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002163
2164 // Get single eth interface data, and call the below callback
2165 // for JSON preparation
2166 getEthernetIfaceData(
2167 ifaceId,
2168 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2169 const bool& success,
2170 const EthernetInterfaceData& ethData,
2171 const boost::container::flat_set<IPv4AddressData>&,
2172 const boost::container::flat_set<IPv6AddressData>&) {
2173 if (success && !ethData.vlan_id.empty())
2174 {
2175 auto callback =
2176 [asyncResp](
2177 const boost::system::error_code ec) {
2178 if (ec)
2179 {
2180 messages::internalError(asyncResp->res);
2181 }
2182 };
2183
Ed Tanouse05aec52022-01-25 10:28:56 -08002184 if (vlanEnable)
Ed Tanousbf648f72021-06-03 15:00:14 -07002185 {
2186 crow::connections::systemBus->async_method_call(
2187 std::move(callback),
2188 "xyz.openbmc_project.Network",
2189 "/xyz/openbmc_project/network/" + ifaceId,
2190 "org.freedesktop.DBus.Properties", "Set",
2191 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanous168e20c2021-12-13 14:39:53 -08002192 dbus::utility::DbusVariantType(vlanId));
Ed Tanousbf648f72021-06-03 15:00:14 -07002193 }
2194 else
2195 {
2196 BMCWEB_LOG_DEBUG
2197 << "vlanEnable is false. Deleting the "
2198 "vlan interface";
2199 crow::connections::systemBus->async_method_call(
2200 std::move(callback),
2201 "xyz.openbmc_project.Network",
2202 std::string(
2203 "/xyz/openbmc_project/network/") +
2204 ifaceId,
2205 "xyz.openbmc_project.Object.Delete",
2206 "Delete");
2207 }
2208 }
2209 else
2210 {
2211 // TODO(Pawel)consider distinguish between non
2212 // existing object, and other errors
2213 messages::resourceNotFound(asyncResp->res,
2214 "VLAN Network Interface",
2215 ifaceId);
2216 return;
2217 }
2218 });
2219 });
2220
2221 BMCWEB_ROUTE(
2222 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002223 // This privilege is incorrect, it should be ConfigureManager
2224 //.privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002225 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002226 .methods(boost::beast::http::verb::delete_)(
2227 [](const crow::Request& /* req */,
2228 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2229 const std::string& parentIfaceId, const std::string& ifaceId) {
2230 if (!verifyNames(parentIfaceId, ifaceId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002231 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002232 messages::resourceNotFound(
2233 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07002234 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002235 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002236
2237 // Get single eth interface data, and call the below callback
2238 // for JSON preparation
2239 getEthernetIfaceData(
2240 ifaceId,
2241 [asyncResp, parentIfaceId, ifaceId](
2242 const bool& success,
2243 const EthernetInterfaceData& ethData,
2244 const boost::container::flat_set<IPv4AddressData>&,
2245 const boost::container::flat_set<IPv6AddressData>&) {
2246 if (success && !ethData.vlan_id.empty())
2247 {
2248 auto callback =
2249 [asyncResp](
2250 const boost::system::error_code ec) {
2251 if (ec)
2252 {
2253 messages::internalError(asyncResp->res);
2254 }
2255 };
2256 crow::connections::systemBus->async_method_call(
2257 std::move(callback),
2258 "xyz.openbmc_project.Network",
2259 std::string("/xyz/openbmc_project/network/") +
2260 ifaceId,
2261 "xyz.openbmc_project.Object.Delete", "Delete");
2262 }
2263 else
2264 {
2265 // ... otherwise return error
2266 // TODO(Pawel)consider distinguish between non
2267 // existing object, and other errors
2268 messages::resourceNotFound(asyncResp->res,
2269 "VLAN Network Interface",
2270 ifaceId);
2271 }
2272 });
Jason M. Billsf12894f2018-10-09 12:45:45 -07002273 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002274
Ed Tanousbf648f72021-06-03 15:00:14 -07002275 BMCWEB_ROUTE(app,
2276 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002277
2278 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002279 .methods(
2280 boost::beast::http::verb::
2281 get)([](const crow::Request& /* req */,
2282 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2283 const std::string& rootInterfaceName) {
2284 // Get eth interface list, and call the below callback for JSON
2285 // preparation
2286 getEthernetIfaceList([asyncResp, rootInterfaceName](
2287 const bool& success,
2288 const boost::container::flat_set<
2289 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002290 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002291 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002292 messages::internalError(asyncResp->res);
2293 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002294 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002295
Ed Tanous81ce6092020-12-17 16:54:55 +00002296 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002297 {
2298 messages::resourceNotFound(asyncResp->res,
2299 "VLanNetworkInterfaceCollection",
2300 rootInterfaceName);
2301 return;
2302 }
2303
Ed Tanous0f74e642018-11-12 15:17:05 -08002304 asyncResp->res.jsonValue["@odata.type"] =
2305 "#VLanNetworkInterfaceCollection."
2306 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002307 asyncResp->res.jsonValue["Name"] =
2308 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002309
Ed Tanous2c70f802020-09-28 14:29:23 -07002310 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002311
Ed Tanous81ce6092020-12-17 16:54:55 +00002312 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002313 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002314 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002315 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002316 std::string path =
2317 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2318 path += rootInterfaceName;
2319 path += "/VLANs/";
2320 path += ifaceItem;
2321 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002322 }
2323 }
2324
Jason M. Billsf12894f2018-10-09 12:45:45 -07002325 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002326 ifaceArray.size();
2327 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002328 asyncResp->res.jsonValue["@odata.id"] =
2329 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2330 rootInterfaceName + "/VLANs";
2331 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002332 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002333
Ed Tanousbf648f72021-06-03 15:00:14 -07002334 BMCWEB_ROUTE(app,
2335 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002336 // This privilege is wrong, it should be ConfigureManager
2337 //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002338 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002339 .methods(boost::beast::http::verb::post)(
2340 [](const crow::Request& req,
2341 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2342 const std::string& rootInterfaceName) {
2343 bool vlanEnable = false;
2344 uint32_t vlanId = 0;
Willy Tu15ed6782021-12-14 11:03:16 -08002345 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId",
2346 vlanId, "VLANEnable", vlanEnable))
Ed Tanousbf648f72021-06-03 15:00:14 -07002347 {
2348 return;
2349 }
2350 // Need both vlanId and vlanEnable to service this request
Ed Tanousdbb59d42022-01-25 11:09:55 -08002351 if (vlanId == 0U)
Ed Tanousbf648f72021-06-03 15:00:14 -07002352 {
2353 messages::propertyMissing(asyncResp->res, "VLANId");
2354 }
2355 if (!vlanEnable)
2356 {
2357 messages::propertyMissing(asyncResp->res, "VLANEnable");
2358 }
2359 if (static_cast<bool>(vlanId) ^ vlanEnable)
2360 {
2361 return;
2362 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002363
Ed Tanousbf648f72021-06-03 15:00:14 -07002364 auto callback =
2365 [asyncResp](const boost::system::error_code ec) {
2366 if (ec)
2367 {
2368 // TODO(ed) make more consistent error messages
2369 // based on phosphor-network responses
2370 messages::internalError(asyncResp->res);
2371 return;
2372 }
2373 messages::created(asyncResp->res);
2374 };
2375 crow::connections::systemBus->async_method_call(
2376 std::move(callback), "xyz.openbmc_project.Network",
2377 "/xyz/openbmc_project/network",
2378 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2379 rootInterfaceName, vlanId);
2380 });
2381}
2382
Ed Tanous1abe55e2018-09-05 08:30:59 -07002383} // namespace redfish