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