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