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 18 #include <boost/container/flat_map.hpp> 19 #include <boost/container/flat_set.hpp> 20 #include <dbus_singleton.hpp> 21 #include <error_messages.hpp> 22 #include <node.hpp> 23 #include <utils/json_utils.hpp> 24 25 #include <optional> 26 #include <regex> 27 #include <variant> 28 29 namespace redfish 30 { 31 32 /** 33 * DBus types primitives for several generic DBus interfaces 34 * TODO(Pawel) consider move this to separate file into boost::dbus 35 */ 36 using PropertiesMapType = boost::container::flat_map< 37 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t, 38 int32_t, uint32_t, int64_t, uint64_t, double>>; 39 40 using GetManagedObjects = std::vector<std::pair< 41 sdbusplus::message::object_path, 42 std::vector<std::pair< 43 std::string, 44 boost::container::flat_map< 45 std::string, 46 std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t, 47 uint32_t, int64_t, uint64_t, double, 48 std::vector<std::string>>>>>>>; 49 50 enum class LinkType 51 { 52 Local, 53 Global 54 }; 55 56 /** 57 * Structure for keeping IPv4 data required by Redfish 58 */ 59 struct IPv4AddressData 60 { 61 std::string id; 62 std::string address; 63 std::string domain; 64 std::string gateway; 65 std::string netmask; 66 std::string origin; 67 LinkType linktype; 68 bool isActive; 69 70 bool operator<(const IPv4AddressData& obj) const 71 { 72 return id < obj.id; 73 } 74 }; 75 76 /** 77 * Structure for keeping IPv6 data required by Redfish 78 */ 79 struct IPv6AddressData 80 { 81 std::string id; 82 std::string address; 83 std::string origin; 84 uint8_t prefixLength; 85 86 bool operator<(const IPv6AddressData& obj) const 87 { 88 return id < obj.id; 89 } 90 }; 91 /** 92 * Structure for keeping basic single Ethernet Interface information 93 * available from DBus 94 */ 95 struct EthernetInterfaceData 96 { 97 uint32_t speed; 98 bool auto_neg; 99 bool DNSEnabled; 100 bool NTPEnabled; 101 bool HostNameEnabled; 102 bool SendHostNameEnabled; 103 bool linkUp; 104 bool nicEnabled; 105 std::string DHCPEnabled; 106 std::string operatingMode; 107 std::string hostname; 108 std::string default_gateway; 109 std::string ipv6_default_gateway; 110 std::string mac_address; 111 std::vector<std::uint32_t> vlan_id; 112 std::vector<std::string> nameServers; 113 std::vector<std::string> staticNameServers; 114 std::vector<std::string> domainnames; 115 }; 116 117 struct DHCPParameters 118 { 119 std::optional<bool> dhcpv4Enabled; 120 std::optional<bool> useDNSServers; 121 std::optional<bool> useNTPServers; 122 std::optional<bool> useUseDomainName; 123 std::optional<std::string> dhcpv6OperatingMode; 124 }; 125 126 // Helper function that changes bits netmask notation (i.e. /24) 127 // into full dot notation 128 inline std::string getNetmask(unsigned int bits) 129 { 130 uint32_t value = 0xffffffff << (32 - bits); 131 std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 132 std::to_string((value >> 16) & 0xff) + "." + 133 std::to_string((value >> 8) & 0xff) + "." + 134 std::to_string(value & 0xff); 135 return netmask; 136 } 137 138 inline bool translateDHCPEnabledToBool(const std::string& inputDHCP, 139 bool isIPv4) 140 { 141 if (isIPv4) 142 { 143 return ( 144 (inputDHCP == 145 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || 146 (inputDHCP == 147 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 148 } 149 return ((inputDHCP == 150 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || 151 (inputDHCP == 152 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 153 } 154 155 inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) 156 { 157 if (isIPv4 && isIPv6) 158 { 159 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; 160 } 161 if (isIPv4) 162 { 163 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; 164 } 165 if (isIPv6) 166 { 167 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; 168 } 169 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; 170 } 171 172 inline std::string 173 translateAddressOriginDbusToRedfish(const std::string& inputOrigin, 174 bool isIPv4) 175 { 176 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 177 { 178 return "Static"; 179 } 180 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 181 { 182 if (isIPv4) 183 { 184 return "IPv4LinkLocal"; 185 } 186 return "LinkLocal"; 187 } 188 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 189 { 190 if (isIPv4) 191 { 192 return "DHCP"; 193 } 194 return "DHCPv6"; 195 } 196 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 197 { 198 return "SLAAC"; 199 } 200 return ""; 201 } 202 203 inline bool extractEthernetInterfaceData(const std::string& ethifaceId, 204 GetManagedObjects& dbusData, 205 EthernetInterfaceData& ethData) 206 { 207 bool idFound = false; 208 for (auto& objpath : dbusData) 209 { 210 for (auto& ifacePair : objpath.second) 211 { 212 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) 213 { 214 idFound = true; 215 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 216 { 217 for (const auto& propertyPair : ifacePair.second) 218 { 219 if (propertyPair.first == "MACAddress") 220 { 221 const std::string* mac = 222 std::get_if<std::string>(&propertyPair.second); 223 if (mac != nullptr) 224 { 225 ethData.mac_address = *mac; 226 } 227 } 228 } 229 } 230 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 231 { 232 for (const auto& propertyPair : ifacePair.second) 233 { 234 if (propertyPair.first == "Id") 235 { 236 const uint32_t* id = 237 std::get_if<uint32_t>(&propertyPair.second); 238 if (id != nullptr) 239 { 240 ethData.vlan_id.push_back(*id); 241 } 242 } 243 } 244 } 245 else if (ifacePair.first == 246 "xyz.openbmc_project.Network.EthernetInterface") 247 { 248 for (const auto& propertyPair : ifacePair.second) 249 { 250 if (propertyPair.first == "AutoNeg") 251 { 252 const bool* autoNeg = 253 std::get_if<bool>(&propertyPair.second); 254 if (autoNeg != nullptr) 255 { 256 ethData.auto_neg = *autoNeg; 257 } 258 } 259 else if (propertyPair.first == "Speed") 260 { 261 const uint32_t* speed = 262 std::get_if<uint32_t>(&propertyPair.second); 263 if (speed != nullptr) 264 { 265 ethData.speed = *speed; 266 } 267 } 268 else if (propertyPair.first == "LinkUp") 269 { 270 const bool* linkUp = 271 std::get_if<bool>(&propertyPair.second); 272 if (linkUp != nullptr) 273 { 274 ethData.linkUp = *linkUp; 275 } 276 } 277 else if (propertyPair.first == "NICEnabled") 278 { 279 const bool* nicEnabled = 280 std::get_if<bool>(&propertyPair.second); 281 if (nicEnabled != nullptr) 282 { 283 ethData.nicEnabled = *nicEnabled; 284 } 285 } 286 else if (propertyPair.first == "Nameservers") 287 { 288 const std::vector<std::string>* nameservers = 289 std::get_if<std::vector<std::string>>( 290 &propertyPair.second); 291 if (nameservers != nullptr) 292 { 293 ethData.nameServers = *nameservers; 294 } 295 } 296 else if (propertyPair.first == "StaticNameServers") 297 { 298 const std::vector<std::string>* staticNameServers = 299 std::get_if<std::vector<std::string>>( 300 &propertyPair.second); 301 if (staticNameServers != nullptr) 302 { 303 ethData.staticNameServers = *staticNameServers; 304 } 305 } 306 else if (propertyPair.first == "DHCPEnabled") 307 { 308 const std::string* dhcpEnabled = 309 std::get_if<std::string>(&propertyPair.second); 310 if (dhcpEnabled != nullptr) 311 { 312 ethData.DHCPEnabled = *dhcpEnabled; 313 } 314 } 315 else if (propertyPair.first == "DomainName") 316 { 317 const std::vector<std::string>* domainNames = 318 std::get_if<std::vector<std::string>>( 319 &propertyPair.second); 320 if (domainNames != nullptr) 321 { 322 ethData.domainnames = *domainNames; 323 } 324 } 325 else if (propertyPair.first == "DefaultGateway") 326 { 327 const std::string* defaultGateway = 328 std::get_if<std::string>(&propertyPair.second); 329 if (defaultGateway != nullptr) 330 { 331 std::string defaultGatewayStr = *defaultGateway; 332 if (defaultGatewayStr.empty()) 333 { 334 ethData.default_gateway = "0.0.0.0"; 335 } 336 else 337 { 338 ethData.default_gateway = defaultGatewayStr; 339 } 340 } 341 } 342 else if (propertyPair.first == "DefaultGateway6") 343 { 344 const std::string* defaultGateway6 = 345 std::get_if<std::string>(&propertyPair.second); 346 if (defaultGateway6 != nullptr) 347 { 348 std::string defaultGateway6Str = 349 *defaultGateway6; 350 if (defaultGateway6Str.empty()) 351 { 352 ethData.ipv6_default_gateway = 353 "0:0:0:0:0:0:0:0"; 354 } 355 else 356 { 357 ethData.ipv6_default_gateway = 358 defaultGateway6Str; 359 } 360 } 361 } 362 } 363 } 364 } 365 366 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") 367 { 368 if (ifacePair.first == 369 "xyz.openbmc_project.Network.DHCPConfiguration") 370 { 371 for (const auto& propertyPair : ifacePair.second) 372 { 373 if (propertyPair.first == "DNSEnabled") 374 { 375 const bool* dnsEnabled = 376 std::get_if<bool>(&propertyPair.second); 377 if (dnsEnabled != nullptr) 378 { 379 ethData.DNSEnabled = *dnsEnabled; 380 } 381 } 382 else if (propertyPair.first == "NTPEnabled") 383 { 384 const bool* ntpEnabled = 385 std::get_if<bool>(&propertyPair.second); 386 if (ntpEnabled != nullptr) 387 { 388 ethData.NTPEnabled = *ntpEnabled; 389 } 390 } 391 else if (propertyPair.first == "HostNameEnabled") 392 { 393 const bool* hostNameEnabled = 394 std::get_if<bool>(&propertyPair.second); 395 if (hostNameEnabled != nullptr) 396 { 397 ethData.HostNameEnabled = *hostNameEnabled; 398 } 399 } 400 else if (propertyPair.first == "SendHostNameEnabled") 401 { 402 const bool* sendHostNameEnabled = 403 std::get_if<bool>(&propertyPair.second); 404 if (sendHostNameEnabled != nullptr) 405 { 406 ethData.SendHostNameEnabled = 407 *sendHostNameEnabled; 408 } 409 } 410 } 411 } 412 } 413 // System configuration shows up in the global namespace, so no need 414 // to check eth number 415 if (ifacePair.first == 416 "xyz.openbmc_project.Network.SystemConfiguration") 417 { 418 for (const auto& propertyPair : ifacePair.second) 419 { 420 if (propertyPair.first == "HostName") 421 { 422 const std::string* hostname = 423 std::get_if<std::string>(&propertyPair.second); 424 if (hostname != nullptr) 425 { 426 ethData.hostname = *hostname; 427 } 428 } 429 } 430 } 431 } 432 } 433 return idFound; 434 } 435 436 // Helper function that extracts data for single ethernet ipv6 address 437 inline void 438 extractIPV6Data(const std::string& ethifaceId, 439 const GetManagedObjects& dbusData, 440 boost::container::flat_set<IPv6AddressData>& ipv6Config) 441 { 442 const std::string ipv6PathStart = 443 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/"; 444 445 // Since there might be several IPv6 configurations aligned with 446 // single ethernet interface, loop over all of them 447 for (const auto& objpath : dbusData) 448 { 449 // Check if proper pattern for object path appears 450 if (boost::starts_with(objpath.first.str, ipv6PathStart)) 451 { 452 for (auto& interface : objpath.second) 453 { 454 if (interface.first == "xyz.openbmc_project.Network.IP") 455 { 456 // Instance IPv6AddressData structure, and set as 457 // appropriate 458 std::pair< 459 boost::container::flat_set<IPv6AddressData>::iterator, 460 bool> 461 it = ipv6Config.insert(IPv6AddressData{}); 462 IPv6AddressData& ipv6Address = *it.first; 463 ipv6Address.id = 464 objpath.first.str.substr(ipv6PathStart.size()); 465 for (auto& property : interface.second) 466 { 467 if (property.first == "Address") 468 { 469 const std::string* address = 470 std::get_if<std::string>(&property.second); 471 if (address != nullptr) 472 { 473 ipv6Address.address = *address; 474 } 475 } 476 else if (property.first == "Origin") 477 { 478 const std::string* origin = 479 std::get_if<std::string>(&property.second); 480 if (origin != nullptr) 481 { 482 ipv6Address.origin = 483 translateAddressOriginDbusToRedfish(*origin, 484 false); 485 } 486 } 487 else if (property.first == "PrefixLength") 488 { 489 const uint8_t* prefix = 490 std::get_if<uint8_t>(&property.second); 491 if (prefix != nullptr) 492 { 493 ipv6Address.prefixLength = *prefix; 494 } 495 } 496 else 497 { 498 BMCWEB_LOG_ERROR 499 << "Got extra property: " << property.first 500 << " on the " << objpath.first.str << " object"; 501 } 502 } 503 } 504 } 505 } 506 } 507 } 508 509 // Helper function that extracts data for single ethernet ipv4 address 510 inline void 511 extractIPData(const std::string& ethifaceId, 512 const GetManagedObjects& dbusData, 513 boost::container::flat_set<IPv4AddressData>& ipv4Config) 514 { 515 const std::string ipv4PathStart = 516 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/"; 517 518 // Since there might be several IPv4 configurations aligned with 519 // single ethernet interface, loop over all of them 520 for (const auto& objpath : dbusData) 521 { 522 // Check if proper pattern for object path appears 523 if (boost::starts_with(objpath.first.str, ipv4PathStart)) 524 { 525 for (auto& interface : objpath.second) 526 { 527 if (interface.first == "xyz.openbmc_project.Network.IP") 528 { 529 // Instance IPv4AddressData structure, and set as 530 // appropriate 531 std::pair< 532 boost::container::flat_set<IPv4AddressData>::iterator, 533 bool> 534 it = ipv4Config.insert(IPv4AddressData{}); 535 IPv4AddressData& ipv4Address = *it.first; 536 ipv4Address.id = 537 objpath.first.str.substr(ipv4PathStart.size()); 538 for (auto& property : interface.second) 539 { 540 if (property.first == "Address") 541 { 542 const std::string* address = 543 std::get_if<std::string>(&property.second); 544 if (address != nullptr) 545 { 546 ipv4Address.address = *address; 547 } 548 } 549 else if (property.first == "Origin") 550 { 551 const std::string* origin = 552 std::get_if<std::string>(&property.second); 553 if (origin != nullptr) 554 { 555 ipv4Address.origin = 556 translateAddressOriginDbusToRedfish(*origin, 557 true); 558 } 559 } 560 else if (property.first == "PrefixLength") 561 { 562 const uint8_t* mask = 563 std::get_if<uint8_t>(&property.second); 564 if (mask != nullptr) 565 { 566 // convert it to the string 567 ipv4Address.netmask = getNetmask(*mask); 568 } 569 } 570 else 571 { 572 BMCWEB_LOG_ERROR 573 << "Got extra property: " << property.first 574 << " on the " << objpath.first.str << " object"; 575 } 576 } 577 // Check if given address is local, or global 578 ipv4Address.linktype = 579 boost::starts_with(ipv4Address.address, "169.254.") 580 ? LinkType::Local 581 : LinkType::Global; 582 } 583 } 584 } 585 } 586 } 587 588 /** 589 * @brief Sets given Id on the given VLAN interface through D-Bus 590 * 591 * @param[in] ifaceId Id of VLAN interface that should be modified 592 * @param[in] inputVlanId New ID of the VLAN 593 * @param[in] callback Function that will be called after the operation 594 * 595 * @return None. 596 */ 597 template <typename CallbackFunc> 598 void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId, 599 CallbackFunc&& callback) 600 { 601 crow::connections::systemBus->async_method_call( 602 callback, "xyz.openbmc_project.Network", 603 std::string("/xyz/openbmc_project/network/") + ifaceId, 604 "org.freedesktop.DBus.Properties", "Set", 605 "xyz.openbmc_project.Network.VLAN", "Id", 606 std::variant<uint32_t>(inputVlanId)); 607 } 608 609 /** 610 * @brief Helper function that verifies IP address to check if it is in 611 * proper format. If bits pointer is provided, also calculates active 612 * bit count for Subnet Mask. 613 * 614 * @param[in] ip IP that will be verified 615 * @param[out] bits Calculated mask in bits notation 616 * 617 * @return true in case of success, false otherwise 618 */ 619 inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip, 620 uint8_t* bits = nullptr) 621 { 622 std::vector<std::string> bytesInMask; 623 624 boost::split(bytesInMask, ip, boost::is_any_of(".")); 625 626 static const constexpr int ipV4AddressSectionsCount = 4; 627 if (bytesInMask.size() != ipV4AddressSectionsCount) 628 { 629 return false; 630 } 631 632 if (bits != nullptr) 633 { 634 *bits = 0; 635 } 636 637 char* endPtr; 638 long previousValue = 255; 639 bool firstZeroInByteHit; 640 for (const std::string& byte : bytesInMask) 641 { 642 if (byte.empty()) 643 { 644 return false; 645 } 646 647 // Use strtol instead of stroi to avoid exceptions 648 long value = std::strtol(byte.c_str(), &endPtr, 10); 649 650 // endPtr should point to the end of the string, otherwise given string 651 // is not 100% number 652 if (*endPtr != '\0') 653 { 654 return false; 655 } 656 657 // Value should be contained in byte 658 if (value < 0 || value > 255) 659 { 660 return false; 661 } 662 663 if (bits != nullptr) 664 { 665 // Mask has to be continuous between bytes 666 if (previousValue != 255 && value != 0) 667 { 668 return false; 669 } 670 671 // Mask has to be continuous inside bytes 672 firstZeroInByteHit = false; 673 674 // Count bits 675 for (long bitIdx = 7; bitIdx >= 0; bitIdx--) 676 { 677 if (value & (1L << bitIdx)) 678 { 679 if (firstZeroInByteHit) 680 { 681 // Continuity not preserved 682 return false; 683 } 684 (*bits)++; 685 } 686 else 687 { 688 firstZeroInByteHit = true; 689 } 690 } 691 } 692 693 previousValue = value; 694 } 695 696 return true; 697 } 698 699 /** 700 * @brief Deletes given IPv4 interface 701 * 702 * @param[in] ifaceId Id of interface whose IP should be deleted 703 * @param[in] ipHash DBus Hash id of IP that should be deleted 704 * @param[io] asyncResp Response object that will be returned to client 705 * 706 * @return None 707 */ 708 inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 709 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 710 { 711 crow::connections::systemBus->async_method_call( 712 [asyncResp](const boost::system::error_code ec) { 713 if (ec) 714 { 715 messages::internalError(asyncResp->res); 716 } 717 }, 718 "xyz.openbmc_project.Network", 719 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 720 "xyz.openbmc_project.Object.Delete", "Delete"); 721 } 722 723 inline void updateIPv4DefaultGateway( 724 const std::string& ifaceId, const std::string& gateway, 725 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 726 { 727 crow::connections::systemBus->async_method_call( 728 [asyncResp](const boost::system::error_code ec) { 729 if (ec) 730 { 731 messages::internalError(asyncResp->res); 732 return; 733 } 734 asyncResp->res.result(boost::beast::http::status::no_content); 735 }, 736 "xyz.openbmc_project.Network", 737 "/xyz/openbmc_project/network/" + ifaceId, 738 "org.freedesktop.DBus.Properties", "Set", 739 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", 740 std::variant<std::string>(gateway)); 741 } 742 /** 743 * @brief Creates a static IPv4 entry 744 * 745 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 746 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 747 * @param[in] gateway IPv4 address of this interfaces gateway 748 * @param[in] address IPv4 address to assign to this interface 749 * @param[io] asyncResp Response object that will be returned to client 750 * 751 * @return None 752 */ 753 inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 754 const std::string& gateway, const std::string& address, 755 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 756 { 757 auto createIpHandler = [asyncResp, ifaceId, 758 gateway](const boost::system::error_code ec) { 759 if (ec) 760 { 761 messages::internalError(asyncResp->res); 762 return; 763 } 764 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 765 }; 766 767 crow::connections::systemBus->async_method_call( 768 std::move(createIpHandler), "xyz.openbmc_project.Network", 769 "/xyz/openbmc_project/network/" + ifaceId, 770 "xyz.openbmc_project.Network.IP.Create", "IP", 771 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 772 gateway); 773 } 774 775 /** 776 * @brief Deletes the IPv4 entry for this interface and creates a replacement 777 * static IPv4 entry 778 * 779 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 780 * @param[in] id The unique hash entry identifying the DBus entry 781 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 782 * @param[in] gateway IPv4 address of this interfaces gateway 783 * @param[in] address IPv4 address to assign to this interface 784 * @param[io] asyncResp Response object that will be returned to client 785 * 786 * @return None 787 */ 788 inline void 789 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id, 790 uint8_t prefixLength, const std::string& gateway, 791 const std::string& address, 792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 793 { 794 crow::connections::systemBus->async_method_call( 795 [asyncResp, ifaceId, address, prefixLength, 796 gateway](const boost::system::error_code ec) { 797 if (ec) 798 { 799 messages::internalError(asyncResp->res); 800 return; 801 } 802 803 crow::connections::systemBus->async_method_call( 804 [asyncResp, ifaceId, 805 gateway](const boost::system::error_code ec2) { 806 if (ec2) 807 { 808 messages::internalError(asyncResp->res); 809 return; 810 } 811 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 812 }, 813 "xyz.openbmc_project.Network", 814 "/xyz/openbmc_project/network/" + ifaceId, 815 "xyz.openbmc_project.Network.IP.Create", "IP", 816 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 817 prefixLength, gateway); 818 }, 819 "xyz.openbmc_project.Network", 820 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 821 "xyz.openbmc_project.Object.Delete", "Delete"); 822 } 823 824 /** 825 * @brief Deletes given IPv6 826 * 827 * @param[in] ifaceId Id of interface whose IP should be deleted 828 * @param[in] ipHash DBus Hash id of IP that should be deleted 829 * @param[io] asyncResp Response object that will be returned to client 830 * 831 * @return None 832 */ 833 inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 834 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 835 { 836 crow::connections::systemBus->async_method_call( 837 [asyncResp](const boost::system::error_code ec) { 838 if (ec) 839 { 840 messages::internalError(asyncResp->res); 841 } 842 }, 843 "xyz.openbmc_project.Network", 844 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 845 "xyz.openbmc_project.Object.Delete", "Delete"); 846 } 847 848 /** 849 * @brief Deletes the IPv6 entry for this interface and creates a replacement 850 * static IPv6 entry 851 * 852 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 853 * @param[in] id The unique hash entry identifying the DBus entry 854 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 855 * @param[in] address IPv6 address to assign to this interface 856 * @param[io] asyncResp Response object that will be returned to client 857 * 858 * @return None 859 */ 860 inline void 861 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id, 862 uint8_t prefixLength, const std::string& address, 863 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 864 { 865 crow::connections::systemBus->async_method_call( 866 [asyncResp, ifaceId, address, 867 prefixLength](const boost::system::error_code ec) { 868 if (ec) 869 { 870 messages::internalError(asyncResp->res); 871 } 872 crow::connections::systemBus->async_method_call( 873 [asyncResp](const boost::system::error_code ec2) { 874 if (ec2) 875 { 876 messages::internalError(asyncResp->res); 877 } 878 }, 879 "xyz.openbmc_project.Network", 880 "/xyz/openbmc_project/network/" + ifaceId, 881 "xyz.openbmc_project.Network.IP.Create", "IP", 882 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 883 prefixLength, ""); 884 }, 885 "xyz.openbmc_project.Network", 886 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 887 "xyz.openbmc_project.Object.Delete", "Delete"); 888 } 889 890 /** 891 * @brief Creates IPv6 with given data 892 * 893 * @param[in] ifaceId Id of interface whose IP should be added 894 * @param[in] prefixLength Prefix length that needs to be added 895 * @param[in] address IP address that needs to be added 896 * @param[io] asyncResp Response object that will be returned to client 897 * 898 * @return None 899 */ 900 inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 901 const std::string& address, 902 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 903 { 904 auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 905 if (ec) 906 { 907 messages::internalError(asyncResp->res); 908 } 909 }; 910 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 911 // does not have associated gateway property 912 crow::connections::systemBus->async_method_call( 913 std::move(createIpHandler), "xyz.openbmc_project.Network", 914 "/xyz/openbmc_project/network/" + ifaceId, 915 "xyz.openbmc_project.Network.IP.Create", "IP", 916 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 917 ""); 918 } 919 920 /** 921 * Function that retrieves all properties for given Ethernet Interface 922 * Object 923 * from EntityManager Network Manager 924 * @param ethiface_id a eth interface id to query on DBus 925 * @param callback a function that shall be called to convert Dbus output 926 * into JSON 927 */ 928 template <typename CallbackFunc> 929 void getEthernetIfaceData(const std::string& ethifaceId, 930 CallbackFunc&& callback) 931 { 932 crow::connections::systemBus->async_method_call( 933 [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}]( 934 const boost::system::error_code errorCode, 935 GetManagedObjects& resp) { 936 EthernetInterfaceData ethData{}; 937 boost::container::flat_set<IPv4AddressData> ipv4Data; 938 boost::container::flat_set<IPv6AddressData> ipv6Data; 939 940 if (errorCode) 941 { 942 callback(false, ethData, ipv4Data, ipv6Data); 943 return; 944 } 945 946 bool found = 947 extractEthernetInterfaceData(ethifaceId, resp, ethData); 948 if (!found) 949 { 950 callback(false, ethData, ipv4Data, ipv6Data); 951 return; 952 } 953 954 extractIPData(ethifaceId, resp, ipv4Data); 955 // Fix global GW 956 for (IPv4AddressData& ipv4 : ipv4Data) 957 { 958 if (((ipv4.linktype == LinkType::Global) && 959 (ipv4.gateway == "0.0.0.0")) || 960 (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) 961 { 962 ipv4.gateway = ethData.default_gateway; 963 } 964 } 965 966 extractIPV6Data(ethifaceId, resp, ipv6Data); 967 // Finally make a callback with useful data 968 callback(true, ethData, ipv4Data, ipv6Data); 969 }, 970 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 971 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 972 } 973 974 /** 975 * Function that retrieves all Ethernet Interfaces available through Network 976 * Manager 977 * @param callback a function that shall be called to convert Dbus output 978 * into JSON. 979 */ 980 template <typename CallbackFunc> 981 void getEthernetIfaceList(CallbackFunc&& callback) 982 { 983 crow::connections::systemBus->async_method_call( 984 [callback{std::move(callback)}]( 985 const boost::system::error_code errorCode, 986 GetManagedObjects& resp) { 987 // Callback requires vector<string> to retrieve all available 988 // ethernet interfaces 989 boost::container::flat_set<std::string> ifaceList; 990 ifaceList.reserve(resp.size()); 991 if (errorCode) 992 { 993 callback(false, ifaceList); 994 return; 995 } 996 997 // Iterate over all retrieved ObjectPaths. 998 for (const auto& objpath : resp) 999 { 1000 // And all interfaces available for certain ObjectPath. 1001 for (const auto& interface : objpath.second) 1002 { 1003 // If interface is 1004 // xyz.openbmc_project.Network.EthernetInterface, this is 1005 // what we're looking for. 1006 if (interface.first == 1007 "xyz.openbmc_project.Network.EthernetInterface") 1008 { 1009 std::string ifaceId = objpath.first.filename(); 1010 if (ifaceId.empty()) 1011 { 1012 continue; 1013 } 1014 // and put it into output vector. 1015 ifaceList.emplace(ifaceId); 1016 } 1017 } 1018 } 1019 // Finally make a callback with useful data 1020 callback(true, ifaceList); 1021 }, 1022 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 1023 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 1024 } 1025 1026 /** 1027 * EthernetCollection derived class for delivering Ethernet Collection Schema 1028 */ 1029 class EthernetCollection : public Node 1030 { 1031 public: 1032 EthernetCollection(App& app) : 1033 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1034 { 1035 entityPrivileges = { 1036 {boost::beast::http::verb::get, {{"Login"}}}, 1037 {boost::beast::http::verb::head, {{"Login"}}}, 1038 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1039 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1040 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1041 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1042 } 1043 1044 private: 1045 /** 1046 * Functions triggers appropriate requests on DBus 1047 */ 1048 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1049 const crow::Request&, const std::vector<std::string>&) override 1050 { 1051 asyncResp->res.jsonValue["@odata.type"] = 1052 "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1053 asyncResp->res.jsonValue["@odata.id"] = 1054 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1055 asyncResp->res.jsonValue["Name"] = 1056 "Ethernet Network Interface Collection"; 1057 asyncResp->res.jsonValue["Description"] = 1058 "Collection of EthernetInterfaces for this Manager"; 1059 1060 // Get eth interface list, and call the below callback for JSON 1061 // preparation 1062 getEthernetIfaceList( 1063 [asyncResp]( 1064 const bool& success, 1065 const boost::container::flat_set<std::string>& ifaceList) { 1066 if (!success) 1067 { 1068 messages::internalError(asyncResp->res); 1069 return; 1070 } 1071 1072 nlohmann::json& ifaceArray = 1073 asyncResp->res.jsonValue["Members"]; 1074 ifaceArray = nlohmann::json::array(); 1075 std::string tag = "_"; 1076 for (const std::string& ifaceItem : ifaceList) 1077 { 1078 std::size_t found = ifaceItem.find(tag); 1079 if (found == std::string::npos) 1080 { 1081 ifaceArray.push_back( 1082 {{"@odata.id", 1083 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1084 ifaceItem}}); 1085 } 1086 } 1087 1088 asyncResp->res.jsonValue["Members@odata.count"] = 1089 ifaceArray.size(); 1090 asyncResp->res.jsonValue["@odata.id"] = 1091 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1092 }); 1093 } 1094 }; 1095 1096 /** 1097 * EthernetInterface derived class for delivering Ethernet Schema 1098 */ 1099 class EthernetInterface : public Node 1100 { 1101 public: 1102 /* 1103 * Default Constructor 1104 */ 1105 EthernetInterface(App& app) : 1106 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", 1107 std::string()) 1108 { 1109 entityPrivileges = { 1110 {boost::beast::http::verb::get, {{"Login"}}}, 1111 {boost::beast::http::verb::head, {{"Login"}}}, 1112 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1113 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1114 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1115 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1116 } 1117 1118 private: 1119 void 1120 handleHostnamePatch(const std::string& hostname, 1121 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1122 { 1123 // SHOULD handle host names of up to 255 characters(RFC 1123) 1124 if (hostname.length() > 255) 1125 { 1126 messages::propertyValueFormatError(asyncResp->res, hostname, 1127 "HostName"); 1128 return; 1129 } 1130 crow::connections::systemBus->async_method_call( 1131 [asyncResp](const boost::system::error_code ec) { 1132 if (ec) 1133 { 1134 messages::internalError(asyncResp->res); 1135 } 1136 }, 1137 "xyz.openbmc_project.Network", 1138 "/xyz/openbmc_project/network/config", 1139 "org.freedesktop.DBus.Properties", "Set", 1140 "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 1141 std::variant<std::string>(hostname)); 1142 } 1143 1144 void handleDomainnamePatch( 1145 const std::string& ifaceId, const std::string& domainname, 1146 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1147 { 1148 std::vector<std::string> vectorDomainname = {domainname}; 1149 crow::connections::systemBus->async_method_call( 1150 [asyncResp](const boost::system::error_code ec) { 1151 if (ec) 1152 { 1153 messages::internalError(asyncResp->res); 1154 } 1155 }, 1156 "xyz.openbmc_project.Network", 1157 "/xyz/openbmc_project/network/" + ifaceId, 1158 "org.freedesktop.DBus.Properties", "Set", 1159 "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 1160 std::variant<std::vector<std::string>>(vectorDomainname)); 1161 } 1162 1163 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 1164 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1165 { 1166 // Total length of FQDN must not exceed 255 characters(RFC 1035) 1167 if (fqdn.length() > 255) 1168 { 1169 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1170 return; 1171 } 1172 1173 size_t pos = fqdn.find('.'); 1174 if (pos == std::string::npos) 1175 { 1176 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1177 return; 1178 } 1179 1180 std::string hostname; 1181 std::string domainname; 1182 domainname = (fqdn).substr(pos + 1); 1183 hostname = (fqdn).substr(0, pos); 1184 1185 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1186 { 1187 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1188 return; 1189 } 1190 1191 handleHostnamePatch(hostname, asyncResp); 1192 handleDomainnamePatch(ifaceId, domainname, asyncResp); 1193 } 1194 1195 bool isHostnameValid(const std::string& hostname) 1196 { 1197 // A valid host name can never have the dotted-decimal form (RFC 1123) 1198 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 1199 { 1200 return false; 1201 } 1202 // Each label(hostname/subdomains) within a valid FQDN 1203 // MUST handle host names of up to 63 characters (RFC 1123) 1204 // labels cannot start or end with hyphens (RFC 952) 1205 // labels can start with numbers (RFC 1123) 1206 const std::regex pattern( 1207 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 1208 1209 return std::regex_match(hostname, pattern); 1210 } 1211 1212 bool isDomainnameValid(const std::string& domainname) 1213 { 1214 // Can have multiple subdomains 1215 // Top Level Domain's min length is 2 character 1216 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]" 1217 "{1,30}\\.)*[a-zA-Z]{2,}$"); 1218 1219 return std::regex_match(domainname, pattern); 1220 } 1221 1222 void handleMACAddressPatch( 1223 const std::string& ifaceId, const std::string& macAddress, 1224 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1225 { 1226 crow::connections::systemBus->async_method_call( 1227 [asyncResp, macAddress](const boost::system::error_code ec) { 1228 if (ec) 1229 { 1230 messages::internalError(asyncResp->res); 1231 return; 1232 } 1233 }, 1234 "xyz.openbmc_project.Network", 1235 "/xyz/openbmc_project/network/" + ifaceId, 1236 "org.freedesktop.DBus.Properties", "Set", 1237 "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1238 std::variant<std::string>(macAddress)); 1239 } 1240 1241 void setDHCPEnabled(const std::string& ifaceId, 1242 const std::string& propertyName, const bool v4Value, 1243 const bool v6Value, 1244 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1245 { 1246 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1247 crow::connections::systemBus->async_method_call( 1248 [asyncResp](const boost::system::error_code ec) { 1249 if (ec) 1250 { 1251 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1252 messages::internalError(asyncResp->res); 1253 return; 1254 } 1255 messages::success(asyncResp->res); 1256 }, 1257 "xyz.openbmc_project.Network", 1258 "/xyz/openbmc_project/network/" + ifaceId, 1259 "org.freedesktop.DBus.Properties", "Set", 1260 "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1261 std::variant<std::string>{dhcp}); 1262 } 1263 1264 void setEthernetInterfaceBoolProperty( 1265 const std::string& ifaceId, const std::string& propertyName, 1266 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1267 { 1268 crow::connections::systemBus->async_method_call( 1269 [asyncResp](const boost::system::error_code ec) { 1270 if (ec) 1271 { 1272 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1273 messages::internalError(asyncResp->res); 1274 return; 1275 } 1276 }, 1277 "xyz.openbmc_project.Network", 1278 "/xyz/openbmc_project/network/" + ifaceId, 1279 "org.freedesktop.DBus.Properties", "Set", 1280 "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1281 std::variant<bool>{value}); 1282 } 1283 1284 void setDHCPv4Config(const std::string& propertyName, const bool& value, 1285 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1286 { 1287 BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1288 crow::connections::systemBus->async_method_call( 1289 [asyncResp](const boost::system::error_code ec) { 1290 if (ec) 1291 { 1292 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1293 messages::internalError(asyncResp->res); 1294 return; 1295 } 1296 }, 1297 "xyz.openbmc_project.Network", 1298 "/xyz/openbmc_project/network/config/dhcp", 1299 "org.freedesktop.DBus.Properties", "Set", 1300 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1301 std::variant<bool>{value}); 1302 } 1303 1304 void handleDHCPPatch(const std::string& ifaceId, 1305 const EthernetInterfaceData& ethData, 1306 const DHCPParameters& v4dhcpParms, 1307 const DHCPParameters& v6dhcpParms, 1308 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1309 { 1310 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 1311 bool ipv6Active = 1312 translateDHCPEnabledToBool(ethData.DHCPEnabled, false); 1313 1314 bool nextv4DHCPState = 1315 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 1316 1317 bool nextv6DHCPState{}; 1318 if (v6dhcpParms.dhcpv6OperatingMode) 1319 { 1320 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 1321 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 1322 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 1323 { 1324 messages::propertyValueFormatError( 1325 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode, 1326 "OperatingMode"); 1327 return; 1328 } 1329 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 1330 } 1331 else 1332 { 1333 nextv6DHCPState = ipv6Active; 1334 } 1335 1336 bool nextDNS{}; 1337 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers) 1338 { 1339 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers) 1340 { 1341 messages::generalError(asyncResp->res); 1342 return; 1343 } 1344 nextDNS = *v4dhcpParms.useDNSServers; 1345 } 1346 else if (v4dhcpParms.useDNSServers) 1347 { 1348 nextDNS = *v4dhcpParms.useDNSServers; 1349 } 1350 else if (v6dhcpParms.useDNSServers) 1351 { 1352 nextDNS = *v6dhcpParms.useDNSServers; 1353 } 1354 else 1355 { 1356 nextDNS = ethData.DNSEnabled; 1357 } 1358 1359 bool nextNTP{}; 1360 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers) 1361 { 1362 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers) 1363 { 1364 messages::generalError(asyncResp->res); 1365 return; 1366 } 1367 nextNTP = *v4dhcpParms.useNTPServers; 1368 } 1369 else if (v4dhcpParms.useNTPServers) 1370 { 1371 nextNTP = *v4dhcpParms.useNTPServers; 1372 } 1373 else if (v6dhcpParms.useNTPServers) 1374 { 1375 nextNTP = *v6dhcpParms.useNTPServers; 1376 } 1377 else 1378 { 1379 nextNTP = ethData.NTPEnabled; 1380 } 1381 1382 bool nextUseDomain{}; 1383 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName) 1384 { 1385 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName) 1386 { 1387 messages::generalError(asyncResp->res); 1388 return; 1389 } 1390 nextUseDomain = *v4dhcpParms.useUseDomainName; 1391 } 1392 else if (v4dhcpParms.useUseDomainName) 1393 { 1394 nextUseDomain = *v4dhcpParms.useUseDomainName; 1395 } 1396 else if (v6dhcpParms.useUseDomainName) 1397 { 1398 nextUseDomain = *v6dhcpParms.useUseDomainName; 1399 } 1400 else 1401 { 1402 nextUseDomain = ethData.HostNameEnabled; 1403 } 1404 1405 BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 1406 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 1407 asyncResp); 1408 BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 1409 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1410 BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 1411 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 1412 BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 1413 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1414 } 1415 1416 boost::container::flat_set<IPv4AddressData>::const_iterator 1417 getNextStaticIpEntry( 1418 const boost::container::flat_set<IPv4AddressData>::const_iterator& 1419 head, 1420 const boost::container::flat_set<IPv4AddressData>::const_iterator& 1421 end) 1422 { 1423 return std::find_if(head, end, [](const IPv4AddressData& value) { 1424 return value.origin == "Static"; 1425 }); 1426 } 1427 1428 boost::container::flat_set<IPv6AddressData>::const_iterator 1429 getNextStaticIpEntry( 1430 const boost::container::flat_set<IPv6AddressData>::const_iterator& 1431 head, 1432 const boost::container::flat_set<IPv6AddressData>::const_iterator& 1433 end) 1434 { 1435 return std::find_if(head, end, [](const IPv6AddressData& value) { 1436 return value.origin == "Static"; 1437 }); 1438 } 1439 1440 void handleIPv4StaticPatch( 1441 const std::string& ifaceId, nlohmann::json& input, 1442 const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1443 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1444 { 1445 if ((!input.is_array()) || input.empty()) 1446 { 1447 messages::propertyValueTypeError( 1448 asyncResp->res, 1449 input.dump(2, ' ', true, 1450 nlohmann::json::error_handler_t::replace), 1451 "IPv4StaticAddresses"); 1452 return; 1453 } 1454 1455 unsigned entryIdx = 1; 1456 // Find the first static IP address currently active on the NIC and 1457 // match it to the first JSON element in the IPv4StaticAddresses array. 1458 // Match each subsequent JSON element to the next static IP programmed 1459 // into the NIC. 1460 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry = 1461 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 1462 1463 for (nlohmann::json& thisJson : input) 1464 { 1465 std::string pathString = 1466 "IPv4StaticAddresses/" + std::to_string(entryIdx); 1467 1468 if (!thisJson.is_null() && !thisJson.empty()) 1469 { 1470 std::optional<std::string> address; 1471 std::optional<std::string> subnetMask; 1472 std::optional<std::string> gateway; 1473 1474 if (!json_util::readJson(thisJson, asyncResp->res, "Address", 1475 address, "SubnetMask", subnetMask, 1476 "Gateway", gateway)) 1477 { 1478 messages::propertyValueFormatError( 1479 asyncResp->res, 1480 thisJson.dump(2, ' ', true, 1481 nlohmann::json::error_handler_t::replace), 1482 pathString); 1483 return; 1484 } 1485 1486 // Find the address/subnet/gateway values. Any values that are 1487 // not explicitly provided are assumed to be unmodified from the 1488 // current state of the interface. Merge existing state into the 1489 // current request. 1490 const std::string* addr = nullptr; 1491 const std::string* gw = nullptr; 1492 uint8_t prefixLength = 0; 1493 bool errorInEntry = false; 1494 if (address) 1495 { 1496 if (ipv4VerifyIpAndGetBitcount(*address)) 1497 { 1498 addr = &(*address); 1499 } 1500 else 1501 { 1502 messages::propertyValueFormatError( 1503 asyncResp->res, *address, pathString + "/Address"); 1504 errorInEntry = true; 1505 } 1506 } 1507 else if (niciPentry != ipv4Data.cend()) 1508 { 1509 addr = &(niciPentry->address); 1510 } 1511 else 1512 { 1513 messages::propertyMissing(asyncResp->res, 1514 pathString + "/Address"); 1515 errorInEntry = true; 1516 } 1517 1518 if (subnetMask) 1519 { 1520 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 1521 { 1522 messages::propertyValueFormatError( 1523 asyncResp->res, *subnetMask, 1524 pathString + "/SubnetMask"); 1525 errorInEntry = true; 1526 } 1527 } 1528 else if (niciPentry != ipv4Data.cend()) 1529 { 1530 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask, 1531 &prefixLength)) 1532 { 1533 messages::propertyValueFormatError( 1534 asyncResp->res, niciPentry->netmask, 1535 pathString + "/SubnetMask"); 1536 errorInEntry = true; 1537 } 1538 } 1539 else 1540 { 1541 messages::propertyMissing(asyncResp->res, 1542 pathString + "/SubnetMask"); 1543 errorInEntry = true; 1544 } 1545 1546 if (gateway) 1547 { 1548 if (ipv4VerifyIpAndGetBitcount(*gateway)) 1549 { 1550 gw = &(*gateway); 1551 } 1552 else 1553 { 1554 messages::propertyValueFormatError( 1555 asyncResp->res, *gateway, pathString + "/Gateway"); 1556 errorInEntry = true; 1557 } 1558 } 1559 else if (niciPentry != ipv4Data.cend()) 1560 { 1561 gw = &niciPentry->gateway; 1562 } 1563 else 1564 { 1565 messages::propertyMissing(asyncResp->res, 1566 pathString + "/Gateway"); 1567 errorInEntry = true; 1568 } 1569 1570 if (errorInEntry) 1571 { 1572 return; 1573 } 1574 1575 if (niciPentry != ipv4Data.cend()) 1576 { 1577 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, 1578 *gw, *addr, asyncResp); 1579 niciPentry = 1580 getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); 1581 } 1582 else 1583 { 1584 createIPv4(ifaceId, prefixLength, *gateway, *address, 1585 asyncResp); 1586 } 1587 entryIdx++; 1588 } 1589 else 1590 { 1591 if (niciPentry == ipv4Data.cend()) 1592 { 1593 // Requesting a DELETE/DO NOT MODIFY action for an item 1594 // that isn't present on the eth(n) interface. Input JSON is 1595 // in error, so bail out. 1596 if (thisJson.is_null()) 1597 { 1598 messages::resourceCannotBeDeleted(asyncResp->res); 1599 return; 1600 } 1601 messages::propertyValueFormatError( 1602 asyncResp->res, 1603 thisJson.dump(2, ' ', true, 1604 nlohmann::json::error_handler_t::replace), 1605 pathString); 1606 return; 1607 } 1608 1609 if (thisJson.is_null()) 1610 { 1611 deleteIPv4(ifaceId, niciPentry->id, asyncResp); 1612 } 1613 if (niciPentry != ipv4Data.cend()) 1614 { 1615 niciPentry = 1616 getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); 1617 } 1618 entryIdx++; 1619 } 1620 } 1621 } 1622 1623 void handleStaticNameServersPatch( 1624 const std::string& ifaceId, 1625 const std::vector<std::string>& updatedStaticNameServers, 1626 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1627 { 1628 crow::connections::systemBus->async_method_call( 1629 [asyncResp](const boost::system::error_code ec) { 1630 if (ec) 1631 { 1632 messages::internalError(asyncResp->res); 1633 return; 1634 } 1635 }, 1636 "xyz.openbmc_project.Network", 1637 "/xyz/openbmc_project/network/" + ifaceId, 1638 "org.freedesktop.DBus.Properties", "Set", 1639 "xyz.openbmc_project.Network.EthernetInterface", 1640 "StaticNameServers", 1641 std::variant<std::vector<std::string>>{updatedStaticNameServers}); 1642 } 1643 1644 void handleIPv6StaticAddressesPatch( 1645 const std::string& ifaceId, const nlohmann::json& input, 1646 const boost::container::flat_set<IPv6AddressData>& ipv6Data, 1647 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1648 { 1649 if (!input.is_array() || input.empty()) 1650 { 1651 messages::propertyValueTypeError( 1652 asyncResp->res, 1653 input.dump(2, ' ', true, 1654 nlohmann::json::error_handler_t::replace), 1655 "IPv6StaticAddresses"); 1656 return; 1657 } 1658 size_t entryIdx = 1; 1659 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry = 1660 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1661 for (const nlohmann::json& thisJson : input) 1662 { 1663 std::string pathString = 1664 "IPv6StaticAddresses/" + std::to_string(entryIdx); 1665 1666 if (!thisJson.is_null() && !thisJson.empty()) 1667 { 1668 std::optional<std::string> address; 1669 std::optional<uint8_t> prefixLength; 1670 nlohmann::json thisJsonCopy = thisJson; 1671 if (!json_util::readJson(thisJsonCopy, asyncResp->res, 1672 "Address", address, "PrefixLength", 1673 prefixLength)) 1674 { 1675 messages::propertyValueFormatError( 1676 asyncResp->res, 1677 thisJson.dump(2, ' ', true, 1678 nlohmann::json::error_handler_t::replace), 1679 pathString); 1680 return; 1681 } 1682 1683 const std::string* addr; 1684 uint8_t prefix; 1685 1686 // Find the address and prefixLength values. Any values that are 1687 // not explicitly provided are assumed to be unmodified from the 1688 // current state of the interface. Merge existing state into the 1689 // current request. 1690 if (address) 1691 { 1692 addr = &(*address); 1693 } 1694 else if (niciPentry != ipv6Data.end()) 1695 { 1696 addr = &(niciPentry->address); 1697 } 1698 else 1699 { 1700 messages::propertyMissing(asyncResp->res, 1701 pathString + "/Address"); 1702 return; 1703 } 1704 1705 if (prefixLength) 1706 { 1707 prefix = *prefixLength; 1708 } 1709 else if (niciPentry != ipv6Data.end()) 1710 { 1711 prefix = niciPentry->prefixLength; 1712 } 1713 else 1714 { 1715 messages::propertyMissing(asyncResp->res, 1716 pathString + "/PrefixLength"); 1717 return; 1718 } 1719 1720 if (niciPentry != ipv6Data.end()) 1721 { 1722 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr, 1723 asyncResp); 1724 niciPentry = 1725 getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); 1726 } 1727 else 1728 { 1729 createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1730 } 1731 entryIdx++; 1732 } 1733 else 1734 { 1735 if (niciPentry == ipv6Data.end()) 1736 { 1737 // Requesting a DELETE/DO NOT MODIFY action for an item 1738 // that isn't present on the eth(n) interface. Input JSON is 1739 // in error, so bail out. 1740 if (thisJson.is_null()) 1741 { 1742 messages::resourceCannotBeDeleted(asyncResp->res); 1743 return; 1744 } 1745 messages::propertyValueFormatError( 1746 asyncResp->res, 1747 thisJson.dump(2, ' ', true, 1748 nlohmann::json::error_handler_t::replace), 1749 pathString); 1750 return; 1751 } 1752 1753 if (thisJson.is_null()) 1754 { 1755 deleteIPv6(ifaceId, niciPentry->id, asyncResp); 1756 } 1757 if (niciPentry != ipv6Data.cend()) 1758 { 1759 niciPentry = 1760 getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); 1761 } 1762 entryIdx++; 1763 } 1764 } 1765 } 1766 1767 void parseInterfaceData( 1768 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1769 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1770 const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1771 const boost::container::flat_set<IPv6AddressData>& ipv6Data) 1772 { 1773 constexpr const std::array<const char*, 1> inventoryForEthernet = { 1774 "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1775 1776 nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 1777 jsonResponse["Id"] = ifaceId; 1778 jsonResponse["@odata.id"] = 1779 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; 1780 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1781 1782 auto health = std::make_shared<HealthPopulate>(asyncResp); 1783 1784 crow::connections::systemBus->async_method_call( 1785 [health](const boost::system::error_code ec, 1786 std::vector<std::string>& resp) { 1787 if (ec) 1788 { 1789 return; 1790 } 1791 1792 health->inventory = std::move(resp); 1793 }, 1794 "xyz.openbmc_project.ObjectMapper", 1795 "/xyz/openbmc_project/object_mapper", 1796 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 1797 int32_t(0), inventoryForEthernet); 1798 1799 health->populate(); 1800 1801 if (ethData.nicEnabled) 1802 { 1803 jsonResponse["LinkStatus"] = "LinkUp"; 1804 jsonResponse["Status"]["State"] = "Enabled"; 1805 } 1806 else 1807 { 1808 jsonResponse["LinkStatus"] = "NoLink"; 1809 jsonResponse["Status"]["State"] = "Disabled"; 1810 } 1811 1812 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 1813 jsonResponse["SpeedMbps"] = ethData.speed; 1814 jsonResponse["MACAddress"] = ethData.mac_address; 1815 jsonResponse["DHCPv4"]["DHCPEnabled"] = 1816 translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 1817 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled; 1818 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled; 1819 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled; 1820 1821 jsonResponse["DHCPv6"]["OperatingMode"] = 1822 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful" 1823 : "Disabled"; 1824 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled; 1825 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled; 1826 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled; 1827 1828 if (!ethData.hostname.empty()) 1829 { 1830 jsonResponse["HostName"] = ethData.hostname; 1831 1832 // When domain name is empty then it means, that it is a network 1833 // without domain names, and the host name itself must be treated as 1834 // FQDN 1835 std::string fqdn = ethData.hostname; 1836 if (!ethData.domainnames.empty()) 1837 { 1838 fqdn += "." + ethData.domainnames[0]; 1839 } 1840 jsonResponse["FQDN"] = fqdn; 1841 } 1842 1843 jsonResponse["VLANs"] = { 1844 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1845 ifaceId + "/VLANs"}}; 1846 1847 jsonResponse["NameServers"] = ethData.nameServers; 1848 jsonResponse["StaticNameServers"] = ethData.staticNameServers; 1849 1850 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 1851 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 1852 ipv4Array = nlohmann::json::array(); 1853 ipv4StaticArray = nlohmann::json::array(); 1854 for (auto& ipv4Config : ipv4Data) 1855 { 1856 1857 std::string gatewayStr = ipv4Config.gateway; 1858 if (gatewayStr.empty()) 1859 { 1860 gatewayStr = "0.0.0.0"; 1861 } 1862 1863 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin}, 1864 {"SubnetMask", ipv4Config.netmask}, 1865 {"Address", ipv4Config.address}, 1866 {"Gateway", gatewayStr}}); 1867 if (ipv4Config.origin == "Static") 1868 { 1869 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin}, 1870 {"SubnetMask", ipv4Config.netmask}, 1871 {"Address", ipv4Config.address}, 1872 {"Gateway", gatewayStr}}); 1873 } 1874 } 1875 1876 std::string ipv6GatewayStr = ethData.ipv6_default_gateway; 1877 if (ipv6GatewayStr.empty()) 1878 { 1879 ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 1880 } 1881 1882 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1883 1884 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 1885 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 1886 ipv6Array = nlohmann::json::array(); 1887 ipv6StaticArray = nlohmann::json::array(); 1888 nlohmann::json& ipv6AddrPolicyTable = 1889 jsonResponse["IPv6AddressPolicyTable"]; 1890 ipv6AddrPolicyTable = nlohmann::json::array(); 1891 for (auto& ipv6Config : ipv6Data) 1892 { 1893 ipv6Array.push_back({{"Address", ipv6Config.address}, 1894 {"PrefixLength", ipv6Config.prefixLength}, 1895 {"AddressOrigin", ipv6Config.origin}, 1896 {"AddressState", nullptr}}); 1897 if (ipv6Config.origin == "Static") 1898 { 1899 ipv6StaticArray.push_back( 1900 {{"Address", ipv6Config.address}, 1901 {"PrefixLength", ipv6Config.prefixLength}, 1902 {"AddressOrigin", ipv6Config.origin}, 1903 {"AddressState", nullptr}}); 1904 } 1905 } 1906 } 1907 1908 /** 1909 * Functions triggers appropriate requests on DBus 1910 */ 1911 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1912 const crow::Request&, 1913 const std::vector<std::string>& params) override 1914 { 1915 if (params.size() != 1) 1916 { 1917 messages::internalError(asyncResp->res); 1918 return; 1919 } 1920 1921 getEthernetIfaceData( 1922 params[0], 1923 [this, asyncResp, ifaceId{std::string(params[0])}]( 1924 const bool& success, const EthernetInterfaceData& ethData, 1925 const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1926 const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 1927 if (!success) 1928 { 1929 // TODO(Pawel)consider distinguish between non existing 1930 // object, and other errors 1931 messages::resourceNotFound(asyncResp->res, 1932 "EthernetInterface", ifaceId); 1933 return; 1934 } 1935 1936 asyncResp->res.jsonValue["@odata.type"] = 1937 "#EthernetInterface.v1_4_1.EthernetInterface"; 1938 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 1939 asyncResp->res.jsonValue["Description"] = 1940 "Management Network Interface"; 1941 1942 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, 1943 ipv6Data); 1944 }); 1945 } 1946 1947 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1948 const crow::Request& req, 1949 const std::vector<std::string>& params) override 1950 { 1951 1952 if (params.size() != 1) 1953 { 1954 messages::internalError(asyncResp->res); 1955 return; 1956 } 1957 1958 const std::string& ifaceId = params[0]; 1959 1960 std::optional<std::string> hostname; 1961 std::optional<std::string> fqdn; 1962 std::optional<std::string> macAddress; 1963 std::optional<std::string> ipv6DefaultGateway; 1964 std::optional<nlohmann::json> ipv4StaticAddresses; 1965 std::optional<nlohmann::json> ipv6StaticAddresses; 1966 std::optional<std::vector<std::string>> staticNameServers; 1967 std::optional<nlohmann::json> dhcpv4; 1968 std::optional<nlohmann::json> dhcpv6; 1969 std::optional<bool> interfaceEnabled; 1970 DHCPParameters v4dhcpParms; 1971 DHCPParameters v6dhcpParms; 1972 1973 if (!json_util::readJson( 1974 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1975 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", 1976 macAddress, "StaticNameServers", staticNameServers, 1977 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", 1978 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1979 "InterfaceEnabled", interfaceEnabled)) 1980 { 1981 return; 1982 } 1983 if (dhcpv4) 1984 { 1985 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", 1986 v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 1987 v4dhcpParms.useDNSServers, "UseNTPServers", 1988 v4dhcpParms.useNTPServers, "UseDomainName", 1989 v4dhcpParms.useUseDomainName)) 1990 { 1991 return; 1992 } 1993 } 1994 1995 if (dhcpv6) 1996 { 1997 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode", 1998 v6dhcpParms.dhcpv6OperatingMode, 1999 "UseDNSServers", v6dhcpParms.useDNSServers, 2000 "UseNTPServers", v6dhcpParms.useNTPServers, 2001 "UseDomainName", 2002 v6dhcpParms.useUseDomainName)) 2003 { 2004 return; 2005 } 2006 } 2007 2008 // Get single eth interface data, and call the below callback for 2009 // JSON preparation 2010 getEthernetIfaceData( 2011 ifaceId, 2012 [this, asyncResp, ifaceId, hostname = std::move(hostname), 2013 fqdn = std::move(fqdn), macAddress = std::move(macAddress), 2014 ipv4StaticAddresses = std::move(ipv4StaticAddresses), 2015 ipv6DefaultGateway = std::move(ipv6DefaultGateway), 2016 ipv6StaticAddresses = std::move(ipv6StaticAddresses), 2017 staticNameServers = std::move(staticNameServers), 2018 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), 2019 v4dhcpParms = std::move(v4dhcpParms), 2020 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 2021 const bool& success, const EthernetInterfaceData& ethData, 2022 const boost::container::flat_set<IPv4AddressData>& ipv4Data, 2023 const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 2024 if (!success) 2025 { 2026 // ... otherwise return error 2027 // TODO(Pawel)consider distinguish between non existing 2028 // object, and other errors 2029 messages::resourceNotFound(asyncResp->res, 2030 "Ethernet Interface", ifaceId); 2031 return; 2032 } 2033 2034 if (dhcpv4 || dhcpv6) 2035 { 2036 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms, 2037 asyncResp); 2038 } 2039 2040 if (hostname) 2041 { 2042 handleHostnamePatch(*hostname, asyncResp); 2043 } 2044 2045 if (fqdn) 2046 { 2047 handleFqdnPatch(ifaceId, *fqdn, asyncResp); 2048 } 2049 2050 if (macAddress) 2051 { 2052 handleMACAddressPatch(ifaceId, *macAddress, asyncResp); 2053 } 2054 2055 if (ipv4StaticAddresses) 2056 { 2057 // TODO(ed) for some reason the capture of ipv4Addresses 2058 // above is returning a const value, not a non-const 2059 // value. This doesn't really work for us, as we need to 2060 // be able to efficiently move out the intermedia 2061 // nlohmann::json objects. This makes a copy of the 2062 // structure, and operates on that, but could be done 2063 // more efficiently 2064 nlohmann::json ipv4Static = *ipv4StaticAddresses; 2065 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, 2066 asyncResp); 2067 } 2068 2069 if (staticNameServers) 2070 { 2071 handleStaticNameServersPatch(ifaceId, *staticNameServers, 2072 asyncResp); 2073 } 2074 2075 if (ipv6DefaultGateway) 2076 { 2077 messages::propertyNotWritable(asyncResp->res, 2078 "IPv6DefaultGateway"); 2079 } 2080 2081 if (ipv6StaticAddresses) 2082 { 2083 nlohmann::json ipv6Static = *ipv6StaticAddresses; 2084 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, 2085 ipv6Data, asyncResp); 2086 } 2087 2088 if (interfaceEnabled) 2089 { 2090 setEthernetInterfaceBoolProperty( 2091 ifaceId, "NICEnabled", *interfaceEnabled, asyncResp); 2092 } 2093 }); 2094 } 2095 }; 2096 2097 /** 2098 * VlanNetworkInterface derived class for delivering VLANNetworkInterface 2099 * Schema 2100 */ 2101 class VlanNetworkInterface : public Node 2102 { 2103 public: 2104 /* 2105 * Default Constructor 2106 */ 2107 VlanNetworkInterface(App& app) : 2108 Node(app, 2109 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/", 2110 std::string(), std::string()) 2111 { 2112 entityPrivileges = { 2113 {boost::beast::http::verb::get, {{"Login"}}}, 2114 {boost::beast::http::verb::head, {{"Login"}}}, 2115 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 2116 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 2117 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 2118 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 2119 } 2120 2121 private: 2122 void parseInterfaceData(nlohmann::json& jsonResponse, 2123 const std::string& parentIfaceId, 2124 const std::string& ifaceId, 2125 const EthernetInterfaceData& ethData) 2126 { 2127 // Fill out obvious data... 2128 jsonResponse["Id"] = ifaceId; 2129 jsonResponse["@odata.id"] = 2130 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parentIfaceId + 2131 "/VLANs/" + ifaceId; 2132 2133 jsonResponse["VLANEnable"] = true; 2134 if (!ethData.vlan_id.empty()) 2135 { 2136 jsonResponse["VLANId"] = ethData.vlan_id.back(); 2137 } 2138 } 2139 2140 bool verifyNames(const std::string& parent, const std::string& iface) 2141 { 2142 if (!boost::starts_with(iface, parent + "_")) 2143 { 2144 return false; 2145 } 2146 return true; 2147 } 2148 2149 /** 2150 * Functions triggers appropriate requests on DBus 2151 */ 2152 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2153 const crow::Request&, 2154 const std::vector<std::string>& params) override 2155 { 2156 // TODO(Pawel) this shall be parameterized call (two params) to get 2157 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. 2158 // Check if there is required param, truly entering this shall be 2159 // impossible. 2160 if (params.size() != 2) 2161 { 2162 messages::internalError(asyncResp->res); 2163 return; 2164 } 2165 2166 const std::string& parentIfaceId = params[0]; 2167 const std::string& ifaceId = params[1]; 2168 asyncResp->res.jsonValue["@odata.type"] = 2169 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 2170 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 2171 2172 if (!verifyNames(parentIfaceId, ifaceId)) 2173 { 2174 return; 2175 } 2176 2177 // Get single eth interface data, and call the below callback for 2178 // JSON preparation 2179 getEthernetIfaceData( 2180 params[1], 2181 [this, asyncResp, parentIfaceId{std::string(params[0])}, 2182 ifaceId{std::string(params[1])}]( 2183 const bool& success, const EthernetInterfaceData& ethData, 2184 const boost::container::flat_set<IPv4AddressData>&, 2185 const boost::container::flat_set<IPv6AddressData>&) { 2186 if (success && ethData.vlan_id.size() != 0) 2187 { 2188 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 2189 ifaceId, ethData); 2190 } 2191 else 2192 { 2193 // ... otherwise return error 2194 // TODO(Pawel)consider distinguish between non existing 2195 // object, and other errors 2196 messages::resourceNotFound( 2197 asyncResp->res, "VLAN Network Interface", ifaceId); 2198 } 2199 }); 2200 } 2201 2202 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2203 const crow::Request& req, 2204 const std::vector<std::string>& params) override 2205 { 2206 2207 if (params.size() != 2) 2208 { 2209 messages::internalError(asyncResp->res); 2210 return; 2211 } 2212 2213 const std::string& parentIfaceId = params[0]; 2214 const std::string& ifaceId = params[1]; 2215 2216 if (!verifyNames(parentIfaceId, ifaceId)) 2217 { 2218 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 2219 ifaceId); 2220 return; 2221 } 2222 2223 bool vlanEnable = false; 2224 uint32_t vlanId = 0; 2225 2226 if (!json_util::readJson(req, asyncResp->res, "VLANEnable", vlanEnable, 2227 "VLANId", vlanId)) 2228 { 2229 return; 2230 } 2231 2232 // Get single eth interface data, and call the below callback for 2233 // JSON preparation 2234 getEthernetIfaceData( 2235 params[1], 2236 [asyncResp, parentIfaceId{std::string(params[0])}, 2237 ifaceId{std::string(params[1])}, &vlanEnable, 2238 &vlanId](const bool& success, const EthernetInterfaceData& ethData, 2239 const boost::container::flat_set<IPv4AddressData>&, 2240 const boost::container::flat_set<IPv6AddressData>&) { 2241 if (success && !ethData.vlan_id.empty()) 2242 { 2243 auto callback = 2244 [asyncResp](const boost::system::error_code ec) { 2245 if (ec) 2246 { 2247 messages::internalError(asyncResp->res); 2248 } 2249 }; 2250 2251 if (vlanEnable == true) 2252 { 2253 crow::connections::systemBus->async_method_call( 2254 std::move(callback), "xyz.openbmc_project.Network", 2255 "/xyz/openbmc_project/network/" + ifaceId, 2256 "org.freedesktop.DBus.Properties", "Set", 2257 "xyz.openbmc_project.Network.VLAN", "Id", 2258 std::variant<uint32_t>(vlanId)); 2259 } 2260 else 2261 { 2262 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the " 2263 "vlan interface"; 2264 crow::connections::systemBus->async_method_call( 2265 std::move(callback), "xyz.openbmc_project.Network", 2266 std::string("/xyz/openbmc_project/network/") + 2267 ifaceId, 2268 "xyz.openbmc_project.Object.Delete", "Delete"); 2269 } 2270 } 2271 else 2272 { 2273 // TODO(Pawel)consider distinguish between non existing 2274 // object, and other errors 2275 messages::resourceNotFound( 2276 asyncResp->res, "VLAN Network Interface", ifaceId); 2277 return; 2278 } 2279 }); 2280 } 2281 2282 void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2283 const crow::Request&, 2284 const std::vector<std::string>& params) override 2285 { 2286 if (params.size() != 2) 2287 { 2288 messages::internalError(asyncResp->res); 2289 return; 2290 } 2291 2292 const std::string& parentIfaceId = params[0]; 2293 const std::string& ifaceId = params[1]; 2294 2295 if (!verifyNames(parentIfaceId, ifaceId)) 2296 { 2297 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 2298 ifaceId); 2299 return; 2300 } 2301 2302 // Get single eth interface data, and call the below callback for 2303 // JSON preparation 2304 getEthernetIfaceData( 2305 params[1], 2306 [asyncResp, parentIfaceId{std::string(params[0])}, 2307 ifaceId{std::string(params[1])}]( 2308 const bool& success, const EthernetInterfaceData& ethData, 2309 const boost::container::flat_set<IPv4AddressData>&, 2310 const boost::container::flat_set<IPv6AddressData>&) { 2311 if (success && !ethData.vlan_id.empty()) 2312 { 2313 auto callback = 2314 [asyncResp](const boost::system::error_code ec) { 2315 if (ec) 2316 { 2317 messages::internalError(asyncResp->res); 2318 } 2319 }; 2320 crow::connections::systemBus->async_method_call( 2321 std::move(callback), "xyz.openbmc_project.Network", 2322 std::string("/xyz/openbmc_project/network/") + ifaceId, 2323 "xyz.openbmc_project.Object.Delete", "Delete"); 2324 } 2325 else 2326 { 2327 // ... otherwise return error 2328 // TODO(Pawel)consider distinguish between non existing 2329 // object, and other errors 2330 messages::resourceNotFound( 2331 asyncResp->res, "VLAN Network Interface", ifaceId); 2332 } 2333 }); 2334 } 2335 }; 2336 2337 /** 2338 * VlanNetworkInterfaceCollection derived class for delivering 2339 * VLANNetworkInterface Collection Schema 2340 */ 2341 class VlanNetworkInterfaceCollection : public Node 2342 { 2343 public: 2344 VlanNetworkInterfaceCollection(App& app) : 2345 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", 2346 std::string()) 2347 { 2348 entityPrivileges = { 2349 {boost::beast::http::verb::get, {{"Login"}}}, 2350 {boost::beast::http::verb::head, {{"Login"}}}, 2351 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 2352 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 2353 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 2354 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 2355 } 2356 2357 private: 2358 /** 2359 * Functions triggers appropriate requests on DBus 2360 */ 2361 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2362 const crow::Request&, 2363 const std::vector<std::string>& params) override 2364 { 2365 if (params.size() != 1) 2366 { 2367 // This means there is a problem with the router 2368 messages::internalError(asyncResp->res); 2369 return; 2370 } 2371 2372 const std::string& rootInterfaceName = params[0]; 2373 2374 // Get eth interface list, and call the below callback for JSON 2375 // preparation 2376 getEthernetIfaceList( 2377 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}]( 2378 const bool& success, 2379 const boost::container::flat_set<std::string>& ifaceList) { 2380 if (!success) 2381 { 2382 messages::internalError(asyncResp->res); 2383 return; 2384 } 2385 2386 if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 2387 { 2388 messages::resourceNotFound(asyncResp->res, 2389 "VLanNetworkInterfaceCollection", 2390 rootInterfaceName); 2391 return; 2392 } 2393 2394 asyncResp->res.jsonValue["@odata.type"] = 2395 "#VLanNetworkInterfaceCollection." 2396 "VLanNetworkInterfaceCollection"; 2397 asyncResp->res.jsonValue["Name"] = 2398 "VLAN Network Interface Collection"; 2399 2400 nlohmann::json ifaceArray = nlohmann::json::array(); 2401 2402 for (const std::string& ifaceItem : ifaceList) 2403 { 2404 if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) 2405 { 2406 std::string path = 2407 "/redfish/v1/Managers/bmc/EthernetInterfaces/"; 2408 path += rootInterfaceName; 2409 path += "/VLANs/"; 2410 path += ifaceItem; 2411 ifaceArray.push_back({{"@odata.id", std::move(path)}}); 2412 } 2413 } 2414 2415 asyncResp->res.jsonValue["Members@odata.count"] = 2416 ifaceArray.size(); 2417 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 2418 asyncResp->res.jsonValue["@odata.id"] = 2419 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 2420 rootInterfaceName + "/VLANs"; 2421 }); 2422 } 2423 2424 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2425 const crow::Request& req, 2426 const std::vector<std::string>& params) override 2427 { 2428 if (params.size() != 1) 2429 { 2430 messages::internalError(asyncResp->res); 2431 return; 2432 } 2433 bool vlanEnable = false; 2434 uint32_t vlanId = 0; 2435 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId, 2436 "VLANEnable", vlanEnable)) 2437 { 2438 return; 2439 } 2440 // Need both vlanId and vlanEnable to service this request 2441 if (!vlanId) 2442 { 2443 messages::propertyMissing(asyncResp->res, "VLANId"); 2444 } 2445 if (!vlanEnable) 2446 { 2447 messages::propertyMissing(asyncResp->res, "VLANEnable"); 2448 } 2449 if (static_cast<bool>(vlanId) ^ vlanEnable) 2450 { 2451 return; 2452 } 2453 2454 const std::string& rootInterfaceName = params[0]; 2455 auto callback = [asyncResp](const boost::system::error_code ec) { 2456 if (ec) 2457 { 2458 // TODO(ed) make more consistent error messages based on 2459 // phosphor-network responses 2460 messages::internalError(asyncResp->res); 2461 return; 2462 } 2463 messages::created(asyncResp->res); 2464 }; 2465 crow::connections::systemBus->async_method_call( 2466 std::move(callback), "xyz.openbmc_project.Network", 2467 "/xyz/openbmc_project/network", 2468 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 2469 rootInterfaceName, vlanId); 2470 } 2471 }; 2472 } // namespace redfish 2473