blob: 5ccc25fe3911eb0c5561274f795f320ed01d0033 [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 Tanous11ba3972022-07-11 09:50:41 -070019#include <boost/algorithm/string/classification.hpp>
20#include <boost/algorithm/string/split.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070021#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020022#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080023#include <dbus_utility.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020024#include <error_messages.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070025#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070026#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027#include <utils/json_utils.hpp>
28
Ed Tanousa24526d2018-12-10 15:17:59 -080029#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053030#include <regex>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032namespace redfish
33{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010034
Ed Tanous4a0cb852018-10-15 07:55:04 -070035enum class LinkType
36{
37 Local,
38 Global
39};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010040
41/**
42 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010043 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070044struct IPv4AddressData
45{
46 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070047 std::string address;
48 std::string domain;
49 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 std::string netmask;
51 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070052 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050053 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070054
Gunnar Mills1214b7e2020-06-04 10:11:30 -050055 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070057 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010059};
60
61/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050062 * Structure for keeping IPv6 data required by Redfish
63 */
64struct IPv6AddressData
65{
66 std::string id;
67 std::string address;
68 std::string origin;
69 uint8_t prefixLength;
70
Gunnar Mills1214b7e2020-06-04 10:11:30 -050071 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050072 {
73 return id < obj.id;
74 }
75};
76/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010077 * Structure for keeping basic single Ethernet Interface information
78 * available from DBus
79 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070080struct EthernetInterfaceData
81{
Ed Tanous4a0cb852018-10-15 07:55:04 -070082 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053083 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080084 bool autoNeg;
85 bool dnsEnabled;
86 bool ntpEnabled;
87 bool hostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080088 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070089 bool nicEnabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080090 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070091 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080092 std::string hostName;
93 std::string defaultGateway;
94 std::string ipv6DefaultGateway;
95 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +080096 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -050097 std::vector<std::string> nameServers;
98 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -080099 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100100};
101
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700102struct DHCPParameters
103{
104 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800105 std::optional<bool> useDnsServers;
106 std::optional<bool> useNtpServers;
107 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700108 std::optional<std::string> dhcpv6OperatingMode;
109};
110
Ed Tanous4a0cb852018-10-15 07:55:04 -0700111// Helper function that changes bits netmask notation (i.e. /24)
112// into full dot notation
113inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700115 uint32_t value = 0xffffffff << (32 - bits);
116 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
117 std::to_string((value >> 16) & 0xff) + "." +
118 std::to_string((value >> 8) & 0xff) + "." +
119 std::to_string(value & 0xff);
120 return netmask;
121}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100122
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800123inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700124 bool isIPv4)
125{
126 if (isIPv4)
127 {
128 return (
129 (inputDHCP ==
130 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
131 (inputDHCP ==
132 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
133 }
134 return ((inputDHCP ==
135 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
136 (inputDHCP ==
137 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
138}
139
Ed Tanous2c70f802020-09-28 14:29:23 -0700140inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700141{
142 if (isIPv4 && isIPv6)
143 {
144 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
145 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700146 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700147 {
148 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
149 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700150 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700151 {
152 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
153 }
154 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
155}
156
Ed Tanous4a0cb852018-10-15 07:55:04 -0700157inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500158 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700159 bool isIPv4)
160{
161 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700163 return "Static";
164 }
165 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
166 {
167 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700169 return "IPv4LinkLocal";
170 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700171 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700172 }
173 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
174 {
175 if (isIPv4)
176 {
177 return "DHCP";
178 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700179 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700180 }
181 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
182 {
183 return "SLAAC";
184 }
185 return "";
186}
187
Ed Tanous02cad962022-06-30 16:50:15 -0700188inline bool extractEthernetInterfaceData(
189 const std::string& ethifaceId,
190 const dbus::utility::ManagedObjectType& dbusData,
191 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700192{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700193 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700194 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700195 {
Ed Tanous02cad962022-06-30 16:50:15 -0700196 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700197 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000198 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700200 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700201 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500203 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700205 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700206 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500207 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800208 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700209 if (mac != nullptr)
210 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800211 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700212 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700214 }
215 }
216 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
217 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500218 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 {
220 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500222 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800223 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 if (id != nullptr)
225 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800226 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700227 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 }
230 }
231 else if (ifacePair.first ==
232 "xyz.openbmc_project.Network.EthernetInterface")
233 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500234 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700235 {
236 if (propertyPair.first == "AutoNeg")
237 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700238 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800239 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700240 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800242 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 }
244 }
245 else if (propertyPair.first == "Speed")
246 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500247 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800248 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700249 if (speed != nullptr)
250 {
251 ethData.speed = *speed;
252 }
253 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530254 else if (propertyPair.first == "MTU")
255 {
256 const uint32_t* mtuSize =
257 std::get_if<uint32_t>(&propertyPair.second);
258 if (mtuSize != nullptr)
259 {
260 ethData.mtuSize = *mtuSize;
261 }
262 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800263 else if (propertyPair.first == "LinkUp")
264 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500265 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800266 std::get_if<bool>(&propertyPair.second);
267 if (linkUp != nullptr)
268 {
269 ethData.linkUp = *linkUp;
270 }
271 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700272 else if (propertyPair.first == "NICEnabled")
273 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500274 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700275 std::get_if<bool>(&propertyPair.second);
276 if (nicEnabled != nullptr)
277 {
278 ethData.nicEnabled = *nicEnabled;
279 }
280 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500281 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700282 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500283 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500284 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800285 &propertyPair.second);
286 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700287 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700288 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500289 }
290 }
291 else if (propertyPair.first == "StaticNameServers")
292 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500293 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500294 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500295 &propertyPair.second);
296 if (staticNameServers != nullptr)
297 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700298 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700299 }
300 }
manojkiraneda2a133282019-02-19 13:09:43 +0530301 else if (propertyPair.first == "DHCPEnabled")
302 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700303 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700304 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700305 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530306 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800307 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530308 }
309 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800310 else if (propertyPair.first == "DomainName")
311 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500312 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500313 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800314 &propertyPair.second);
315 if (domainNames != nullptr)
316 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700317 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800318 }
319 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500320 else if (propertyPair.first == "DefaultGateway")
321 {
322 const std::string* defaultGateway =
323 std::get_if<std::string>(&propertyPair.second);
324 if (defaultGateway != nullptr)
325 {
326 std::string defaultGatewayStr = *defaultGateway;
327 if (defaultGatewayStr.empty())
328 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800329 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500330 }
331 else
332 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800333 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500334 }
335 }
336 }
337 else if (propertyPair.first == "DefaultGateway6")
338 {
339 const std::string* defaultGateway6 =
340 std::get_if<std::string>(&propertyPair.second);
341 if (defaultGateway6 != nullptr)
342 {
343 std::string defaultGateway6Str =
344 *defaultGateway6;
345 if (defaultGateway6Str.empty())
346 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800347 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500348 "0:0:0:0:0:0:0:0";
349 }
350 else
351 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800352 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500353 defaultGateway6Str;
354 }
355 }
356 }
Ed Tanous029573d2019-02-01 10:57:49 -0800357 }
358 }
359 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700360
361 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
362 {
363 if (ifacePair.first ==
364 "xyz.openbmc_project.Network.DHCPConfiguration")
365 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500366 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700367 {
368 if (propertyPair.first == "DNSEnabled")
369 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700370 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700371 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700372 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700373 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800374 ethData.dnsEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700375 }
376 }
377 else if (propertyPair.first == "NTPEnabled")
378 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700379 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700380 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700381 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700382 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800383 ethData.ntpEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700384 }
385 }
386 else if (propertyPair.first == "HostNameEnabled")
387 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700388 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700389 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700390 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700391 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800392 ethData.hostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700393 }
394 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700395 }
396 }
397 }
Ed Tanous029573d2019-02-01 10:57:49 -0800398 // System configuration shows up in the global namespace, so no need
399 // to check eth number
400 if (ifacePair.first ==
401 "xyz.openbmc_project.Network.SystemConfiguration")
402 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500403 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800404 {
405 if (propertyPair.first == "HostName")
406 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500407 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500408 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800409 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700410 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800411 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800412 }
413 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700414 }
415 }
416 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700418 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700419}
420
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500421// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700422inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000423 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800424 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000425 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500426{
427 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000428 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500429
430 // Since there might be several IPv6 configurations aligned with
431 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000432 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500433 {
434 // Check if proper pattern for object path appears
Ed Tanous11ba3972022-07-11 09:50:41 -0700435 if (objpath.first.str.starts_with(ipv6PathStart))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500436 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800437 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500438 {
439 if (interface.first == "xyz.openbmc_project.Network.IP")
440 {
441 // Instance IPv6AddressData structure, and set as
442 // appropriate
443 std::pair<
444 boost::container::flat_set<IPv6AddressData>::iterator,
445 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000446 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700447 IPv6AddressData& ipv6Address = *it.first;
448 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700449 objpath.first.str.substr(ipv6PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800450 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500451 {
452 if (property.first == "Address")
453 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500454 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500455 std::get_if<std::string>(&property.second);
456 if (address != nullptr)
457 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700458 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500459 }
460 }
461 else if (property.first == "Origin")
462 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500463 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500464 std::get_if<std::string>(&property.second);
465 if (origin != nullptr)
466 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700467 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500468 translateAddressOriginDbusToRedfish(*origin,
469 false);
470 }
471 }
472 else if (property.first == "PrefixLength")
473 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500474 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500475 std::get_if<uint8_t>(&property.second);
476 if (prefix != nullptr)
477 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700478 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500479 }
480 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600481 else if (property.first == "Type" ||
482 property.first == "Gateway")
483 {
484 // Type & Gateway is not used
485 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500486 else
487 {
488 BMCWEB_LOG_ERROR
489 << "Got extra property: " << property.first
490 << " on the " << objpath.first.str << " object";
491 }
492 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500493 }
494 }
495 }
496 }
497}
498
Ed Tanous4a0cb852018-10-15 07:55:04 -0700499// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700500inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000501 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800502 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000503 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700504{
505 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000506 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700507
508 // Since there might be several IPv4 configurations aligned with
509 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000510 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700511 {
512 // Check if proper pattern for object path appears
Ed Tanous11ba3972022-07-11 09:50:41 -0700513 if (objpath.first.str.starts_with(ipv4PathStart))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700514 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800515 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700516 {
517 if (interface.first == "xyz.openbmc_project.Network.IP")
518 {
519 // Instance IPv4AddressData structure, and set as
520 // appropriate
521 std::pair<
522 boost::container::flat_set<IPv4AddressData>::iterator,
523 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000524 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700525 IPv4AddressData& ipv4Address = *it.first;
526 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700527 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800528 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700529 {
530 if (property.first == "Address")
531 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500532 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800533 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700534 if (address != nullptr)
535 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700536 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700537 }
538 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700539 else if (property.first == "Origin")
540 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500541 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800542 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700543 if (origin != nullptr)
544 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700545 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700546 translateAddressOriginDbusToRedfish(*origin,
547 true);
548 }
549 }
550 else if (property.first == "PrefixLength")
551 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500552 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800553 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700554 if (mask != nullptr)
555 {
556 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700557 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700558 }
559 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600560 else if (property.first == "Type" ||
561 property.first == "Gateway")
562 {
563 // Type & Gateway is not used
564 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700565 else
566 {
567 BMCWEB_LOG_ERROR
568 << "Got extra property: " << property.first
569 << " on the " << objpath.first.str << " object";
570 }
571 }
572 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700573 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700574 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700575 ? LinkType::Local
576 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700577 }
578 }
579 }
580 }
581}
582
583/**
Ed Tanous4a0cb852018-10-15 07:55:04 -0700584 * @brief Helper function that verifies IP address to check if it is in
585 * proper format. If bits pointer is provided, also calculates active
586 * bit count for Subnet Mask.
587 *
588 * @param[in] ip IP that will be verified
589 * @param[out] bits Calculated mask in bits notation
590 *
591 * @return true in case of success, false otherwise
592 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500593inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
594 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700595{
596 std::vector<std::string> bytesInMask;
597
598 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700599
600 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700601 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700602 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700603 return false;
604 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605
Ed Tanous4a0cb852018-10-15 07:55:04 -0700606 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700607 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700608 *bits = 0;
609 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700610
Ed Tanous543f4402022-01-06 13:12:53 -0800611 char* endPtr = nullptr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700612 long previousValue = 255;
Ed Tanous543f4402022-01-06 13:12:53 -0800613 bool firstZeroInByteHit = false;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500614 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700616 if (byte.empty())
617 {
618 return false;
619 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700620
Ed Tanous4a0cb852018-10-15 07:55:04 -0700621 // Use strtol instead of stroi to avoid exceptions
622 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623
Ed Tanous4a0cb852018-10-15 07:55:04 -0700624 // endPtr should point to the end of the string, otherwise given string
625 // is not 100% number
626 if (*endPtr != '\0')
627 {
628 return false;
629 }
630
631 // Value should be contained in byte
632 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700633 {
634 return false;
635 }
636
637 if (bits != nullptr)
638 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700639 // Mask has to be continuous between bytes
640 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700641 {
642 return false;
643 }
644
Ed Tanous4a0cb852018-10-15 07:55:04 -0700645 // Mask has to be continuous inside bytes
646 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000649 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800651 if ((value & (1L << bitIdx)) != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700652 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700655 // Continuity not preserved
656 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700658 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700660 else
661 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700663 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700664 }
665 }
666
667 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700668 }
669
Ed Tanous4a0cb852018-10-15 07:55:04 -0700670 return true;
671}
672
673/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700674 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700675 *
676 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700677 * @param[in] ipHash DBus Hash id of IP that should be deleted
678 * @param[io] asyncResp Response object that will be returned to client
679 *
680 * @return None
681 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500682inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800683 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700684{
685 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700686 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700687 if (ec)
688 {
689 messages::internalError(asyncResp->res);
690 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700691 },
692 "xyz.openbmc_project.Network",
693 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
694 "xyz.openbmc_project.Object.Delete", "Delete");
695}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696
Gunnar Mills244b6d52021-04-12 15:44:23 -0500697inline void updateIPv4DefaultGateway(
698 const std::string& ifaceId, const std::string& gateway,
699 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500700{
701 crow::connections::systemBus->async_method_call(
702 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700703 if (ec)
704 {
705 messages::internalError(asyncResp->res);
706 return;
707 }
708 asyncResp->res.result(boost::beast::http::status::no_content);
Ravi Teja9010ec22019-08-01 23:30:25 -0500709 },
710 "xyz.openbmc_project.Network",
711 "/xyz/openbmc_project/network/" + ifaceId,
712 "org.freedesktop.DBus.Properties", "Set",
713 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800714 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500715}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700716/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700717 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700718 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700719 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
720 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
721 * @param[in] gateway IPv4 address of this interfaces gateway
722 * @param[in] address IPv4 address to assign to this interface
723 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700724 *
725 * @return None
726 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000727inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
728 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800729 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700730{
Ed Tanous002d39b2022-05-31 08:59:27 -0700731 auto createIpHandler =
732 [asyncResp, ifaceId, gateway](const boost::system::error_code ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500733 if (ec)
734 {
735 messages::internalError(asyncResp->res);
736 return;
737 }
738 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
739 };
740
Ed Tanous4a0cb852018-10-15 07:55:04 -0700741 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500742 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700743 "/xyz/openbmc_project/network/" + ifaceId,
744 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700745 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700746 gateway);
747}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500748
749/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700750 * @brief Deletes the IPv4 entry for this interface and creates a replacement
751 * static IPv4 entry
752 *
753 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
754 * @param[in] id The unique hash entry identifying the DBus entry
755 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
756 * @param[in] gateway IPv4 address of this interfaces gateway
757 * @param[in] address IPv4 address to assign to this interface
758 * @param[io] asyncResp Response object that will be returned to client
759 *
760 * @return None
761 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800762inline void
763 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
764 uint8_t prefixLength, const std::string& gateway,
765 const std::string& address,
766 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700767{
768 crow::connections::systemBus->async_method_call(
769 [asyncResp, ifaceId, address, prefixLength,
770 gateway](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700771 if (ec)
772 {
773 messages::internalError(asyncResp->res);
774 return;
775 }
776
777 crow::connections::systemBus->async_method_call(
778 [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) {
779 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700780 {
781 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500782 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700783 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700784 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
785 },
786 "xyz.openbmc_project.Network",
787 "/xyz/openbmc_project/network/" + ifaceId,
788 "xyz.openbmc_project.Network.IP.Create", "IP",
789 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
790 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700791 },
792 "xyz.openbmc_project.Network",
793 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
794 "xyz.openbmc_project.Object.Delete", "Delete");
795}
796
797/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500798 * @brief Deletes given IPv6
799 *
800 * @param[in] ifaceId Id of interface whose IP should be deleted
801 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500802 * @param[io] asyncResp Response object that will be returned to client
803 *
804 * @return None
805 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500806inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800807 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500808{
809 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700810 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700811 if (ec)
812 {
813 messages::internalError(asyncResp->res);
814 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500815 },
816 "xyz.openbmc_project.Network",
817 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
818 "xyz.openbmc_project.Object.Delete", "Delete");
819}
820
821/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700822 * @brief Deletes the IPv6 entry for this interface and creates a replacement
823 * static IPv6 entry
824 *
825 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
826 * @param[in] id The unique hash entry identifying the DBus entry
827 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
828 * @param[in] address IPv6 address to assign to this interface
829 * @param[io] asyncResp Response object that will be returned to client
830 *
831 * @return None
832 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800833inline void
834 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
835 uint8_t prefixLength, const std::string& address,
836 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700837{
838 crow::connections::systemBus->async_method_call(
839 [asyncResp, ifaceId, address,
840 prefixLength](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700841 if (ec)
842 {
843 messages::internalError(asyncResp->res);
844 }
845 crow::connections::systemBus->async_method_call(
846 [asyncResp](const boost::system::error_code ec2) {
847 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700848 {
849 messages::internalError(asyncResp->res);
850 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700851 },
852 "xyz.openbmc_project.Network",
853 "/xyz/openbmc_project/network/" + ifaceId,
854 "xyz.openbmc_project.Network.IP.Create", "IP",
855 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
856 prefixLength, "");
Johnathan Mantey01784822019-06-18 12:44:21 -0700857 },
858 "xyz.openbmc_project.Network",
859 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
860 "xyz.openbmc_project.Object.Delete", "Delete");
861}
862
863/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500864 * @brief Creates IPv6 with given data
865 *
866 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500867 * @param[in] prefixLength Prefix length that needs to be added
868 * @param[in] address IP address that needs to be added
869 * @param[io] asyncResp Response object that will be returned to client
870 *
871 * @return None
872 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500873inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
874 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800875 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500876{
877 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
878 if (ec)
879 {
880 messages::internalError(asyncResp->res);
881 }
882 };
883 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500884 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500885 crow::connections::systemBus->async_method_call(
886 std::move(createIpHandler), "xyz.openbmc_project.Network",
887 "/xyz/openbmc_project/network/" + ifaceId,
888 "xyz.openbmc_project.Network.IP.Create", "IP",
889 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
890 "");
891}
892
Ed Tanous4a0cb852018-10-15 07:55:04 -0700893/**
894 * Function that retrieves all properties for given Ethernet Interface
895 * Object
896 * from EntityManager Network Manager
897 * @param ethiface_id a eth interface id to query on DBus
898 * @param callback a function that shall be called to convert Dbus output
899 * into JSON
900 */
901template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000902void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500903 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700904{
905 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800906 [ethifaceId{std::string{ethifaceId}},
907 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000908 const boost::system::error_code errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700909 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700910 EthernetInterfaceData ethData{};
911 boost::container::flat_set<IPv4AddressData> ipv4Data;
912 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700913
Ed Tanous002d39b2022-05-31 08:59:27 -0700914 if (errorCode)
915 {
916 callback(false, ethData, ipv4Data, ipv6Data);
917 return;
918 }
919
920 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
921 if (!found)
922 {
923 callback(false, ethData, ipv4Data, ipv6Data);
924 return;
925 }
926
927 extractIPData(ethifaceId, resp, ipv4Data);
928 // Fix global GW
929 for (IPv4AddressData& ipv4 : ipv4Data)
930 {
931 if (((ipv4.linktype == LinkType::Global) &&
932 (ipv4.gateway == "0.0.0.0")) ||
933 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700934 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700935 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700936 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700937 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700938
Ed Tanous002d39b2022-05-31 08:59:27 -0700939 extractIPV6Data(ethifaceId, resp, ipv6Data);
940 // Finally make a callback with useful data
941 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700942 },
943 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
944 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700945}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700946
947/**
948 * Function that retrieves all Ethernet Interfaces available through Network
949 * Manager
950 * @param callback a function that shall be called to convert Dbus output
951 * into JSON.
952 */
953template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500954void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700955{
956 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800957 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000958 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800959 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700960 // Callback requires vector<string> to retrieve all available
961 // ethernet interfaces
962 boost::container::flat_set<std::string> ifaceList;
963 ifaceList.reserve(resp.size());
964 if (errorCode)
965 {
966 callback(false, ifaceList);
967 return;
968 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700969
Ed Tanous002d39b2022-05-31 08:59:27 -0700970 // Iterate over all retrieved ObjectPaths.
971 for (const auto& objpath : resp)
972 {
973 // And all interfaces available for certain ObjectPath.
974 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700975 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700976 // If interface is
977 // xyz.openbmc_project.Network.EthernetInterface, this is
978 // what we're looking for.
979 if (interface.first ==
980 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700981 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700982 std::string ifaceId = objpath.first.filename();
983 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700984 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700985 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700987 // and put it into output vector.
988 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700990 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700991 }
992 // Finally make a callback with useful data
993 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700994 },
995 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
996 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700997}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100998
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700999inline void
1000 handleHostnamePatch(const std::string& hostname,
1001 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002{
Ed Tanousbf648f72021-06-03 15:00:14 -07001003 // SHOULD handle host names of up to 255 characters(RFC 1123)
1004 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001005 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001006 messages::propertyValueFormatError(asyncResp->res, hostname,
1007 "HostName");
1008 return;
1009 }
1010 crow::connections::systemBus->async_method_call(
1011 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001012 if (ec)
1013 {
1014 messages::internalError(asyncResp->res);
1015 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001016 },
1017 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1018 "org.freedesktop.DBus.Properties", "Set",
1019 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001020 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001021}
1022
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001023inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301024 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1025 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1026{
1027 sdbusplus::message::object_path objPath =
1028 "/xyz/openbmc_project/network/" + ifaceId;
1029 crow::connections::systemBus->async_method_call(
1030 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001031 if (ec)
1032 {
1033 messages::internalError(asyncResp->res);
1034 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301035 },
1036 "xyz.openbmc_project.Network", objPath,
1037 "org.freedesktop.DBus.Properties", "Set",
1038 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1039 std::variant<size_t>(mtuSize));
1040}
1041
1042inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001043 handleDomainnamePatch(const std::string& ifaceId,
1044 const std::string& domainname,
1045 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001046{
1047 std::vector<std::string> vectorDomainname = {domainname};
1048 crow::connections::systemBus->async_method_call(
1049 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001050 if (ec)
1051 {
1052 messages::internalError(asyncResp->res);
1053 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001054 },
1055 "xyz.openbmc_project.Network",
1056 "/xyz/openbmc_project/network/" + ifaceId,
1057 "org.freedesktop.DBus.Properties", "Set",
1058 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001059 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001060}
1061
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001062inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001063{
1064 // A valid host name can never have the dotted-decimal form (RFC 1123)
1065 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1066 {
1067 return false;
1068 }
1069 // Each label(hostname/subdomains) within a valid FQDN
1070 // MUST handle host names of up to 63 characters (RFC 1123)
1071 // labels cannot start or end with hyphens (RFC 952)
1072 // labels can start with numbers (RFC 1123)
1073 const std::regex pattern(
1074 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1075
1076 return std::regex_match(hostname, pattern);
1077}
1078
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001079inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001080{
1081 // Can have multiple subdomains
1082 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001083 const std::regex pattern(
1084 "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
Ed Tanousbf648f72021-06-03 15:00:14 -07001085
1086 return std::regex_match(domainname, pattern);
1087}
1088
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001089inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1090 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001091{
1092 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1093 if (fqdn.length() > 255)
1094 {
1095 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1096 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097 }
1098
Ed Tanousbf648f72021-06-03 15:00:14 -07001099 size_t pos = fqdn.find('.');
1100 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001101 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001102 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1103 return;
1104 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001105
Ed Tanousbf648f72021-06-03 15:00:14 -07001106 std::string hostname;
1107 std::string domainname;
1108 domainname = (fqdn).substr(pos + 1);
1109 hostname = (fqdn).substr(0, pos);
1110
1111 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1112 {
1113 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1114 return;
1115 }
1116
1117 handleHostnamePatch(hostname, asyncResp);
1118 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1119}
1120
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001121inline void
1122 handleMACAddressPatch(const std::string& ifaceId,
1123 const std::string& macAddress,
1124 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001125{
1126 crow::connections::systemBus->async_method_call(
1127 [asyncResp, macAddress](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 if (ec)
1129 {
1130 messages::internalError(asyncResp->res);
1131 return;
1132 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001133 },
1134 "xyz.openbmc_project.Network",
1135 "/xyz/openbmc_project/network/" + ifaceId,
1136 "org.freedesktop.DBus.Properties", "Set",
1137 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001138 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001139}
1140
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001141inline void setDHCPEnabled(const std::string& ifaceId,
1142 const std::string& propertyName, const bool v4Value,
1143 const bool v6Value,
1144 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001145{
1146 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1147 crow::connections::systemBus->async_method_call(
1148 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001149 if (ec)
1150 {
1151 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1152 messages::internalError(asyncResp->res);
1153 return;
1154 }
1155 messages::success(asyncResp->res);
Ed Tanousbf648f72021-06-03 15:00:14 -07001156 },
1157 "xyz.openbmc_project.Network",
1158 "/xyz/openbmc_project/network/" + ifaceId,
1159 "org.freedesktop.DBus.Properties", "Set",
1160 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001161 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001162}
1163
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001164inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001165 const std::string& ifaceId, const std::string& propertyName,
1166 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1167{
1168 crow::connections::systemBus->async_method_call(
1169 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001170 if (ec)
1171 {
1172 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1173 messages::internalError(asyncResp->res);
1174 return;
1175 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001176 },
1177 "xyz.openbmc_project.Network",
1178 "/xyz/openbmc_project/network/" + ifaceId,
1179 "org.freedesktop.DBus.Properties", "Set",
1180 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001181 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001182}
1183
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001184inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1185 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001186{
1187 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1188 crow::connections::systemBus->async_method_call(
1189 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001190 if (ec)
1191 {
1192 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1193 messages::internalError(asyncResp->res);
1194 return;
1195 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001196 },
1197 "xyz.openbmc_project.Network",
1198 "/xyz/openbmc_project/network/config/dhcp",
1199 "org.freedesktop.DBus.Properties", "Set",
1200 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001201 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001202}
1203
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001204inline void handleDHCPPatch(const std::string& ifaceId,
1205 const EthernetInterfaceData& ethData,
1206 const DHCPParameters& v4dhcpParms,
1207 const DHCPParameters& v6dhcpParms,
1208 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001209{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001210 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1211 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001212
1213 bool nextv4DHCPState =
1214 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1215
1216 bool nextv6DHCPState{};
1217 if (v6dhcpParms.dhcpv6OperatingMode)
1218 {
1219 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1220 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1221 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1222 {
1223 messages::propertyValueFormatError(asyncResp->res,
1224 *v6dhcpParms.dhcpv6OperatingMode,
1225 "OperatingMode");
1226 return;
1227 }
1228 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1229 }
1230 else
1231 {
1232 nextv6DHCPState = ipv6Active;
1233 }
1234
1235 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001236 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001237 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001238 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001239 {
1240 messages::generalError(asyncResp->res);
1241 return;
1242 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001243 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001244 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001245 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001246 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001247 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001248 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001249 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001250 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001251 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001252 }
1253 else
1254 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001255 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001256 }
1257
1258 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001259 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001260 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001261 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001262 {
1263 messages::generalError(asyncResp->res);
1264 return;
1265 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001266 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001267 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001268 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001269 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001270 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001271 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001272 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001273 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001274 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001275 }
1276 else
1277 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001278 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001279 }
1280
1281 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001282 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001283 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001284 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001285 {
1286 messages::generalError(asyncResp->res);
1287 return;
1288 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001289 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001290 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001291 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001292 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001293 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001294 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001295 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001296 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001297 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001298 }
1299 else
1300 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001301 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001302 }
1303
1304 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1305 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1306 asyncResp);
1307 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1308 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1309 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1310 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1311 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1312 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1313}
1314
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001315inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001316 getNextStaticIpEntry(
1317 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1318 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1319{
1320 return std::find_if(head, end, [](const IPv4AddressData& value) {
1321 return value.origin == "Static";
1322 });
1323}
1324
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001325inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001326 getNextStaticIpEntry(
1327 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1328 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1329{
1330 return std::find_if(head, end, [](const IPv6AddressData& value) {
1331 return value.origin == "Static";
1332 });
1333}
1334
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001335inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001336 const std::string& ifaceId, nlohmann::json& input,
1337 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1338 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1339{
1340 if ((!input.is_array()) || input.empty())
1341 {
1342 messages::propertyValueTypeError(
1343 asyncResp->res,
1344 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1345 "IPv4StaticAddresses");
1346 return;
1347 }
1348
1349 unsigned entryIdx = 1;
1350 // Find the first static IP address currently active on the NIC and
1351 // match it to the first JSON element in the IPv4StaticAddresses array.
1352 // Match each subsequent JSON element to the next static IP programmed
1353 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001354 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001355 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1356
1357 for (nlohmann::json& thisJson : input)
1358 {
1359 std::string pathString =
1360 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1361
1362 if (!thisJson.is_null() && !thisJson.empty())
1363 {
1364 std::optional<std::string> address;
1365 std::optional<std::string> subnetMask;
1366 std::optional<std::string> gateway;
1367
1368 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1369 address, "SubnetMask", subnetMask,
1370 "Gateway", gateway))
1371 {
1372 messages::propertyValueFormatError(
1373 asyncResp->res,
1374 thisJson.dump(2, ' ', true,
1375 nlohmann::json::error_handler_t::replace),
1376 pathString);
1377 return;
1378 }
1379
1380 // Find the address/subnet/gateway values. Any values that are
1381 // not explicitly provided are assumed to be unmodified from the
1382 // current state of the interface. Merge existing state into the
1383 // current request.
1384 const std::string* addr = nullptr;
1385 const std::string* gw = nullptr;
1386 uint8_t prefixLength = 0;
1387 bool errorInEntry = false;
1388 if (address)
1389 {
1390 if (ipv4VerifyIpAndGetBitcount(*address))
1391 {
1392 addr = &(*address);
1393 }
1394 else
1395 {
1396 messages::propertyValueFormatError(asyncResp->res, *address,
1397 pathString + "/Address");
1398 errorInEntry = true;
1399 }
1400 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001401 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001402 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001403 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001404 }
1405 else
1406 {
1407 messages::propertyMissing(asyncResp->res,
1408 pathString + "/Address");
1409 errorInEntry = true;
1410 }
1411
1412 if (subnetMask)
1413 {
1414 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1415 {
1416 messages::propertyValueFormatError(
1417 asyncResp->res, *subnetMask,
1418 pathString + "/SubnetMask");
1419 errorInEntry = true;
1420 }
1421 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001422 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001423 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001424 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001425 &prefixLength))
1426 {
1427 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001428 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001429 pathString + "/SubnetMask");
1430 errorInEntry = true;
1431 }
1432 }
1433 else
1434 {
1435 messages::propertyMissing(asyncResp->res,
1436 pathString + "/SubnetMask");
1437 errorInEntry = true;
1438 }
1439
1440 if (gateway)
1441 {
1442 if (ipv4VerifyIpAndGetBitcount(*gateway))
1443 {
1444 gw = &(*gateway);
1445 }
1446 else
1447 {
1448 messages::propertyValueFormatError(asyncResp->res, *gateway,
1449 pathString + "/Gateway");
1450 errorInEntry = true;
1451 }
1452 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001453 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001454 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001455 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001456 }
1457 else
1458 {
1459 messages::propertyMissing(asyncResp->res,
1460 pathString + "/Gateway");
1461 errorInEntry = true;
1462 }
1463
1464 if (errorInEntry)
1465 {
1466 return;
1467 }
1468
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001469 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001470 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001471 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001472 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001473 nicIpEntry =
1474 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001475 }
1476 else
1477 {
1478 createIPv4(ifaceId, prefixLength, *gateway, *address,
1479 asyncResp);
1480 }
1481 entryIdx++;
1482 }
1483 else
1484 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001485 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001486 {
1487 // Requesting a DELETE/DO NOT MODIFY action for an item
1488 // that isn't present on the eth(n) interface. Input JSON is
1489 // in error, so bail out.
1490 if (thisJson.is_null())
1491 {
1492 messages::resourceCannotBeDeleted(asyncResp->res);
1493 return;
1494 }
1495 messages::propertyValueFormatError(
1496 asyncResp->res,
1497 thisJson.dump(2, ' ', true,
1498 nlohmann::json::error_handler_t::replace),
1499 pathString);
1500 return;
1501 }
1502
1503 if (thisJson.is_null())
1504 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001505 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001506 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001507 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001508 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001509 nicIpEntry =
1510 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001511 }
1512 entryIdx++;
1513 }
1514 }
1515}
1516
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001517inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001518 const std::string& ifaceId,
1519 const std::vector<std::string>& updatedStaticNameServers,
1520 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1521{
1522 crow::connections::systemBus->async_method_call(
1523 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001524 if (ec)
1525 {
1526 messages::internalError(asyncResp->res);
1527 return;
1528 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001529 },
1530 "xyz.openbmc_project.Network",
1531 "/xyz/openbmc_project/network/" + ifaceId,
1532 "org.freedesktop.DBus.Properties", "Set",
1533 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001534 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001535}
1536
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001537inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001538 const std::string& ifaceId, const nlohmann::json& input,
1539 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1540 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1541{
1542 if (!input.is_array() || input.empty())
1543 {
1544 messages::propertyValueTypeError(
1545 asyncResp->res,
1546 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1547 "IPv6StaticAddresses");
1548 return;
1549 }
1550 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001551 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001552 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1553 for (const nlohmann::json& thisJson : input)
1554 {
1555 std::string pathString =
1556 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1557
1558 if (!thisJson.is_null() && !thisJson.empty())
1559 {
1560 std::optional<std::string> address;
1561 std::optional<uint8_t> prefixLength;
1562 nlohmann::json thisJsonCopy = thisJson;
1563 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1564 address, "PrefixLength", prefixLength))
1565 {
1566 messages::propertyValueFormatError(
1567 asyncResp->res,
1568 thisJson.dump(2, ' ', true,
1569 nlohmann::json::error_handler_t::replace),
1570 pathString);
1571 return;
1572 }
1573
Ed Tanous543f4402022-01-06 13:12:53 -08001574 const std::string* addr = nullptr;
1575 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001576
1577 // Find the address and prefixLength values. Any values that are
1578 // not explicitly provided are assumed to be unmodified from the
1579 // current state of the interface. Merge existing state into the
1580 // current request.
1581 if (address)
1582 {
1583 addr = &(*address);
1584 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001585 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001586 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001587 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001588 }
1589 else
1590 {
1591 messages::propertyMissing(asyncResp->res,
1592 pathString + "/Address");
1593 return;
1594 }
1595
1596 if (prefixLength)
1597 {
1598 prefix = *prefixLength;
1599 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001600 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001601 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001602 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001603 }
1604 else
1605 {
1606 messages::propertyMissing(asyncResp->res,
1607 pathString + "/PrefixLength");
1608 return;
1609 }
1610
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001611 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001612 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001613 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001614 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001615 nicIpEntry =
1616 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001617 }
1618 else
1619 {
1620 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1621 }
1622 entryIdx++;
1623 }
1624 else
1625 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001626 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001627 {
1628 // Requesting a DELETE/DO NOT MODIFY action for an item
1629 // that isn't present on the eth(n) interface. Input JSON is
1630 // in error, so bail out.
1631 if (thisJson.is_null())
1632 {
1633 messages::resourceCannotBeDeleted(asyncResp->res);
1634 return;
1635 }
1636 messages::propertyValueFormatError(
1637 asyncResp->res,
1638 thisJson.dump(2, ' ', true,
1639 nlohmann::json::error_handler_t::replace),
1640 pathString);
1641 return;
1642 }
1643
1644 if (thisJson.is_null())
1645 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001646 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001647 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001648 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001649 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001650 nicIpEntry =
1651 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001652 }
1653 entryIdx++;
1654 }
1655 }
1656}
1657
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001658inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001659 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1660 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1661 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1662 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1663{
1664 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1665 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1666
1667 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1668 jsonResponse["Id"] = ifaceId;
1669 jsonResponse["@odata.id"] =
1670 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1671 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1672
1673 auto health = std::make_shared<HealthPopulate>(asyncResp);
1674
1675 crow::connections::systemBus->async_method_call(
1676 [health](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001677 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001678 if (ec)
1679 {
1680 return;
1681 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001682
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 health->inventory = resp;
Ed Tanousbf648f72021-06-03 15:00:14 -07001684 },
1685 "xyz.openbmc_project.ObjectMapper",
1686 "/xyz/openbmc_project/object_mapper",
1687 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1688 inventoryForEthernet);
1689
1690 health->populate();
1691
1692 if (ethData.nicEnabled)
1693 {
1694 jsonResponse["LinkStatus"] = "LinkUp";
1695 jsonResponse["Status"]["State"] = "Enabled";
1696 }
1697 else
1698 {
1699 jsonResponse["LinkStatus"] = "NoLink";
1700 jsonResponse["Status"]["State"] = "Disabled";
1701 }
1702
1703 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1704 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301705 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001706 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001707 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001708 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1709 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1710 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1711 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001712
1713 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001714 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001715 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001716 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1717 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1718 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001719
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001720 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001721 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001722 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001723
1724 // When domain name is empty then it means, that it is a network
1725 // without domain names, and the host name itself must be treated as
1726 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001727 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001728 if (!ethData.domainnames.empty())
1729 {
1730 fqdn += "." + ethData.domainnames[0];
1731 }
1732 jsonResponse["FQDN"] = fqdn;
1733 }
1734
1735 jsonResponse["VLANs"] = {
1736 {"@odata.id",
1737 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1738
1739 jsonResponse["NameServers"] = ethData.nameServers;
1740 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1741
1742 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1743 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1744 ipv4Array = nlohmann::json::array();
1745 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001746 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001747 {
1748
1749 std::string gatewayStr = ipv4Config.gateway;
1750 if (gatewayStr.empty())
1751 {
1752 gatewayStr = "0.0.0.0";
1753 }
Ed Tanous14766872022-03-15 10:44:42 -07001754 nlohmann::json::object_t ipv4;
1755 ipv4["AddressOrigin"] = ipv4Config.origin;
1756 ipv4["SubnetMask"] = ipv4Config.netmask;
1757 ipv4["Address"] = ipv4Config.address;
1758 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001759
Ed Tanousbf648f72021-06-03 15:00:14 -07001760 if (ipv4Config.origin == "Static")
1761 {
Ed Tanous14766872022-03-15 10:44:42 -07001762 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001763 }
Ed Tanous14766872022-03-15 10:44:42 -07001764
1765 ipv4Array.push_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001766 }
1767
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001768 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001769 if (ipv6GatewayStr.empty())
1770 {
1771 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1772 }
1773
1774 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1775
1776 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1777 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1778 ipv6Array = nlohmann::json::array();
1779 ipv6StaticArray = nlohmann::json::array();
1780 nlohmann::json& ipv6AddrPolicyTable =
1781 jsonResponse["IPv6AddressPolicyTable"];
1782 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001783 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001784 {
Ed Tanous14766872022-03-15 10:44:42 -07001785 nlohmann::json::object_t ipv6;
1786 ipv6["Address"] = ipv6Config.address;
1787 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1788 ipv6["AddressOrigin"] = ipv6Config.origin;
1789 ipv6["AddressState"] = nullptr;
1790 ipv6Array.push_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001791 if (ipv6Config.origin == "Static")
1792 {
Ed Tanous14766872022-03-15 10:44:42 -07001793 nlohmann::json::object_t ipv6Static;
1794 ipv6Static["Address"] = ipv6Config.address;
1795 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1796 ipv6StaticArray.push_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001797 }
1798 }
1799}
1800
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001801inline void parseInterfaceData(nlohmann::json& jsonResponse,
1802 const std::string& parentIfaceId,
1803 const std::string& ifaceId,
1804 const EthernetInterfaceData& ethData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001805{
1806 // Fill out obvious data...
1807 jsonResponse["Id"] = ifaceId;
1808 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1809 parentIfaceId + "/VLANs/" + ifaceId;
1810
1811 jsonResponse["VLANEnable"] = true;
Jiaqing Zhao17e22022022-04-14 18:58:06 +08001812 if (ethData.vlanId)
Ed Tanousbf648f72021-06-03 15:00:14 -07001813 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +08001814 jsonResponse["VLANId"] = *ethData.vlanId;
Ed Tanousbf648f72021-06-03 15:00:14 -07001815 }
1816}
1817
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001818inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001819{
Ed Tanous11ba3972022-07-11 09:50:41 -07001820 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001821}
1822
1823inline void requestEthernetInterfacesRoutes(App& app)
1824{
1825 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001826 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001827 .methods(boost::beast::http::verb::get)(
1828 [&app](const crow::Request& req,
1829 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001830 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001831 {
1832 return;
1833 }
1834
1835 asyncResp->res.jsonValue["@odata.type"] =
1836 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1837 asyncResp->res.jsonValue["@odata.id"] =
1838 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1839 asyncResp->res.jsonValue["Name"] =
1840 "Ethernet Network Interface Collection";
1841 asyncResp->res.jsonValue["Description"] =
1842 "Collection of EthernetInterfaces for this Manager";
1843
1844 // Get eth interface list, and call the below callback for JSON
1845 // preparation
1846 getEthernetIfaceList(
1847 [asyncResp](
1848 const bool& success,
1849 const boost::container::flat_set<std::string>& ifaceList) {
1850 if (!success)
1851 {
1852 messages::internalError(asyncResp->res);
1853 return;
1854 }
1855
1856 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1857 ifaceArray = nlohmann::json::array();
1858 std::string tag = "_";
1859 for (const std::string& ifaceItem : ifaceList)
1860 {
1861 std::size_t found = ifaceItem.find(tag);
1862 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001863 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001864 nlohmann::json::object_t iface;
1865 iface["@odata.id"] =
1866 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1867 ifaceItem;
1868 ifaceArray.push_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001869 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001870 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001871
Ed Tanous002d39b2022-05-31 08:59:27 -07001872 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1873 asyncResp->res.jsonValue["@odata.id"] =
1874 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1875 });
1876 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001877
Ed Tanousbf648f72021-06-03 15:00:14 -07001878 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001879 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001880 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001881 [&app](const crow::Request& req,
1882 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1883 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001884 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001885 {
1886 return;
1887 }
1888 getEthernetIfaceData(
1889 ifaceId,
1890 [asyncResp, ifaceId](
1891 const bool& success, const EthernetInterfaceData& ethData,
1892 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1893 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1894 if (!success)
1895 {
1896 // TODO(Pawel)consider distinguish between non
1897 // existing object, and other errors
1898 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1899 ifaceId);
1900 return;
1901 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001902
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 asyncResp->res.jsonValue["@odata.type"] =
1904 "#EthernetInterface.v1_4_1.EthernetInterface";
1905 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1906 asyncResp->res.jsonValue["Description"] =
1907 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301908
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001910 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001912
Ed Tanousbf648f72021-06-03 15:00:14 -07001913 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001914 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001915 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001916 [&app](const crow::Request& req,
1917 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1918 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001919 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001920 {
1921 return;
1922 }
1923 std::optional<std::string> hostname;
1924 std::optional<std::string> fqdn;
1925 std::optional<std::string> macAddress;
1926 std::optional<std::string> ipv6DefaultGateway;
1927 std::optional<nlohmann::json> ipv4StaticAddresses;
1928 std::optional<nlohmann::json> ipv6StaticAddresses;
1929 std::optional<std::vector<std::string>> staticNameServers;
1930 std::optional<nlohmann::json> dhcpv4;
1931 std::optional<nlohmann::json> dhcpv6;
1932 std::optional<bool> interfaceEnabled;
1933 std::optional<size_t> mtuSize;
1934 DHCPParameters v4dhcpParms;
1935 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001936
Ed Tanous002d39b2022-05-31 08:59:27 -07001937 if (!json_util::readJsonPatch(
1938 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1939 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1940 macAddress, "StaticNameServers", staticNameServers,
1941 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1942 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1943 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1944 {
1945 return;
1946 }
1947 if (dhcpv4)
1948 {
1949 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1950 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1951 v4dhcpParms.useDnsServers, "UseNTPServers",
1952 v4dhcpParms.useNtpServers, "UseDomainName",
1953 v4dhcpParms.useDomainName))
1954 {
1955 return;
1956 }
1957 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001958
Ed Tanous002d39b2022-05-31 08:59:27 -07001959 if (dhcpv6)
1960 {
1961 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1962 v6dhcpParms.dhcpv6OperatingMode,
1963 "UseDNSServers", v6dhcpParms.useDnsServers,
1964 "UseNTPServers", v6dhcpParms.useNtpServers,
1965 "UseDomainName",
1966 v6dhcpParms.useDomainName))
1967 {
1968 return;
1969 }
1970 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001971
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 // Get single eth interface data, and call the below callback
1973 // for JSON preparation
1974 getEthernetIfaceData(
1975 ifaceId,
1976 [asyncResp, ifaceId, hostname = std::move(hostname),
1977 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1978 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1979 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1980 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1981 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001982 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1983 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001984 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1985 const bool& success, const EthernetInterfaceData& ethData,
1986 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1987 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1988 if (!success)
1989 {
1990 // ... otherwise return error
1991 // TODO(Pawel)consider distinguish between non
1992 // existing object, and other errors
1993 messages::resourceNotFound(asyncResp->res, "Ethernet Interface",
1994 ifaceId);
1995 return;
1996 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001997
Ed Tanous002d39b2022-05-31 08:59:27 -07001998 if (dhcpv4 || dhcpv6)
1999 {
2000 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07002001 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002002 }
Tejas Patil35fb5312021-09-20 15:35:20 +05302003
Ed Tanous002d39b2022-05-31 08:59:27 -07002004 if (hostname)
2005 {
2006 handleHostnamePatch(*hostname, asyncResp);
2007 }
2008
2009 if (fqdn)
2010 {
2011 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2012 }
2013
2014 if (macAddress)
2015 {
2016 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2017 }
2018
2019 if (ipv4StaticAddresses)
2020 {
2021 // TODO(ed) for some reason the capture of
2022 // ipv4Addresses above is returning a const value,
2023 // not a non-const value. This doesn't really work
2024 // for us, as we need to be able to efficiently move
2025 // out the intermedia nlohmann::json objects. This
2026 // makes a copy of the structure, and operates on
2027 // that, but could be done more efficiently
2028 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2029 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
2030 }
2031
2032 if (staticNameServers)
2033 {
2034 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2035 asyncResp);
2036 }
2037
2038 if (ipv6DefaultGateway)
2039 {
2040 messages::propertyNotWritable(asyncResp->res,
2041 "IPv6DefaultGateway");
2042 }
2043
2044 if (ipv6StaticAddresses)
2045 {
2046 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
2047 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
2048 asyncResp);
2049 }
2050
2051 if (interfaceEnabled)
2052 {
2053 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2054 *interfaceEnabled, asyncResp);
2055 }
2056
2057 if (mtuSize)
2058 {
2059 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2060 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002061 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002062 });
manojkiraneda2a133282019-02-19 13:09:43 +05302063
Ed Tanousbf648f72021-06-03 15:00:14 -07002064 BMCWEB_ROUTE(
2065 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002066 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002067 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002068 [&app](const crow::Request& req,
2069 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2070 const std::string& parentIfaceId,
2071 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002072 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002073 {
2074 return;
2075 }
2076 asyncResp->res.jsonValue["@odata.type"] =
2077 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2078 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002079
Ed Tanous002d39b2022-05-31 08:59:27 -07002080 if (!verifyNames(parentIfaceId, ifaceId))
2081 {
2082 return;
2083 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002084
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 // Get single eth interface data, and call the below callback
2086 // for JSON preparation
2087 getEthernetIfaceData(
2088 ifaceId,
2089 [asyncResp, parentIfaceId,
2090 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2091 const boost::container::flat_set<IPv4AddressData>&,
2092 const boost::container::flat_set<IPv6AddressData>&) {
2093 if (success && ethData.vlanId)
2094 {
2095 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2096 ifaceId, ethData);
2097 }
2098 else
2099 {
2100 // ... otherwise return error
2101 // TODO(Pawel)consider distinguish between non
2102 // existing object, and other errors
2103 messages::resourceNotFound(asyncResp->res,
2104 "VLAN Network Interface", ifaceId);
2105 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002106 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002107 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002108
Ed Tanousbf648f72021-06-03 15:00:14 -07002109 BMCWEB_ROUTE(
2110 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002111 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002112 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002113 [&app](const crow::Request& req,
2114 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2115 const std::string& parentIfaceId,
2116 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002117 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002118 {
2119 return;
2120 }
2121 if (!verifyNames(parentIfaceId, ifaceId))
2122 {
2123 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2124 ifaceId);
2125 return;
2126 }
2127
2128 std::optional<bool> vlanEnable;
2129 std::optional<uint32_t> vlanId;
2130
2131 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2132 vlanEnable, "VLANId", vlanId))
2133 {
2134 return;
2135 }
2136
2137 if (vlanId)
2138 {
2139 messages::propertyNotWritable(asyncResp->res, "VLANId");
2140 return;
2141 }
2142
2143 // Get single eth interface data, and call the below callback
2144 // for JSON preparation
2145 getEthernetIfaceData(
2146 ifaceId,
2147 [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2148 const bool& success, const EthernetInterfaceData& ethData,
2149 const boost::container::flat_set<IPv4AddressData>&,
2150 const boost::container::flat_set<IPv6AddressData>&) {
2151 if (success && ethData.vlanId)
2152 {
2153 auto callback =
2154 [asyncResp](const boost::system::error_code ec) {
2155 if (ec)
2156 {
2157 messages::internalError(asyncResp->res);
2158 return;
2159 }
2160 };
2161
2162 if (vlanEnable && !(*vlanEnable))
Ed Tanous45ca1b82022-03-25 13:07:27 -07002163 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002164 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2165 "vlan interface";
2166 crow::connections::systemBus->async_method_call(
2167 std::move(callback), "xyz.openbmc_project.Network",
2168 std::string("/xyz/openbmc_project/network/") + ifaceId,
2169 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07002170 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002171 }
2172 else
2173 {
2174 // TODO(Pawel)consider distinguish between non
2175 // existing object, and other errors
2176 messages::resourceNotFound(asyncResp->res,
2177 "VLAN Network Interface", ifaceId);
2178 return;
2179 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002180 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002181 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002182
2183 BMCWEB_ROUTE(
2184 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002185 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002186 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002187 [&app](const crow::Request& req,
2188 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2189 const std::string& parentIfaceId,
2190 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002191 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002192 {
2193 return;
2194 }
2195 if (!verifyNames(parentIfaceId, ifaceId))
2196 {
2197 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2198 ifaceId);
2199 return;
2200 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002201
Ed Tanous002d39b2022-05-31 08:59:27 -07002202 // Get single eth interface data, and call the below callback
2203 // for JSON preparation
2204 getEthernetIfaceData(
2205 ifaceId,
2206 [asyncResp, parentIfaceId,
2207 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2208 const boost::container::flat_set<IPv4AddressData>&,
2209 const boost::container::flat_set<IPv6AddressData>&) {
2210 if (success && ethData.vlanId)
2211 {
2212 auto callback =
2213 [asyncResp](const boost::system::error_code ec) {
2214 if (ec)
2215 {
2216 messages::internalError(asyncResp->res);
2217 }
2218 };
2219 crow::connections::systemBus->async_method_call(
2220 std::move(callback), "xyz.openbmc_project.Network",
2221 std::string("/xyz/openbmc_project/network/") + ifaceId,
2222 "xyz.openbmc_project.Object.Delete", "Delete");
2223 }
2224 else
2225 {
2226 // ... otherwise return error
2227 // TODO(Pawel)consider distinguish between non
2228 // existing object, and other errors
2229 messages::resourceNotFound(asyncResp->res,
2230 "VLAN Network Interface", ifaceId);
2231 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002232 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002233 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002234
Ed Tanousbf648f72021-06-03 15:00:14 -07002235 BMCWEB_ROUTE(app,
2236 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002237
2238 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002239 .methods(boost::beast::http::verb::get)(
2240 [&app](const crow::Request& req,
2241 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2242 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002243 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002244 {
2245 return;
2246 }
2247 // Get eth interface list, and call the below callback for JSON
2248 // preparation
2249 getEthernetIfaceList(
2250 [asyncResp, rootInterfaceName](
2251 const bool& success,
2252 const boost::container::flat_set<std::string>& ifaceList) {
2253 if (!success)
2254 {
2255 messages::internalError(asyncResp->res);
2256 return;
2257 }
2258
2259 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
2260 {
2261 messages::resourceNotFound(asyncResp->res,
2262 "VLanNetworkInterfaceCollection",
2263 rootInterfaceName);
2264 return;
2265 }
2266
2267 asyncResp->res.jsonValue["@odata.type"] =
2268 "#VLanNetworkInterfaceCollection."
2269 "VLanNetworkInterfaceCollection";
2270 asyncResp->res.jsonValue["Name"] =
2271 "VLAN Network Interface Collection";
2272
2273 nlohmann::json ifaceArray = nlohmann::json::array();
2274
2275 for (const std::string& ifaceItem : ifaceList)
2276 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002277 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002278 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002279 std::string path =
2280 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2281 path += rootInterfaceName;
2282 path += "/VLANs/";
2283 path += ifaceItem;
2284 nlohmann::json::object_t iface;
2285 iface["@odata.id"] = std::move(path);
2286 ifaceArray.push_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002287 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002288 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002289
Ed Tanous002d39b2022-05-31 08:59:27 -07002290 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2291 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2292 asyncResp->res.jsonValue["@odata.id"] =
2293 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2294 rootInterfaceName + "/VLANs";
2295 });
2296 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002297
Ed Tanousbf648f72021-06-03 15:00:14 -07002298 BMCWEB_ROUTE(app,
2299 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002300 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002301 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002302 [&app](const crow::Request& req,
2303 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2304 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002305 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002306 {
2307 return;
2308 }
2309 bool vlanEnable = false;
2310 uint32_t vlanId = 0;
2311 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2312 "VLANEnable", vlanEnable))
2313 {
2314 return;
2315 }
2316 // Need both vlanId and vlanEnable to service this request
2317 if (vlanId == 0U)
2318 {
2319 messages::propertyMissing(asyncResp->res, "VLANId");
2320 }
2321 if (!vlanEnable)
2322 {
2323 messages::propertyMissing(asyncResp->res, "VLANEnable");
2324 }
2325 if (static_cast<bool>(vlanId) ^ vlanEnable)
2326 {
2327 return;
2328 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002329
Ed Tanous002d39b2022-05-31 08:59:27 -07002330 auto callback = [asyncResp](const boost::system::error_code ec) {
2331 if (ec)
2332 {
2333 // TODO(ed) make more consistent error messages
2334 // based on phosphor-network responses
2335 messages::internalError(asyncResp->res);
2336 return;
2337 }
2338 messages::created(asyncResp->res);
2339 };
2340 crow::connections::systemBus->async_method_call(
2341 std::move(callback), "xyz.openbmc_project.Network",
2342 "/xyz/openbmc_project/network",
2343 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2344 rootInterfaceName, vlanId);
2345 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002346}
2347
Ed Tanous1abe55e2018-09-05 08:30:59 -07002348} // namespace redfish