Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 1 | /* |
| 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 Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 18 | #include <app.hpp> |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 19 | #include <boost/algorithm/string/classification.hpp> |
| 20 | #include <boost/algorithm/string/split.hpp> |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_set.hpp> |
Kowalski, Kamil | 179db1d | 2018-04-23 11:12:41 +0200 | [diff] [blame] | 22 | #include <dbus_singleton.hpp> |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 23 | #include <dbus_utility.hpp> |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 24 | #include <error_messages.hpp> |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 25 | #include <query.hpp> |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 26 | #include <registries/privilege_registry.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 27 | #include <utils/json_utils.hpp> |
| 28 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 29 | #include <optional> |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 30 | #include <regex> |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 31 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | namespace redfish |
| 33 | { |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 34 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 35 | enum class LinkType |
| 36 | { |
| 37 | Local, |
| 38 | Global |
| 39 | }; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Structure for keeping IPv4 data required by Redfish |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 43 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 44 | struct IPv4AddressData |
| 45 | { |
| 46 | std::string id; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 47 | std::string address; |
| 48 | std::string domain; |
| 49 | std::string gateway; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | std::string netmask; |
| 51 | std::string origin; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 52 | LinkType linktype; |
Sunitha Harish | 01c6e85 | 2020-03-20 05:04:09 -0500 | [diff] [blame] | 53 | bool isActive; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 54 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 55 | bool operator<(const IPv4AddressData& obj) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 57 | return id < obj.id; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 62 | * Structure for keeping IPv6 data required by Redfish |
| 63 | */ |
| 64 | struct IPv6AddressData |
| 65 | { |
| 66 | std::string id; |
| 67 | std::string address; |
| 68 | std::string origin; |
| 69 | uint8_t prefixLength; |
| 70 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 71 | bool operator<(const IPv6AddressData& obj) const |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 72 | { |
| 73 | return id < obj.id; |
| 74 | } |
| 75 | }; |
| 76 | /** |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 77 | * Structure for keeping basic single Ethernet Interface information |
| 78 | * available from DBus |
| 79 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | struct EthernetInterfaceData |
| 81 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 82 | uint32_t speed; |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 83 | size_t mtuSize; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 84 | bool autoNeg; |
| 85 | bool dnsEnabled; |
| 86 | bool ntpEnabled; |
| 87 | bool hostNameEnabled; |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 88 | bool linkUp; |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 89 | bool nicEnabled; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 90 | std::string dhcpEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 91 | std::string operatingMode; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 92 | std::string hostName; |
| 93 | std::string defaultGateway; |
| 94 | std::string ipv6DefaultGateway; |
| 95 | std::string macAddress; |
Jiaqing Zhao | 17e2202 | 2022-04-14 18:58:06 +0800 | [diff] [blame] | 96 | std::optional<uint32_t> vlanId; |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 97 | std::vector<std::string> nameServers; |
| 98 | std::vector<std::string> staticNameServers; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 99 | std::vector<std::string> domainnames; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 102 | struct DHCPParameters |
| 103 | { |
| 104 | std::optional<bool> dhcpv4Enabled; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 105 | std::optional<bool> useDnsServers; |
| 106 | std::optional<bool> useNtpServers; |
| 107 | std::optional<bool> useDomainName; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 108 | std::optional<std::string> dhcpv6OperatingMode; |
| 109 | }; |
| 110 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 111 | // Helper function that changes bits netmask notation (i.e. /24) |
| 112 | // into full dot notation |
| 113 | inline std::string getNetmask(unsigned int bits) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 115 | 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, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 122 | |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 123 | inline bool translateDhcpEnabledToBool(const std::string& inputDHCP, |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 124 | 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 Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 140 | inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 141 | { |
| 142 | if (isIPv4 && isIPv6) |
| 143 | { |
| 144 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; |
| 145 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 146 | if (isIPv4) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 147 | { |
| 148 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; |
| 149 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 150 | if (isIPv6) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 151 | { |
| 152 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; |
| 153 | } |
| 154 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; |
| 155 | } |
| 156 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 157 | inline std::string |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 158 | translateAddressOriginDbusToRedfish(const std::string& inputOrigin, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 159 | bool isIPv4) |
| 160 | { |
| 161 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 162 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 163 | return "Static"; |
| 164 | } |
| 165 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") |
| 166 | { |
| 167 | if (isIPv4) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 168 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 169 | return "IPv4LinkLocal"; |
| 170 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 171 | return "LinkLocal"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 172 | } |
| 173 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") |
| 174 | { |
| 175 | if (isIPv4) |
| 176 | { |
| 177 | return "DHCP"; |
| 178 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 179 | return "DHCPv6"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 180 | } |
| 181 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") |
| 182 | { |
| 183 | return "SLAAC"; |
| 184 | } |
| 185 | return ""; |
| 186 | } |
| 187 | |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 188 | inline bool extractEthernetInterfaceData( |
| 189 | const std::string& ethifaceId, |
| 190 | const dbus::utility::ManagedObjectType& dbusData, |
| 191 | EthernetInterfaceData& ethData) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 192 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 193 | bool idFound = false; |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 194 | for (const auto& objpath : dbusData) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 195 | { |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 196 | for (const auto& ifacePair : objpath.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 197 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 198 | if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 200 | idFound = true; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 201 | if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 202 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 203 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 205 | if (propertyPair.first == "MACAddress") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 206 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 207 | const std::string* mac = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 208 | std::get_if<std::string>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 209 | if (mac != nullptr) |
| 210 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 211 | ethData.macAddress = *mac; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 212 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 213 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") |
| 217 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 218 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 219 | { |
| 220 | if (propertyPair.first == "Id") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 221 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 222 | const uint32_t* id = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 223 | std::get_if<uint32_t>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 224 | if (id != nullptr) |
| 225 | { |
Jiaqing Zhao | 17e2202 | 2022-04-14 18:58:06 +0800 | [diff] [blame] | 226 | ethData.vlanId = *id; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 227 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 228 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | else if (ifacePair.first == |
| 232 | "xyz.openbmc_project.Network.EthernetInterface") |
| 233 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 234 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 235 | { |
| 236 | if (propertyPair.first == "AutoNeg") |
| 237 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 238 | const bool* autoNeg = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 239 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 240 | if (autoNeg != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 241 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 242 | ethData.autoNeg = *autoNeg; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | else if (propertyPair.first == "Speed") |
| 246 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 247 | const uint32_t* speed = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 248 | std::get_if<uint32_t>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 249 | if (speed != nullptr) |
| 250 | { |
| 251 | ethData.speed = *speed; |
| 252 | } |
| 253 | } |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 254 | 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 Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 263 | else if (propertyPair.first == "LinkUp") |
| 264 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 265 | const bool* linkUp = |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 266 | std::get_if<bool>(&propertyPair.second); |
| 267 | if (linkUp != nullptr) |
| 268 | { |
| 269 | ethData.linkUp = *linkUp; |
| 270 | } |
| 271 | } |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 272 | else if (propertyPair.first == "NICEnabled") |
| 273 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 274 | const bool* nicEnabled = |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 275 | std::get_if<bool>(&propertyPair.second); |
| 276 | if (nicEnabled != nullptr) |
| 277 | { |
| 278 | ethData.nicEnabled = *nicEnabled; |
| 279 | } |
| 280 | } |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 281 | else if (propertyPair.first == "Nameservers") |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 282 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 283 | const std::vector<std::string>* nameservers = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 284 | std::get_if<std::vector<std::string>>( |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 285 | &propertyPair.second); |
| 286 | if (nameservers != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 287 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 288 | ethData.nameServers = *nameservers; |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | else if (propertyPair.first == "StaticNameServers") |
| 292 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 293 | const std::vector<std::string>* staticNameServers = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 294 | std::get_if<std::vector<std::string>>( |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 295 | &propertyPair.second); |
| 296 | if (staticNameServers != nullptr) |
| 297 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 298 | ethData.staticNameServers = *staticNameServers; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 301 | else if (propertyPair.first == "DHCPEnabled") |
| 302 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 303 | const std::string* dhcpEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 304 | std::get_if<std::string>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 305 | if (dhcpEnabled != nullptr) |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 306 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 307 | ethData.dhcpEnabled = *dhcpEnabled; |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 308 | } |
| 309 | } |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 310 | else if (propertyPair.first == "DomainName") |
| 311 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 312 | const std::vector<std::string>* domainNames = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 313 | std::get_if<std::vector<std::string>>( |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 314 | &propertyPair.second); |
| 315 | if (domainNames != nullptr) |
| 316 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 317 | ethData.domainnames = *domainNames; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 320 | 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 Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 329 | ethData.defaultGateway = "0.0.0.0"; |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 330 | } |
| 331 | else |
| 332 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 333 | ethData.defaultGateway = defaultGatewayStr; |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 334 | } |
| 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 Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 347 | ethData.ipv6DefaultGateway = |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 348 | "0:0:0:0:0:0:0:0"; |
| 349 | } |
| 350 | else |
| 351 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 352 | ethData.ipv6DefaultGateway = |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 353 | defaultGateway6Str; |
| 354 | } |
| 355 | } |
| 356 | } |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 360 | |
| 361 | if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") |
| 362 | { |
| 363 | if (ifacePair.first == |
| 364 | "xyz.openbmc_project.Network.DHCPConfiguration") |
| 365 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 366 | for (const auto& propertyPair : ifacePair.second) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 367 | { |
| 368 | if (propertyPair.first == "DNSEnabled") |
| 369 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 370 | const bool* dnsEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 371 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 372 | if (dnsEnabled != nullptr) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 373 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 374 | ethData.dnsEnabled = *dnsEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | else if (propertyPair.first == "NTPEnabled") |
| 378 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 379 | const bool* ntpEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 380 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 381 | if (ntpEnabled != nullptr) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 382 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 383 | ethData.ntpEnabled = *ntpEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | else if (propertyPair.first == "HostNameEnabled") |
| 387 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 388 | const bool* hostNameEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 389 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 390 | if (hostNameEnabled != nullptr) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 391 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 392 | ethData.hostNameEnabled = *hostNameEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 398 | // 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 Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 403 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 404 | { |
| 405 | if (propertyPair.first == "HostName") |
| 406 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 407 | const std::string* hostname = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 408 | std::get_if<std::string>(&propertyPair.second); |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 409 | if (hostname != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 410 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 411 | ethData.hostName = *hostname; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 417 | } |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 418 | return idFound; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 421 | // Helper function that extracts data for single ethernet ipv6 address |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 422 | inline void |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 423 | extractIPV6Data(const std::string& ethifaceId, |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 424 | const dbus::utility::ManagedObjectType& dbusData, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 425 | boost::container::flat_set<IPv6AddressData>& ipv6Config) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 426 | { |
| 427 | const std::string ipv6PathStart = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 428 | "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/"; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 429 | |
| 430 | // Since there might be several IPv6 configurations aligned with |
| 431 | // single ethernet interface, loop over all of them |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 432 | for (const auto& objpath : dbusData) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 433 | { |
| 434 | // Check if proper pattern for object path appears |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 435 | if (objpath.first.str.starts_with(ipv6PathStart)) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 436 | { |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 437 | for (const auto& interface : objpath.second) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 438 | { |
| 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 Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 446 | it = ipv6Config.insert(IPv6AddressData{}); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 447 | IPv6AddressData& ipv6Address = *it.first; |
| 448 | ipv6Address.id = |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 449 | objpath.first.str.substr(ipv6PathStart.size()); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 450 | for (const auto& property : interface.second) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 451 | { |
| 452 | if (property.first == "Address") |
| 453 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 454 | const std::string* address = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 455 | std::get_if<std::string>(&property.second); |
| 456 | if (address != nullptr) |
| 457 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 458 | ipv6Address.address = *address; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | else if (property.first == "Origin") |
| 462 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 463 | const std::string* origin = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 464 | std::get_if<std::string>(&property.second); |
| 465 | if (origin != nullptr) |
| 466 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 467 | ipv6Address.origin = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 468 | translateAddressOriginDbusToRedfish(*origin, |
| 469 | false); |
| 470 | } |
| 471 | } |
| 472 | else if (property.first == "PrefixLength") |
| 473 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 474 | const uint8_t* prefix = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 475 | std::get_if<uint8_t>(&property.second); |
| 476 | if (prefix != nullptr) |
| 477 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 478 | ipv6Address.prefixLength = *prefix; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 479 | } |
| 480 | } |
Asmitha Karunanithi | 889ff69 | 2021-11-29 08:43:30 -0600 | [diff] [blame] | 481 | else if (property.first == "Type" || |
| 482 | property.first == "Gateway") |
| 483 | { |
| 484 | // Type & Gateway is not used |
| 485 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 486 | else |
| 487 | { |
| 488 | BMCWEB_LOG_ERROR |
| 489 | << "Got extra property: " << property.first |
| 490 | << " on the " << objpath.first.str << " object"; |
| 491 | } |
| 492 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 499 | // Helper function that extracts data for single ethernet ipv4 address |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 500 | inline void |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 501 | extractIPData(const std::string& ethifaceId, |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 502 | const dbus::utility::ManagedObjectType& dbusData, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 503 | boost::container::flat_set<IPv4AddressData>& ipv4Config) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 504 | { |
| 505 | const std::string ipv4PathStart = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 506 | "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 507 | |
| 508 | // Since there might be several IPv4 configurations aligned with |
| 509 | // single ethernet interface, loop over all of them |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 510 | for (const auto& objpath : dbusData) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 511 | { |
| 512 | // Check if proper pattern for object path appears |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 513 | if (objpath.first.str.starts_with(ipv4PathStart)) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 514 | { |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 515 | for (const auto& interface : objpath.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 516 | { |
| 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 Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 524 | it = ipv4Config.insert(IPv4AddressData{}); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 525 | IPv4AddressData& ipv4Address = *it.first; |
| 526 | ipv4Address.id = |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 527 | objpath.first.str.substr(ipv4PathStart.size()); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 528 | for (const auto& property : interface.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 529 | { |
| 530 | if (property.first == "Address") |
| 531 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 532 | const std::string* address = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 533 | std::get_if<std::string>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 534 | if (address != nullptr) |
| 535 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 536 | ipv4Address.address = *address; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 539 | else if (property.first == "Origin") |
| 540 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 541 | const std::string* origin = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 542 | std::get_if<std::string>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 543 | if (origin != nullptr) |
| 544 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 545 | ipv4Address.origin = |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 546 | translateAddressOriginDbusToRedfish(*origin, |
| 547 | true); |
| 548 | } |
| 549 | } |
| 550 | else if (property.first == "PrefixLength") |
| 551 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 552 | const uint8_t* mask = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 553 | std::get_if<uint8_t>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 554 | if (mask != nullptr) |
| 555 | { |
| 556 | // convert it to the string |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 557 | ipv4Address.netmask = getNetmask(*mask); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
Asmitha Karunanithi | 889ff69 | 2021-11-29 08:43:30 -0600 | [diff] [blame] | 560 | else if (property.first == "Type" || |
| 561 | property.first == "Gateway") |
| 562 | { |
| 563 | // Type & Gateway is not used |
| 564 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 565 | 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 Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 573 | ipv4Address.linktype = |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 574 | ipv4Address.address.starts_with("169.254.") |
Johnathan Mantey | 18659d1 | 2019-06-07 10:26:29 -0700 | [diff] [blame] | 575 | ? LinkType::Local |
| 576 | : LinkType::Global; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 584 | * @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 Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 593 | inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip, |
| 594 | uint8_t* bits = nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 595 | { |
| 596 | std::vector<std::string> bytesInMask; |
| 597 | |
| 598 | boost::split(bytesInMask, ip, boost::is_any_of(".")); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 599 | |
| 600 | static const constexpr int ipV4AddressSectionsCount = 4; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 601 | if (bytesInMask.size() != ipV4AddressSectionsCount) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 602 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 603 | return false; |
| 604 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 605 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 606 | if (bits != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 607 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 608 | *bits = 0; |
| 609 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 610 | |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 611 | char* endPtr = nullptr; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 612 | long previousValue = 255; |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 613 | bool firstZeroInByteHit = false; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 614 | for (const std::string& byte : bytesInMask) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 615 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 616 | if (byte.empty()) |
| 617 | { |
| 618 | return false; |
| 619 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 620 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 621 | // Use strtol instead of stroi to avoid exceptions |
| 622 | long value = std::strtol(byte.c_str(), &endPtr, 10); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 623 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 624 | // 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 633 | { |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | if (bits != nullptr) |
| 638 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 639 | // Mask has to be continuous between bytes |
| 640 | if (previousValue != 255 && value != 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 641 | { |
| 642 | return false; |
| 643 | } |
| 644 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 645 | // Mask has to be continuous inside bytes |
| 646 | firstZeroInByteHit = false; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 647 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 648 | // Count bits |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 649 | for (long bitIdx = 7; bitIdx >= 0; bitIdx--) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 650 | { |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 651 | if ((value & (1L << bitIdx)) != 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 652 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 653 | if (firstZeroInByteHit) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 654 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 655 | // Continuity not preserved |
| 656 | return false; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 657 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 658 | (*bits)++; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 659 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 660 | else |
| 661 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 662 | firstZeroInByteHit = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 663 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | previousValue = value; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 670 | return true; |
| 671 | } |
| 672 | |
| 673 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 674 | * @brief Deletes given IPv4 interface |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 675 | * |
| 676 | * @param[in] ifaceId Id of interface whose IP should be deleted |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 677 | * @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 Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 682 | inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 683 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 684 | { |
| 685 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 686 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 687 | if (ec) |
| 688 | { |
| 689 | messages::internalError(asyncResp->res); |
| 690 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 691 | }, |
| 692 | "xyz.openbmc_project.Network", |
| 693 | "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, |
| 694 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 695 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 696 | |
Gunnar Mills | 244b6d5 | 2021-04-12 15:44:23 -0500 | [diff] [blame] | 697 | inline void updateIPv4DefaultGateway( |
| 698 | const std::string& ifaceId, const std::string& gateway, |
| 699 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 700 | { |
| 701 | crow::connections::systemBus->async_method_call( |
| 702 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 703 | if (ec) |
| 704 | { |
| 705 | messages::internalError(asyncResp->res); |
| 706 | return; |
| 707 | } |
| 708 | asyncResp->res.result(boost::beast::http::status::no_content); |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 709 | }, |
| 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 Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 714 | dbus::utility::DbusVariantType(gateway)); |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 715 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 716 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 717 | * @brief Creates a static IPv4 entry |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 718 | * |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 719 | * @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 Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 724 | * |
| 725 | * @return None |
| 726 | */ |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 727 | inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, |
| 728 | const std::string& gateway, const std::string& address, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 729 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 730 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 731 | auto createIpHandler = |
| 732 | [asyncResp, ifaceId, gateway](const boost::system::error_code ec) { |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 733 | if (ec) |
| 734 | { |
| 735 | messages::internalError(asyncResp->res); |
| 736 | return; |
| 737 | } |
| 738 | updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); |
| 739 | }; |
| 740 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 741 | crow::connections::systemBus->async_method_call( |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 742 | std::move(createIpHandler), "xyz.openbmc_project.Network", |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 743 | "/xyz/openbmc_project/network/" + ifaceId, |
| 744 | "xyz.openbmc_project.Network.IP.Create", "IP", |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 745 | "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 746 | gateway); |
| 747 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 748 | |
| 749 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 750 | * @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 | */ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 762 | inline 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 Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 767 | { |
| 768 | crow::connections::systemBus->async_method_call( |
| 769 | [asyncResp, ifaceId, address, prefixLength, |
| 770 | gateway](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 771 | 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 Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 780 | { |
| 781 | messages::internalError(asyncResp->res); |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 782 | return; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 783 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 784 | 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 Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 791 | }, |
| 792 | "xyz.openbmc_project.Network", |
| 793 | +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, |
| 794 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 795 | } |
| 796 | |
| 797 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 798 | * @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 Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 802 | * @param[io] asyncResp Response object that will be returned to client |
| 803 | * |
| 804 | * @return None |
| 805 | */ |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 806 | inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 807 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 808 | { |
| 809 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 810 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 811 | if (ec) |
| 812 | { |
| 813 | messages::internalError(asyncResp->res); |
| 814 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 815 | }, |
| 816 | "xyz.openbmc_project.Network", |
| 817 | "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, |
| 818 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 819 | } |
| 820 | |
| 821 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 822 | * @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 | */ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 833 | inline 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 Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 837 | { |
| 838 | crow::connections::systemBus->async_method_call( |
| 839 | [asyncResp, ifaceId, address, |
| 840 | prefixLength](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 841 | 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 Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 848 | { |
| 849 | messages::internalError(asyncResp->res); |
| 850 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 851 | }, |
| 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 Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 857 | }, |
| 858 | "xyz.openbmc_project.Network", |
| 859 | +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, |
| 860 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 861 | } |
| 862 | |
| 863 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 864 | * @brief Creates IPv6 with given data |
| 865 | * |
| 866 | * @param[in] ifaceId Id of interface whose IP should be added |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 867 | * @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 Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 873 | inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, |
| 874 | const std::string& address, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 875 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 876 | { |
| 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 Mills | 4e0453b | 2020-07-08 14:00:30 -0500 | [diff] [blame] | 884 | // does not have associated gateway property |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 885 | 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 Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 893 | /** |
| 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 | */ |
| 901 | template <typename CallbackFunc> |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 902 | void getEthernetIfaceData(const std::string& ethifaceId, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 903 | CallbackFunc&& callback) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 904 | { |
| 905 | crow::connections::systemBus->async_method_call( |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 906 | [ethifaceId{std::string{ethifaceId}}, |
| 907 | callback{std::forward<CallbackFunc>(callback)}]( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 908 | const boost::system::error_code errorCode, |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 909 | const dbus::utility::ManagedObjectType& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 910 | EthernetInterfaceData ethData{}; |
| 911 | boost::container::flat_set<IPv4AddressData> ipv4Data; |
| 912 | boost::container::flat_set<IPv6AddressData> ipv6Data; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 913 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 914 | 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 Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 934 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 935 | ipv4.gateway = ethData.defaultGateway; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 936 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 937 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 938 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 939 | extractIPV6Data(ethifaceId, resp, ipv6Data); |
| 940 | // Finally make a callback with useful data |
| 941 | callback(true, ethData, ipv4Data, ipv6Data); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 942 | }, |
| 943 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", |
| 944 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 945 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 946 | |
| 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 | */ |
| 953 | template <typename CallbackFunc> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 954 | void getEthernetIfaceList(CallbackFunc&& callback) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 955 | { |
| 956 | crow::connections::systemBus->async_method_call( |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 957 | [callback{std::forward<CallbackFunc>(callback)}]( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 958 | const boost::system::error_code errorCode, |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 959 | dbus::utility::ManagedObjectType& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 960 | // 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 Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 969 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 970 | // 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 Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 975 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 976 | // If interface is |
| 977 | // xyz.openbmc_project.Network.EthernetInterface, this is |
| 978 | // what we're looking for. |
| 979 | if (interface.first == |
| 980 | "xyz.openbmc_project.Network.EthernetInterface") |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 981 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 982 | std::string ifaceId = objpath.first.filename(); |
| 983 | if (ifaceId.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 984 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 985 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 986 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 987 | // and put it into output vector. |
| 988 | ifaceList.emplace(ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 989 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 990 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 991 | } |
| 992 | // Finally make a callback with useful data |
| 993 | callback(true, ifaceList); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 994 | }, |
| 995 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", |
| 996 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 997 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 998 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 999 | inline void |
| 1000 | handleHostnamePatch(const std::string& hostname, |
| 1001 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1002 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1003 | // SHOULD handle host names of up to 255 characters(RFC 1123) |
| 1004 | if (hostname.length() > 255) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1005 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1006 | 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 Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1012 | if (ec) |
| 1013 | { |
| 1014 | messages::internalError(asyncResp->res); |
| 1015 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1016 | }, |
| 1017 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", |
| 1018 | "org.freedesktop.DBus.Properties", "Set", |
| 1019 | "xyz.openbmc_project.Network.SystemConfiguration", "HostName", |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 1020 | dbus::utility::DbusVariantType(hostname)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1023 | inline void |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 1024 | 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 Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1031 | if (ec) |
| 1032 | { |
| 1033 | messages::internalError(asyncResp->res); |
| 1034 | } |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 1035 | }, |
| 1036 | "xyz.openbmc_project.Network", objPath, |
| 1037 | "org.freedesktop.DBus.Properties", "Set", |
| 1038 | "xyz.openbmc_project.Network.EthernetInterface", "MTU", |
| 1039 | std::variant<size_t>(mtuSize)); |
| 1040 | } |
| 1041 | |
| 1042 | inline void |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1043 | handleDomainnamePatch(const std::string& ifaceId, |
| 1044 | const std::string& domainname, |
| 1045 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1046 | { |
| 1047 | std::vector<std::string> vectorDomainname = {domainname}; |
| 1048 | crow::connections::systemBus->async_method_call( |
| 1049 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1050 | if (ec) |
| 1051 | { |
| 1052 | messages::internalError(asyncResp->res); |
| 1053 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1054 | }, |
| 1055 | "xyz.openbmc_project.Network", |
| 1056 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1057 | "org.freedesktop.DBus.Properties", "Set", |
| 1058 | "xyz.openbmc_project.Network.EthernetInterface", "DomainName", |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 1059 | dbus::utility::DbusVariantType(vectorDomainname)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1062 | inline bool isHostnameValid(const std::string& hostname) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1063 | { |
| 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 Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1079 | inline bool isDomainnameValid(const std::string& domainname) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1080 | { |
| 1081 | // Can have multiple subdomains |
| 1082 | // Top Level Domain's min length is 2 character |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1083 | 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 Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1085 | |
| 1086 | return std::regex_match(domainname, pattern); |
| 1087 | } |
| 1088 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1089 | inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, |
| 1090 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1091 | { |
| 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1099 | size_t pos = fqdn.find('.'); |
| 1100 | if (pos == std::string::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1101 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1102 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 1103 | return; |
| 1104 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1105 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1106 | 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 Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1121 | inline void |
| 1122 | handleMACAddressPatch(const std::string& ifaceId, |
| 1123 | const std::string& macAddress, |
| 1124 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1125 | { |
Johnathan Mantey | 58283f4 | 2022-08-15 14:38:36 -0700 | [diff] [blame^] | 1126 | static constexpr std::string_view dbusNotAllowedError = |
| 1127 | "xyz.openbmc_project.Common.Error.NotAllowed"; |
| 1128 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1129 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 58283f4 | 2022-08-15 14:38:36 -0700 | [diff] [blame^] | 1130 | [asyncResp, macAddress](const boost::system::error_code ec, |
| 1131 | const sdbusplus::message::message& msg) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1132 | if (ec) |
| 1133 | { |
Johnathan Mantey | 58283f4 | 2022-08-15 14:38:36 -0700 | [diff] [blame^] | 1134 | const sd_bus_error* err = msg.get_error(); |
| 1135 | if (err == nullptr) |
| 1136 | { |
| 1137 | messages::internalError(asyncResp->res); |
| 1138 | return; |
| 1139 | } |
| 1140 | if (err->name == dbusNotAllowedError) |
| 1141 | { |
| 1142 | messages::propertyNotWritable(asyncResp->res, "MACAddress"); |
| 1143 | return; |
| 1144 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1145 | messages::internalError(asyncResp->res); |
| 1146 | return; |
| 1147 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1148 | }, |
| 1149 | "xyz.openbmc_project.Network", |
| 1150 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1151 | "org.freedesktop.DBus.Properties", "Set", |
| 1152 | "xyz.openbmc_project.Network.MACAddress", "MACAddress", |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 1153 | dbus::utility::DbusVariantType(macAddress)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1156 | inline void setDHCPEnabled(const std::string& ifaceId, |
| 1157 | const std::string& propertyName, const bool v4Value, |
| 1158 | const bool v6Value, |
| 1159 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1160 | { |
| 1161 | const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); |
| 1162 | crow::connections::systemBus->async_method_call( |
| 1163 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1164 | if (ec) |
| 1165 | { |
| 1166 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1167 | messages::internalError(asyncResp->res); |
| 1168 | return; |
| 1169 | } |
| 1170 | messages::success(asyncResp->res); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1171 | }, |
| 1172 | "xyz.openbmc_project.Network", |
| 1173 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1174 | "org.freedesktop.DBus.Properties", "Set", |
| 1175 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 1176 | dbus::utility::DbusVariantType{dhcp}); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1179 | inline void setEthernetInterfaceBoolProperty( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1180 | const std::string& ifaceId, const std::string& propertyName, |
| 1181 | const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1182 | { |
| 1183 | crow::connections::systemBus->async_method_call( |
| 1184 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1185 | if (ec) |
| 1186 | { |
| 1187 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1188 | messages::internalError(asyncResp->res); |
| 1189 | return; |
| 1190 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1191 | }, |
| 1192 | "xyz.openbmc_project.Network", |
| 1193 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1194 | "org.freedesktop.DBus.Properties", "Set", |
| 1195 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 1196 | dbus::utility::DbusVariantType{value}); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1199 | inline void setDHCPv4Config(const std::string& propertyName, const bool& value, |
| 1200 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1201 | { |
| 1202 | BMCWEB_LOG_DEBUG << propertyName << " = " << value; |
| 1203 | crow::connections::systemBus->async_method_call( |
| 1204 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1205 | if (ec) |
| 1206 | { |
| 1207 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1208 | messages::internalError(asyncResp->res); |
| 1209 | return; |
| 1210 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1211 | }, |
| 1212 | "xyz.openbmc_project.Network", |
| 1213 | "/xyz/openbmc_project/network/config/dhcp", |
| 1214 | "org.freedesktop.DBus.Properties", "Set", |
| 1215 | "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 1216 | dbus::utility::DbusVariantType{value}); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1219 | inline void handleDHCPPatch(const std::string& ifaceId, |
| 1220 | const EthernetInterfaceData& ethData, |
| 1221 | const DHCPParameters& v4dhcpParms, |
| 1222 | const DHCPParameters& v6dhcpParms, |
| 1223 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1224 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1225 | bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true); |
| 1226 | bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1227 | |
| 1228 | bool nextv4DHCPState = |
| 1229 | v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; |
| 1230 | |
| 1231 | bool nextv6DHCPState{}; |
| 1232 | if (v6dhcpParms.dhcpv6OperatingMode) |
| 1233 | { |
| 1234 | if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && |
| 1235 | (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && |
| 1236 | (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) |
| 1237 | { |
| 1238 | messages::propertyValueFormatError(asyncResp->res, |
| 1239 | *v6dhcpParms.dhcpv6OperatingMode, |
| 1240 | "OperatingMode"); |
| 1241 | return; |
| 1242 | } |
| 1243 | nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); |
| 1244 | } |
| 1245 | else |
| 1246 | { |
| 1247 | nextv6DHCPState = ipv6Active; |
| 1248 | } |
| 1249 | |
| 1250 | bool nextDNS{}; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1251 | if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1252 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1253 | if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1254 | { |
| 1255 | messages::generalError(asyncResp->res); |
| 1256 | return; |
| 1257 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1258 | nextDNS = *v4dhcpParms.useDnsServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1259 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1260 | else if (v4dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1261 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1262 | nextDNS = *v4dhcpParms.useDnsServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1263 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1264 | else if (v6dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1265 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1266 | nextDNS = *v6dhcpParms.useDnsServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1267 | } |
| 1268 | else |
| 1269 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1270 | nextDNS = ethData.dnsEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | bool nextNTP{}; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1274 | if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1275 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1276 | if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1277 | { |
| 1278 | messages::generalError(asyncResp->res); |
| 1279 | return; |
| 1280 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1281 | nextNTP = *v4dhcpParms.useNtpServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1282 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1283 | else if (v4dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1284 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1285 | nextNTP = *v4dhcpParms.useNtpServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1286 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1287 | else if (v6dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1288 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1289 | nextNTP = *v6dhcpParms.useNtpServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1290 | } |
| 1291 | else |
| 1292 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1293 | nextNTP = ethData.ntpEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | bool nextUseDomain{}; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1297 | if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1298 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1299 | if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1300 | { |
| 1301 | messages::generalError(asyncResp->res); |
| 1302 | return; |
| 1303 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1304 | nextUseDomain = *v4dhcpParms.useDomainName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1305 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1306 | else if (v4dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1307 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1308 | nextUseDomain = *v4dhcpParms.useDomainName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1309 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1310 | else if (v6dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1311 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1312 | nextUseDomain = *v6dhcpParms.useDomainName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1313 | } |
| 1314 | else |
| 1315 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1316 | nextUseDomain = ethData.hostNameEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; |
| 1320 | setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, |
| 1321 | asyncResp); |
| 1322 | BMCWEB_LOG_DEBUG << "set DNSEnabled..."; |
| 1323 | setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); |
| 1324 | BMCWEB_LOG_DEBUG << "set NTPEnabled..."; |
| 1325 | setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); |
| 1326 | BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; |
| 1327 | setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); |
| 1328 | } |
| 1329 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1330 | inline boost::container::flat_set<IPv4AddressData>::const_iterator |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1331 | getNextStaticIpEntry( |
| 1332 | const boost::container::flat_set<IPv4AddressData>::const_iterator& head, |
| 1333 | const boost::container::flat_set<IPv4AddressData>::const_iterator& end) |
| 1334 | { |
| 1335 | return std::find_if(head, end, [](const IPv4AddressData& value) { |
| 1336 | return value.origin == "Static"; |
| 1337 | }); |
| 1338 | } |
| 1339 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1340 | inline boost::container::flat_set<IPv6AddressData>::const_iterator |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1341 | getNextStaticIpEntry( |
| 1342 | const boost::container::flat_set<IPv6AddressData>::const_iterator& head, |
| 1343 | const boost::container::flat_set<IPv6AddressData>::const_iterator& end) |
| 1344 | { |
| 1345 | return std::find_if(head, end, [](const IPv6AddressData& value) { |
| 1346 | return value.origin == "Static"; |
| 1347 | }); |
| 1348 | } |
| 1349 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1350 | inline void handleIPv4StaticPatch( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1351 | const std::string& ifaceId, nlohmann::json& input, |
| 1352 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
| 1353 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1354 | { |
| 1355 | if ((!input.is_array()) || input.empty()) |
| 1356 | { |
| 1357 | messages::propertyValueTypeError( |
| 1358 | asyncResp->res, |
| 1359 | input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), |
| 1360 | "IPv4StaticAddresses"); |
| 1361 | return; |
| 1362 | } |
| 1363 | |
| 1364 | unsigned entryIdx = 1; |
| 1365 | // Find the first static IP address currently active on the NIC and |
| 1366 | // match it to the first JSON element in the IPv4StaticAddresses array. |
| 1367 | // Match each subsequent JSON element to the next static IP programmed |
| 1368 | // into the NIC. |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1369 | boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry = |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1370 | getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); |
| 1371 | |
| 1372 | for (nlohmann::json& thisJson : input) |
| 1373 | { |
| 1374 | std::string pathString = |
| 1375 | "IPv4StaticAddresses/" + std::to_string(entryIdx); |
| 1376 | |
| 1377 | if (!thisJson.is_null() && !thisJson.empty()) |
| 1378 | { |
| 1379 | std::optional<std::string> address; |
| 1380 | std::optional<std::string> subnetMask; |
| 1381 | std::optional<std::string> gateway; |
| 1382 | |
| 1383 | if (!json_util::readJson(thisJson, asyncResp->res, "Address", |
| 1384 | address, "SubnetMask", subnetMask, |
| 1385 | "Gateway", gateway)) |
| 1386 | { |
| 1387 | messages::propertyValueFormatError( |
| 1388 | asyncResp->res, |
| 1389 | thisJson.dump(2, ' ', true, |
| 1390 | nlohmann::json::error_handler_t::replace), |
| 1391 | pathString); |
| 1392 | return; |
| 1393 | } |
| 1394 | |
| 1395 | // Find the address/subnet/gateway values. Any values that are |
| 1396 | // not explicitly provided are assumed to be unmodified from the |
| 1397 | // current state of the interface. Merge existing state into the |
| 1398 | // current request. |
| 1399 | const std::string* addr = nullptr; |
| 1400 | const std::string* gw = nullptr; |
| 1401 | uint8_t prefixLength = 0; |
| 1402 | bool errorInEntry = false; |
| 1403 | if (address) |
| 1404 | { |
| 1405 | if (ipv4VerifyIpAndGetBitcount(*address)) |
| 1406 | { |
| 1407 | addr = &(*address); |
| 1408 | } |
| 1409 | else |
| 1410 | { |
| 1411 | messages::propertyValueFormatError(asyncResp->res, *address, |
| 1412 | pathString + "/Address"); |
| 1413 | errorInEntry = true; |
| 1414 | } |
| 1415 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1416 | else if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1417 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1418 | addr = &(nicIpEntry->address); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1419 | } |
| 1420 | else |
| 1421 | { |
| 1422 | messages::propertyMissing(asyncResp->res, |
| 1423 | pathString + "/Address"); |
| 1424 | errorInEntry = true; |
| 1425 | } |
| 1426 | |
| 1427 | if (subnetMask) |
| 1428 | { |
| 1429 | if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) |
| 1430 | { |
| 1431 | messages::propertyValueFormatError( |
| 1432 | asyncResp->res, *subnetMask, |
| 1433 | pathString + "/SubnetMask"); |
| 1434 | errorInEntry = true; |
| 1435 | } |
| 1436 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1437 | else if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1438 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1439 | if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1440 | &prefixLength)) |
| 1441 | { |
| 1442 | messages::propertyValueFormatError( |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1443 | asyncResp->res, nicIpEntry->netmask, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1444 | pathString + "/SubnetMask"); |
| 1445 | errorInEntry = true; |
| 1446 | } |
| 1447 | } |
| 1448 | else |
| 1449 | { |
| 1450 | messages::propertyMissing(asyncResp->res, |
| 1451 | pathString + "/SubnetMask"); |
| 1452 | errorInEntry = true; |
| 1453 | } |
| 1454 | |
| 1455 | if (gateway) |
| 1456 | { |
| 1457 | if (ipv4VerifyIpAndGetBitcount(*gateway)) |
| 1458 | { |
| 1459 | gw = &(*gateway); |
| 1460 | } |
| 1461 | else |
| 1462 | { |
| 1463 | messages::propertyValueFormatError(asyncResp->res, *gateway, |
| 1464 | pathString + "/Gateway"); |
| 1465 | errorInEntry = true; |
| 1466 | } |
| 1467 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1468 | else if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1469 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1470 | gw = &nicIpEntry->gateway; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1471 | } |
| 1472 | else |
| 1473 | { |
| 1474 | messages::propertyMissing(asyncResp->res, |
| 1475 | pathString + "/Gateway"); |
| 1476 | errorInEntry = true; |
| 1477 | } |
| 1478 | |
| 1479 | if (errorInEntry) |
| 1480 | { |
| 1481 | return; |
| 1482 | } |
| 1483 | |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1484 | if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1485 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1486 | deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1487 | *addr, asyncResp); |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1488 | nicIpEntry = |
| 1489 | getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1490 | } |
| 1491 | else |
| 1492 | { |
| 1493 | createIPv4(ifaceId, prefixLength, *gateway, *address, |
| 1494 | asyncResp); |
| 1495 | } |
| 1496 | entryIdx++; |
| 1497 | } |
| 1498 | else |
| 1499 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1500 | if (nicIpEntry == ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1501 | { |
| 1502 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1503 | // that isn't present on the eth(n) interface. Input JSON is |
| 1504 | // in error, so bail out. |
| 1505 | if (thisJson.is_null()) |
| 1506 | { |
| 1507 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1508 | return; |
| 1509 | } |
| 1510 | messages::propertyValueFormatError( |
| 1511 | asyncResp->res, |
| 1512 | thisJson.dump(2, ' ', true, |
| 1513 | nlohmann::json::error_handler_t::replace), |
| 1514 | pathString); |
| 1515 | return; |
| 1516 | } |
| 1517 | |
| 1518 | if (thisJson.is_null()) |
| 1519 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1520 | deleteIPv4(ifaceId, nicIpEntry->id, asyncResp); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1521 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1522 | if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1523 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1524 | nicIpEntry = |
| 1525 | getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1526 | } |
| 1527 | entryIdx++; |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1532 | inline void handleStaticNameServersPatch( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1533 | const std::string& ifaceId, |
| 1534 | const std::vector<std::string>& updatedStaticNameServers, |
| 1535 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1536 | { |
| 1537 | crow::connections::systemBus->async_method_call( |
| 1538 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1539 | if (ec) |
| 1540 | { |
| 1541 | messages::internalError(asyncResp->res); |
| 1542 | return; |
| 1543 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1544 | }, |
| 1545 | "xyz.openbmc_project.Network", |
| 1546 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1547 | "org.freedesktop.DBus.Properties", "Set", |
| 1548 | "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 1549 | dbus::utility::DbusVariantType{updatedStaticNameServers}); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1552 | inline void handleIPv6StaticAddressesPatch( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1553 | const std::string& ifaceId, const nlohmann::json& input, |
| 1554 | const boost::container::flat_set<IPv6AddressData>& ipv6Data, |
| 1555 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1556 | { |
| 1557 | if (!input.is_array() || input.empty()) |
| 1558 | { |
| 1559 | messages::propertyValueTypeError( |
| 1560 | asyncResp->res, |
| 1561 | input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), |
| 1562 | "IPv6StaticAddresses"); |
| 1563 | return; |
| 1564 | } |
| 1565 | size_t entryIdx = 1; |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1566 | boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry = |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1567 | getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); |
| 1568 | for (const nlohmann::json& thisJson : input) |
| 1569 | { |
| 1570 | std::string pathString = |
| 1571 | "IPv6StaticAddresses/" + std::to_string(entryIdx); |
| 1572 | |
| 1573 | if (!thisJson.is_null() && !thisJson.empty()) |
| 1574 | { |
| 1575 | std::optional<std::string> address; |
| 1576 | std::optional<uint8_t> prefixLength; |
| 1577 | nlohmann::json thisJsonCopy = thisJson; |
| 1578 | if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", |
| 1579 | address, "PrefixLength", prefixLength)) |
| 1580 | { |
| 1581 | messages::propertyValueFormatError( |
| 1582 | asyncResp->res, |
| 1583 | thisJson.dump(2, ' ', true, |
| 1584 | nlohmann::json::error_handler_t::replace), |
| 1585 | pathString); |
| 1586 | return; |
| 1587 | } |
| 1588 | |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 1589 | const std::string* addr = nullptr; |
| 1590 | uint8_t prefix = 0; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1591 | |
| 1592 | // Find the address and prefixLength values. Any values that are |
| 1593 | // not explicitly provided are assumed to be unmodified from the |
| 1594 | // current state of the interface. Merge existing state into the |
| 1595 | // current request. |
| 1596 | if (address) |
| 1597 | { |
| 1598 | addr = &(*address); |
| 1599 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1600 | else if (nicIpEntry != ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1601 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1602 | addr = &(nicIpEntry->address); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1603 | } |
| 1604 | else |
| 1605 | { |
| 1606 | messages::propertyMissing(asyncResp->res, |
| 1607 | pathString + "/Address"); |
| 1608 | return; |
| 1609 | } |
| 1610 | |
| 1611 | if (prefixLength) |
| 1612 | { |
| 1613 | prefix = *prefixLength; |
| 1614 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1615 | else if (nicIpEntry != ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1616 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1617 | prefix = nicIpEntry->prefixLength; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1618 | } |
| 1619 | else |
| 1620 | { |
| 1621 | messages::propertyMissing(asyncResp->res, |
| 1622 | pathString + "/PrefixLength"); |
| 1623 | return; |
| 1624 | } |
| 1625 | |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1626 | if (nicIpEntry != ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1627 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1628 | deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1629 | asyncResp); |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1630 | nicIpEntry = |
| 1631 | getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1632 | } |
| 1633 | else |
| 1634 | { |
| 1635 | createIPv6(ifaceId, *prefixLength, *addr, asyncResp); |
| 1636 | } |
| 1637 | entryIdx++; |
| 1638 | } |
| 1639 | else |
| 1640 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1641 | if (nicIpEntry == ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1642 | { |
| 1643 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1644 | // that isn't present on the eth(n) interface. Input JSON is |
| 1645 | // in error, so bail out. |
| 1646 | if (thisJson.is_null()) |
| 1647 | { |
| 1648 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1649 | return; |
| 1650 | } |
| 1651 | messages::propertyValueFormatError( |
| 1652 | asyncResp->res, |
| 1653 | thisJson.dump(2, ' ', true, |
| 1654 | nlohmann::json::error_handler_t::replace), |
| 1655 | pathString); |
| 1656 | return; |
| 1657 | } |
| 1658 | |
| 1659 | if (thisJson.is_null()) |
| 1660 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1661 | deleteIPv6(ifaceId, nicIpEntry->id, asyncResp); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1662 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1663 | if (nicIpEntry != ipv6Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1664 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1665 | nicIpEntry = |
| 1666 | getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1667 | } |
| 1668 | entryIdx++; |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1673 | inline void parseInterfaceData( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1674 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1675 | const std::string& ifaceId, const EthernetInterfaceData& ethData, |
| 1676 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
| 1677 | const boost::container::flat_set<IPv6AddressData>& ipv6Data) |
| 1678 | { |
| 1679 | constexpr const std::array<const char*, 1> inventoryForEthernet = { |
| 1680 | "xyz.openbmc_project.Inventory.Item.Ethernet"}; |
| 1681 | |
| 1682 | nlohmann::json& jsonResponse = asyncResp->res.jsonValue; |
| 1683 | jsonResponse["Id"] = ifaceId; |
| 1684 | jsonResponse["@odata.id"] = |
| 1685 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; |
| 1686 | jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; |
| 1687 | |
| 1688 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 1689 | |
| 1690 | crow::connections::systemBus->async_method_call( |
| 1691 | [health](const boost::system::error_code ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1692 | const dbus::utility::MapperGetSubTreePathsResponse& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1693 | if (ec) |
| 1694 | { |
| 1695 | return; |
| 1696 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1697 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1698 | health->inventory = resp; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1699 | }, |
| 1700 | "xyz.openbmc_project.ObjectMapper", |
| 1701 | "/xyz/openbmc_project/object_mapper", |
| 1702 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0), |
| 1703 | inventoryForEthernet); |
| 1704 | |
| 1705 | health->populate(); |
| 1706 | |
| 1707 | if (ethData.nicEnabled) |
| 1708 | { |
| 1709 | jsonResponse["LinkStatus"] = "LinkUp"; |
| 1710 | jsonResponse["Status"]["State"] = "Enabled"; |
| 1711 | } |
| 1712 | else |
| 1713 | { |
| 1714 | jsonResponse["LinkStatus"] = "NoLink"; |
| 1715 | jsonResponse["Status"]["State"] = "Disabled"; |
| 1716 | } |
| 1717 | |
| 1718 | jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; |
| 1719 | jsonResponse["SpeedMbps"] = ethData.speed; |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 1720 | jsonResponse["MTUSize"] = ethData.mtuSize; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1721 | jsonResponse["MACAddress"] = ethData.macAddress; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1722 | jsonResponse["DHCPv4"]["DHCPEnabled"] = |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1723 | translateDhcpEnabledToBool(ethData.dhcpEnabled, true); |
| 1724 | jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled; |
| 1725 | jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled; |
| 1726 | jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1727 | |
| 1728 | jsonResponse["DHCPv6"]["OperatingMode"] = |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1729 | translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful" |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1730 | : "Disabled"; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1731 | jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled; |
| 1732 | jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled; |
| 1733 | jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1734 | |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1735 | if (!ethData.hostName.empty()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1736 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1737 | jsonResponse["HostName"] = ethData.hostName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1738 | |
| 1739 | // When domain name is empty then it means, that it is a network |
| 1740 | // without domain names, and the host name itself must be treated as |
| 1741 | // FQDN |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1742 | std::string fqdn = ethData.hostName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1743 | if (!ethData.domainnames.empty()) |
| 1744 | { |
| 1745 | fqdn += "." + ethData.domainnames[0]; |
| 1746 | } |
| 1747 | jsonResponse["FQDN"] = fqdn; |
| 1748 | } |
| 1749 | |
| 1750 | jsonResponse["VLANs"] = { |
| 1751 | {"@odata.id", |
| 1752 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}}; |
| 1753 | |
| 1754 | jsonResponse["NameServers"] = ethData.nameServers; |
| 1755 | jsonResponse["StaticNameServers"] = ethData.staticNameServers; |
| 1756 | |
| 1757 | nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; |
| 1758 | nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; |
| 1759 | ipv4Array = nlohmann::json::array(); |
| 1760 | ipv4StaticArray = nlohmann::json::array(); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 1761 | for (const auto& ipv4Config : ipv4Data) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1762 | { |
| 1763 | |
| 1764 | std::string gatewayStr = ipv4Config.gateway; |
| 1765 | if (gatewayStr.empty()) |
| 1766 | { |
| 1767 | gatewayStr = "0.0.0.0"; |
| 1768 | } |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1769 | nlohmann::json::object_t ipv4; |
| 1770 | ipv4["AddressOrigin"] = ipv4Config.origin; |
| 1771 | ipv4["SubnetMask"] = ipv4Config.netmask; |
| 1772 | ipv4["Address"] = ipv4Config.address; |
| 1773 | ipv4["Gateway"] = gatewayStr; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1774 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1775 | if (ipv4Config.origin == "Static") |
| 1776 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1777 | ipv4StaticArray.push_back(ipv4); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1778 | } |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1779 | |
| 1780 | ipv4Array.push_back(std::move(ipv4)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1783 | std::string ipv6GatewayStr = ethData.ipv6DefaultGateway; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1784 | if (ipv6GatewayStr.empty()) |
| 1785 | { |
| 1786 | ipv6GatewayStr = "0:0:0:0:0:0:0:0"; |
| 1787 | } |
| 1788 | |
| 1789 | jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; |
| 1790 | |
| 1791 | nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; |
| 1792 | nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; |
| 1793 | ipv6Array = nlohmann::json::array(); |
| 1794 | ipv6StaticArray = nlohmann::json::array(); |
| 1795 | nlohmann::json& ipv6AddrPolicyTable = |
| 1796 | jsonResponse["IPv6AddressPolicyTable"]; |
| 1797 | ipv6AddrPolicyTable = nlohmann::json::array(); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 1798 | for (const auto& ipv6Config : ipv6Data) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1799 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1800 | nlohmann::json::object_t ipv6; |
| 1801 | ipv6["Address"] = ipv6Config.address; |
| 1802 | ipv6["PrefixLength"] = ipv6Config.prefixLength; |
| 1803 | ipv6["AddressOrigin"] = ipv6Config.origin; |
| 1804 | ipv6["AddressState"] = nullptr; |
| 1805 | ipv6Array.push_back(std::move(ipv6)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1806 | if (ipv6Config.origin == "Static") |
| 1807 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1808 | nlohmann::json::object_t ipv6Static; |
| 1809 | ipv6Static["Address"] = ipv6Config.address; |
| 1810 | ipv6Static["PrefixLength"] = ipv6Config.prefixLength; |
| 1811 | ipv6StaticArray.push_back(std::move(ipv6Static)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1816 | inline void parseInterfaceData(nlohmann::json& jsonResponse, |
| 1817 | const std::string& parentIfaceId, |
| 1818 | const std::string& ifaceId, |
| 1819 | const EthernetInterfaceData& ethData) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1820 | { |
| 1821 | // Fill out obvious data... |
| 1822 | jsonResponse["Id"] = ifaceId; |
| 1823 | jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 1824 | parentIfaceId + "/VLANs/" + ifaceId; |
| 1825 | |
| 1826 | jsonResponse["VLANEnable"] = true; |
Jiaqing Zhao | 17e2202 | 2022-04-14 18:58:06 +0800 | [diff] [blame] | 1827 | if (ethData.vlanId) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1828 | { |
Jiaqing Zhao | 17e2202 | 2022-04-14 18:58:06 +0800 | [diff] [blame] | 1829 | jsonResponse["VLANId"] = *ethData.vlanId; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1833 | inline bool verifyNames(const std::string& parent, const std::string& iface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1834 | { |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 1835 | return iface.starts_with(parent + "_"); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | inline void requestEthernetInterfacesRoutes(App& app) |
| 1839 | { |
| 1840 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1841 | .privileges(redfish::privileges::getEthernetInterfaceCollection) |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1842 | .methods(boost::beast::http::verb::get)( |
| 1843 | [&app](const crow::Request& req, |
| 1844 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1845 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1846 | { |
| 1847 | return; |
| 1848 | } |
| 1849 | |
| 1850 | asyncResp->res.jsonValue["@odata.type"] = |
| 1851 | "#EthernetInterfaceCollection.EthernetInterfaceCollection"; |
| 1852 | asyncResp->res.jsonValue["@odata.id"] = |
| 1853 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
| 1854 | asyncResp->res.jsonValue["Name"] = |
| 1855 | "Ethernet Network Interface Collection"; |
| 1856 | asyncResp->res.jsonValue["Description"] = |
| 1857 | "Collection of EthernetInterfaces for this Manager"; |
| 1858 | |
| 1859 | // Get eth interface list, and call the below callback for JSON |
| 1860 | // preparation |
| 1861 | getEthernetIfaceList( |
| 1862 | [asyncResp]( |
| 1863 | const bool& success, |
| 1864 | const boost::container::flat_set<std::string>& ifaceList) { |
| 1865 | if (!success) |
| 1866 | { |
| 1867 | messages::internalError(asyncResp->res); |
| 1868 | return; |
| 1869 | } |
| 1870 | |
| 1871 | nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; |
| 1872 | ifaceArray = nlohmann::json::array(); |
| 1873 | std::string tag = "_"; |
| 1874 | for (const std::string& ifaceItem : ifaceList) |
| 1875 | { |
| 1876 | std::size_t found = ifaceItem.find(tag); |
| 1877 | if (found == std::string::npos) |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1878 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1879 | nlohmann::json::object_t iface; |
| 1880 | iface["@odata.id"] = |
| 1881 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 1882 | ifaceItem; |
| 1883 | ifaceArray.push_back(std::move(iface)); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1884 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1885 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1886 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1887 | asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); |
| 1888 | asyncResp->res.jsonValue["@odata.id"] = |
| 1889 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
| 1890 | }); |
| 1891 | }); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1892 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1893 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1894 | .privileges(redfish::privileges::getEthernetInterface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1895 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1896 | [&app](const crow::Request& req, |
| 1897 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1898 | const std::string& ifaceId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1899 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1900 | { |
| 1901 | return; |
| 1902 | } |
| 1903 | getEthernetIfaceData( |
| 1904 | ifaceId, |
| 1905 | [asyncResp, ifaceId]( |
| 1906 | const bool& success, const EthernetInterfaceData& ethData, |
| 1907 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
| 1908 | const boost::container::flat_set<IPv6AddressData>& ipv6Data) { |
| 1909 | if (!success) |
| 1910 | { |
| 1911 | // TODO(Pawel)consider distinguish between non |
| 1912 | // existing object, and other errors |
| 1913 | messages::resourceNotFound(asyncResp->res, "EthernetInterface", |
| 1914 | ifaceId); |
| 1915 | return; |
| 1916 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1917 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1918 | asyncResp->res.jsonValue["@odata.type"] = |
| 1919 | "#EthernetInterface.v1_4_1.EthernetInterface"; |
| 1920 | asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; |
| 1921 | asyncResp->res.jsonValue["Description"] = |
| 1922 | "Management Network Interface"; |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1923 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1924 | parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1925 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1926 | }); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1927 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1928 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1929 | .privileges(redfish::privileges::patchEthernetInterface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1930 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1931 | [&app](const crow::Request& req, |
| 1932 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1933 | const std::string& ifaceId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1934 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1935 | { |
| 1936 | return; |
| 1937 | } |
| 1938 | std::optional<std::string> hostname; |
| 1939 | std::optional<std::string> fqdn; |
| 1940 | std::optional<std::string> macAddress; |
| 1941 | std::optional<std::string> ipv6DefaultGateway; |
| 1942 | std::optional<nlohmann::json> ipv4StaticAddresses; |
| 1943 | std::optional<nlohmann::json> ipv6StaticAddresses; |
| 1944 | std::optional<std::vector<std::string>> staticNameServers; |
| 1945 | std::optional<nlohmann::json> dhcpv4; |
| 1946 | std::optional<nlohmann::json> dhcpv6; |
| 1947 | std::optional<bool> interfaceEnabled; |
| 1948 | std::optional<size_t> mtuSize; |
| 1949 | DHCPParameters v4dhcpParms; |
| 1950 | DHCPParameters v6dhcpParms; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1951 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1952 | if (!json_util::readJsonPatch( |
| 1953 | req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, |
| 1954 | "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", |
| 1955 | macAddress, "StaticNameServers", staticNameServers, |
| 1956 | "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", |
| 1957 | ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, |
| 1958 | "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled)) |
| 1959 | { |
| 1960 | return; |
| 1961 | } |
| 1962 | if (dhcpv4) |
| 1963 | { |
| 1964 | if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", |
| 1965 | v4dhcpParms.dhcpv4Enabled, "UseDNSServers", |
| 1966 | v4dhcpParms.useDnsServers, "UseNTPServers", |
| 1967 | v4dhcpParms.useNtpServers, "UseDomainName", |
| 1968 | v4dhcpParms.useDomainName)) |
| 1969 | { |
| 1970 | return; |
| 1971 | } |
| 1972 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1973 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1974 | if (dhcpv6) |
| 1975 | { |
| 1976 | if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode", |
| 1977 | v6dhcpParms.dhcpv6OperatingMode, |
| 1978 | "UseDNSServers", v6dhcpParms.useDnsServers, |
| 1979 | "UseNTPServers", v6dhcpParms.useNtpServers, |
| 1980 | "UseDomainName", |
| 1981 | v6dhcpParms.useDomainName)) |
| 1982 | { |
| 1983 | return; |
| 1984 | } |
| 1985 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1986 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1987 | // Get single eth interface data, and call the below callback |
| 1988 | // for JSON preparation |
| 1989 | getEthernetIfaceData( |
| 1990 | ifaceId, |
| 1991 | [asyncResp, ifaceId, hostname = std::move(hostname), |
| 1992 | fqdn = std::move(fqdn), macAddress = std::move(macAddress), |
| 1993 | ipv4StaticAddresses = std::move(ipv4StaticAddresses), |
| 1994 | ipv6DefaultGateway = std::move(ipv6DefaultGateway), |
| 1995 | ipv6StaticAddresses = std::move(ipv6StaticAddresses), |
| 1996 | staticNameServers = std::move(staticNameServers), |
Ed Tanous | bc20089 | 2022-06-30 17:49:12 -0700 | [diff] [blame] | 1997 | dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize, |
| 1998 | v4dhcpParms = std::move(v4dhcpParms), |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1999 | v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( |
| 2000 | const bool& success, const EthernetInterfaceData& ethData, |
| 2001 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
| 2002 | const boost::container::flat_set<IPv6AddressData>& ipv6Data) { |
| 2003 | if (!success) |
| 2004 | { |
| 2005 | // ... otherwise return error |
| 2006 | // TODO(Pawel)consider distinguish between non |
| 2007 | // existing object, and other errors |
| 2008 | messages::resourceNotFound(asyncResp->res, "Ethernet Interface", |
| 2009 | ifaceId); |
| 2010 | return; |
| 2011 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2012 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2013 | if (dhcpv4 || dhcpv6) |
| 2014 | { |
| 2015 | handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2016 | asyncResp); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2017 | } |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 2018 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2019 | if (hostname) |
| 2020 | { |
| 2021 | handleHostnamePatch(*hostname, asyncResp); |
| 2022 | } |
| 2023 | |
| 2024 | if (fqdn) |
| 2025 | { |
| 2026 | handleFqdnPatch(ifaceId, *fqdn, asyncResp); |
| 2027 | } |
| 2028 | |
| 2029 | if (macAddress) |
| 2030 | { |
| 2031 | handleMACAddressPatch(ifaceId, *macAddress, asyncResp); |
| 2032 | } |
| 2033 | |
| 2034 | if (ipv4StaticAddresses) |
| 2035 | { |
| 2036 | // TODO(ed) for some reason the capture of |
| 2037 | // ipv4Addresses above is returning a const value, |
| 2038 | // not a non-const value. This doesn't really work |
| 2039 | // for us, as we need to be able to efficiently move |
| 2040 | // out the intermedia nlohmann::json objects. This |
| 2041 | // makes a copy of the structure, and operates on |
| 2042 | // that, but could be done more efficiently |
| 2043 | nlohmann::json ipv4Static = *ipv4StaticAddresses; |
| 2044 | handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp); |
| 2045 | } |
| 2046 | |
| 2047 | if (staticNameServers) |
| 2048 | { |
| 2049 | handleStaticNameServersPatch(ifaceId, *staticNameServers, |
| 2050 | asyncResp); |
| 2051 | } |
| 2052 | |
| 2053 | if (ipv6DefaultGateway) |
| 2054 | { |
| 2055 | messages::propertyNotWritable(asyncResp->res, |
| 2056 | "IPv6DefaultGateway"); |
| 2057 | } |
| 2058 | |
| 2059 | if (ipv6StaticAddresses) |
| 2060 | { |
| 2061 | const nlohmann::json& ipv6Static = *ipv6StaticAddresses; |
| 2062 | handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data, |
| 2063 | asyncResp); |
| 2064 | } |
| 2065 | |
| 2066 | if (interfaceEnabled) |
| 2067 | { |
| 2068 | setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled", |
| 2069 | *interfaceEnabled, asyncResp); |
| 2070 | } |
| 2071 | |
| 2072 | if (mtuSize) |
| 2073 | { |
| 2074 | handleMTUSizePatch(ifaceId, *mtuSize, asyncResp); |
| 2075 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2076 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2077 | }); |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 2078 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2079 | BMCWEB_ROUTE( |
| 2080 | app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2081 | .privileges(redfish::privileges::getVLanNetworkInterface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2082 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 2083 | [&app](const crow::Request& req, |
| 2084 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2085 | const std::string& parentIfaceId, |
| 2086 | const std::string& ifaceId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 2087 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2088 | { |
| 2089 | return; |
| 2090 | } |
| 2091 | asyncResp->res.jsonValue["@odata.type"] = |
| 2092 | "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; |
| 2093 | asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2094 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2095 | if (!verifyNames(parentIfaceId, ifaceId)) |
| 2096 | { |
| 2097 | return; |
| 2098 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2099 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2100 | // Get single eth interface data, and call the below callback |
| 2101 | // for JSON preparation |
| 2102 | getEthernetIfaceData( |
| 2103 | ifaceId, |
| 2104 | [asyncResp, parentIfaceId, |
| 2105 | ifaceId](const bool& success, const EthernetInterfaceData& ethData, |
| 2106 | const boost::container::flat_set<IPv4AddressData>&, |
| 2107 | const boost::container::flat_set<IPv6AddressData>&) { |
| 2108 | if (success && ethData.vlanId) |
| 2109 | { |
| 2110 | parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, |
| 2111 | ifaceId, ethData); |
| 2112 | } |
| 2113 | else |
| 2114 | { |
| 2115 | // ... otherwise return error |
| 2116 | // TODO(Pawel)consider distinguish between non |
| 2117 | // existing object, and other errors |
| 2118 | messages::resourceNotFound(asyncResp->res, |
| 2119 | "VLAN Network Interface", ifaceId); |
| 2120 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2121 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2122 | }); |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 2123 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2124 | BMCWEB_ROUTE( |
| 2125 | app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") |
Abhishek Patel | 3d768a1 | 2021-07-31 16:44:51 -0500 | [diff] [blame] | 2126 | .privileges(redfish::privileges::patchVLanNetworkInterface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2127 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 2128 | [&app](const crow::Request& req, |
| 2129 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2130 | const std::string& parentIfaceId, |
| 2131 | const std::string& ifaceId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 2132 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2133 | { |
| 2134 | return; |
| 2135 | } |
| 2136 | if (!verifyNames(parentIfaceId, ifaceId)) |
| 2137 | { |
| 2138 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 2139 | ifaceId); |
| 2140 | return; |
| 2141 | } |
| 2142 | |
| 2143 | std::optional<bool> vlanEnable; |
| 2144 | std::optional<uint32_t> vlanId; |
| 2145 | |
| 2146 | if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable", |
| 2147 | vlanEnable, "VLANId", vlanId)) |
| 2148 | { |
| 2149 | return; |
| 2150 | } |
| 2151 | |
| 2152 | if (vlanId) |
| 2153 | { |
| 2154 | messages::propertyNotWritable(asyncResp->res, "VLANId"); |
| 2155 | return; |
| 2156 | } |
| 2157 | |
| 2158 | // Get single eth interface data, and call the below callback |
| 2159 | // for JSON preparation |
| 2160 | getEthernetIfaceData( |
| 2161 | ifaceId, |
| 2162 | [asyncResp, parentIfaceId, ifaceId, vlanEnable]( |
| 2163 | const bool& success, const EthernetInterfaceData& ethData, |
| 2164 | const boost::container::flat_set<IPv4AddressData>&, |
| 2165 | const boost::container::flat_set<IPv6AddressData>&) { |
| 2166 | if (success && ethData.vlanId) |
| 2167 | { |
| 2168 | auto callback = |
| 2169 | [asyncResp](const boost::system::error_code ec) { |
| 2170 | if (ec) |
| 2171 | { |
| 2172 | messages::internalError(asyncResp->res); |
| 2173 | return; |
| 2174 | } |
| 2175 | }; |
| 2176 | |
| 2177 | if (vlanEnable && !(*vlanEnable)) |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 2178 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2179 | BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the " |
| 2180 | "vlan interface"; |
| 2181 | crow::connections::systemBus->async_method_call( |
| 2182 | std::move(callback), "xyz.openbmc_project.Network", |
| 2183 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 2184 | "xyz.openbmc_project.Object.Delete", "Delete"); |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 2185 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2186 | } |
| 2187 | else |
| 2188 | { |
| 2189 | // TODO(Pawel)consider distinguish between non |
| 2190 | // existing object, and other errors |
| 2191 | messages::resourceNotFound(asyncResp->res, |
| 2192 | "VLAN Network Interface", ifaceId); |
| 2193 | return; |
| 2194 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2195 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2196 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2197 | |
| 2198 | BMCWEB_ROUTE( |
| 2199 | app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") |
Abhishek Patel | 3d768a1 | 2021-07-31 16:44:51 -0500 | [diff] [blame] | 2200 | .privileges(redfish::privileges::deleteVLanNetworkInterface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2201 | .methods(boost::beast::http::verb::delete_)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 2202 | [&app](const crow::Request& req, |
| 2203 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2204 | const std::string& parentIfaceId, |
| 2205 | const std::string& ifaceId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 2206 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2207 | { |
| 2208 | return; |
| 2209 | } |
| 2210 | if (!verifyNames(parentIfaceId, ifaceId)) |
| 2211 | { |
| 2212 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 2213 | ifaceId); |
| 2214 | return; |
| 2215 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2216 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2217 | // Get single eth interface data, and call the below callback |
| 2218 | // for JSON preparation |
| 2219 | getEthernetIfaceData( |
| 2220 | ifaceId, |
| 2221 | [asyncResp, parentIfaceId, |
| 2222 | ifaceId](const bool& success, const EthernetInterfaceData& ethData, |
| 2223 | const boost::container::flat_set<IPv4AddressData>&, |
| 2224 | const boost::container::flat_set<IPv6AddressData>&) { |
| 2225 | if (success && ethData.vlanId) |
| 2226 | { |
| 2227 | auto callback = |
| 2228 | [asyncResp](const boost::system::error_code ec) { |
| 2229 | if (ec) |
| 2230 | { |
| 2231 | messages::internalError(asyncResp->res); |
| 2232 | } |
| 2233 | }; |
| 2234 | crow::connections::systemBus->async_method_call( |
| 2235 | std::move(callback), "xyz.openbmc_project.Network", |
| 2236 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 2237 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 2238 | } |
| 2239 | else |
| 2240 | { |
| 2241 | // ... otherwise return error |
| 2242 | // TODO(Pawel)consider distinguish between non |
| 2243 | // existing object, and other errors |
| 2244 | messages::resourceNotFound(asyncResp->res, |
| 2245 | "VLAN Network Interface", ifaceId); |
| 2246 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2247 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2248 | }); |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2249 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2250 | BMCWEB_ROUTE(app, |
| 2251 | "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2252 | |
| 2253 | .privileges(redfish::privileges::getVLanNetworkInterfaceCollection) |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 2254 | .methods(boost::beast::http::verb::get)( |
| 2255 | [&app](const crow::Request& req, |
| 2256 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2257 | const std::string& rootInterfaceName) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 2258 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2259 | { |
| 2260 | return; |
| 2261 | } |
| 2262 | // Get eth interface list, and call the below callback for JSON |
| 2263 | // preparation |
| 2264 | getEthernetIfaceList( |
| 2265 | [asyncResp, rootInterfaceName]( |
| 2266 | const bool& success, |
| 2267 | const boost::container::flat_set<std::string>& ifaceList) { |
| 2268 | if (!success) |
| 2269 | { |
| 2270 | messages::internalError(asyncResp->res); |
| 2271 | return; |
| 2272 | } |
| 2273 | |
| 2274 | if (ifaceList.find(rootInterfaceName) == ifaceList.end()) |
| 2275 | { |
| 2276 | messages::resourceNotFound(asyncResp->res, |
| 2277 | "VLanNetworkInterfaceCollection", |
| 2278 | rootInterfaceName); |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | asyncResp->res.jsonValue["@odata.type"] = |
| 2283 | "#VLanNetworkInterfaceCollection." |
| 2284 | "VLanNetworkInterfaceCollection"; |
| 2285 | asyncResp->res.jsonValue["Name"] = |
| 2286 | "VLAN Network Interface Collection"; |
| 2287 | |
| 2288 | nlohmann::json ifaceArray = nlohmann::json::array(); |
| 2289 | |
| 2290 | for (const std::string& ifaceItem : ifaceList) |
| 2291 | { |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 2292 | if (ifaceItem.starts_with(rootInterfaceName + "_")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2293 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2294 | std::string path = |
| 2295 | "/redfish/v1/Managers/bmc/EthernetInterfaces/"; |
| 2296 | path += rootInterfaceName; |
| 2297 | path += "/VLANs/"; |
| 2298 | path += ifaceItem; |
| 2299 | nlohmann::json::object_t iface; |
| 2300 | iface["@odata.id"] = std::move(path); |
| 2301 | ifaceArray.push_back(std::move(iface)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2302 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2303 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2304 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2305 | asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); |
| 2306 | asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); |
| 2307 | asyncResp->res.jsonValue["@odata.id"] = |
| 2308 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 2309 | rootInterfaceName + "/VLANs"; |
| 2310 | }); |
| 2311 | }); |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2312 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2313 | BMCWEB_ROUTE(app, |
| 2314 | "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") |
Abhishek Patel | 3d768a1 | 2021-07-31 16:44:51 -0500 | [diff] [blame] | 2315 | .privileges(redfish::privileges::postVLanNetworkInterfaceCollection) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2316 | .methods(boost::beast::http::verb::post)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 2317 | [&app](const crow::Request& req, |
| 2318 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2319 | const std::string& rootInterfaceName) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 2320 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2321 | { |
| 2322 | return; |
| 2323 | } |
| 2324 | bool vlanEnable = false; |
| 2325 | uint32_t vlanId = 0; |
| 2326 | if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId, |
| 2327 | "VLANEnable", vlanEnable)) |
| 2328 | { |
| 2329 | return; |
| 2330 | } |
| 2331 | // Need both vlanId and vlanEnable to service this request |
| 2332 | if (vlanId == 0U) |
| 2333 | { |
| 2334 | messages::propertyMissing(asyncResp->res, "VLANId"); |
| 2335 | } |
| 2336 | if (!vlanEnable) |
| 2337 | { |
| 2338 | messages::propertyMissing(asyncResp->res, "VLANEnable"); |
| 2339 | } |
| 2340 | if (static_cast<bool>(vlanId) ^ vlanEnable) |
| 2341 | { |
| 2342 | return; |
| 2343 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2344 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2345 | auto callback = [asyncResp](const boost::system::error_code ec) { |
| 2346 | if (ec) |
| 2347 | { |
| 2348 | // TODO(ed) make more consistent error messages |
| 2349 | // based on phosphor-network responses |
| 2350 | messages::internalError(asyncResp->res); |
| 2351 | return; |
| 2352 | } |
| 2353 | messages::created(asyncResp->res); |
| 2354 | }; |
| 2355 | crow::connections::systemBus->async_method_call( |
| 2356 | std::move(callback), "xyz.openbmc_project.Network", |
| 2357 | "/xyz/openbmc_project/network", |
| 2358 | "xyz.openbmc_project.Network.VLAN.Create", "VLAN", |
| 2359 | rootInterfaceName, vlanId); |
| 2360 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2363 | } // namespace redfish |