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 <node.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 void handleHostnamePatch(const std::string& hostname, 1028 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1029 { 1030 // SHOULD handle host names of up to 255 characters(RFC 1123) 1031 if (hostname.length() > 255) 1032 { 1033 messages::propertyValueFormatError(asyncResp->res, hostname, 1034 "HostName"); 1035 return; 1036 } 1037 crow::connections::systemBus->async_method_call( 1038 [asyncResp](const boost::system::error_code ec) { 1039 if (ec) 1040 { 1041 messages::internalError(asyncResp->res); 1042 } 1043 }, 1044 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", 1045 "org.freedesktop.DBus.Properties", "Set", 1046 "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 1047 std::variant<std::string>(hostname)); 1048 } 1049 1050 void handleDomainnamePatch(const std::string& ifaceId, 1051 const std::string& domainname, 1052 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1053 { 1054 std::vector<std::string> vectorDomainname = {domainname}; 1055 crow::connections::systemBus->async_method_call( 1056 [asyncResp](const boost::system::error_code ec) { 1057 if (ec) 1058 { 1059 messages::internalError(asyncResp->res); 1060 } 1061 }, 1062 "xyz.openbmc_project.Network", 1063 "/xyz/openbmc_project/network/" + ifaceId, 1064 "org.freedesktop.DBus.Properties", "Set", 1065 "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 1066 std::variant<std::vector<std::string>>(vectorDomainname)); 1067 } 1068 1069 bool isHostnameValid(const std::string& hostname) 1070 { 1071 // A valid host name can never have the dotted-decimal form (RFC 1123) 1072 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 1073 { 1074 return false; 1075 } 1076 // Each label(hostname/subdomains) within a valid FQDN 1077 // MUST handle host names of up to 63 characters (RFC 1123) 1078 // labels cannot start or end with hyphens (RFC 952) 1079 // labels can start with numbers (RFC 1123) 1080 const std::regex pattern( 1081 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 1082 1083 return std::regex_match(hostname, pattern); 1084 } 1085 1086 bool isDomainnameValid(const std::string& domainname) 1087 { 1088 // Can have multiple subdomains 1089 // Top Level Domain's min length is 2 character 1090 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]" 1091 "{1,30}\\.)*[a-zA-Z]{2,}$"); 1092 1093 return std::regex_match(domainname, pattern); 1094 } 1095 1096 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 1097 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1098 { 1099 // Total length of FQDN must not exceed 255 characters(RFC 1035) 1100 if (fqdn.length() > 255) 1101 { 1102 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1103 return; 1104 } 1105 1106 size_t pos = fqdn.find('.'); 1107 if (pos == std::string::npos) 1108 { 1109 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1110 return; 1111 } 1112 1113 std::string hostname; 1114 std::string domainname; 1115 domainname = (fqdn).substr(pos + 1); 1116 hostname = (fqdn).substr(0, pos); 1117 1118 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1119 { 1120 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1121 return; 1122 } 1123 1124 handleHostnamePatch(hostname, asyncResp); 1125 handleDomainnamePatch(ifaceId, domainname, asyncResp); 1126 } 1127 1128 void handleMACAddressPatch(const std::string& ifaceId, 1129 const std::string& macAddress, 1130 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1131 { 1132 crow::connections::systemBus->async_method_call( 1133 [asyncResp, macAddress](const boost::system::error_code ec) { 1134 if (ec) 1135 { 1136 messages::internalError(asyncResp->res); 1137 return; 1138 } 1139 }, 1140 "xyz.openbmc_project.Network", 1141 "/xyz/openbmc_project/network/" + ifaceId, 1142 "org.freedesktop.DBus.Properties", "Set", 1143 "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1144 std::variant<std::string>(macAddress)); 1145 } 1146 1147 void setDHCPEnabled(const std::string& ifaceId, const std::string& propertyName, 1148 const bool v4Value, const bool v6Value, 1149 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1150 { 1151 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1152 crow::connections::systemBus->async_method_call( 1153 [asyncResp](const boost::system::error_code ec) { 1154 if (ec) 1155 { 1156 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1157 messages::internalError(asyncResp->res); 1158 return; 1159 } 1160 messages::success(asyncResp->res); 1161 }, 1162 "xyz.openbmc_project.Network", 1163 "/xyz/openbmc_project/network/" + ifaceId, 1164 "org.freedesktop.DBus.Properties", "Set", 1165 "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1166 std::variant<std::string>{dhcp}); 1167 } 1168 1169 void setEthernetInterfaceBoolProperty( 1170 const std::string& ifaceId, const std::string& propertyName, 1171 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1172 { 1173 crow::connections::systemBus->async_method_call( 1174 [asyncResp](const boost::system::error_code ec) { 1175 if (ec) 1176 { 1177 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1178 messages::internalError(asyncResp->res); 1179 return; 1180 } 1181 }, 1182 "xyz.openbmc_project.Network", 1183 "/xyz/openbmc_project/network/" + ifaceId, 1184 "org.freedesktop.DBus.Properties", "Set", 1185 "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1186 std::variant<bool>{value}); 1187 } 1188 1189 void setDHCPv4Config(const std::string& propertyName, const bool& value, 1190 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1191 { 1192 BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1193 crow::connections::systemBus->async_method_call( 1194 [asyncResp](const boost::system::error_code ec) { 1195 if (ec) 1196 { 1197 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1198 messages::internalError(asyncResp->res); 1199 return; 1200 } 1201 }, 1202 "xyz.openbmc_project.Network", 1203 "/xyz/openbmc_project/network/config/dhcp", 1204 "org.freedesktop.DBus.Properties", "Set", 1205 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1206 std::variant<bool>{value}); 1207 } 1208 1209 void handleDHCPPatch(const std::string& ifaceId, 1210 const EthernetInterfaceData& ethData, 1211 const DHCPParameters& v4dhcpParms, 1212 const DHCPParameters& v6dhcpParms, 1213 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1214 { 1215 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 1216 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false); 1217 1218 bool nextv4DHCPState = 1219 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 1220 1221 bool nextv6DHCPState{}; 1222 if (v6dhcpParms.dhcpv6OperatingMode) 1223 { 1224 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 1225 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 1226 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 1227 { 1228 messages::propertyValueFormatError(asyncResp->res, 1229 *v6dhcpParms.dhcpv6OperatingMode, 1230 "OperatingMode"); 1231 return; 1232 } 1233 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 1234 } 1235 else 1236 { 1237 nextv6DHCPState = ipv6Active; 1238 } 1239 1240 bool nextDNS{}; 1241 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers) 1242 { 1243 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers) 1244 { 1245 messages::generalError(asyncResp->res); 1246 return; 1247 } 1248 nextDNS = *v4dhcpParms.useDNSServers; 1249 } 1250 else if (v4dhcpParms.useDNSServers) 1251 { 1252 nextDNS = *v4dhcpParms.useDNSServers; 1253 } 1254 else if (v6dhcpParms.useDNSServers) 1255 { 1256 nextDNS = *v6dhcpParms.useDNSServers; 1257 } 1258 else 1259 { 1260 nextDNS = ethData.DNSEnabled; 1261 } 1262 1263 bool nextNTP{}; 1264 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers) 1265 { 1266 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers) 1267 { 1268 messages::generalError(asyncResp->res); 1269 return; 1270 } 1271 nextNTP = *v4dhcpParms.useNTPServers; 1272 } 1273 else if (v4dhcpParms.useNTPServers) 1274 { 1275 nextNTP = *v4dhcpParms.useNTPServers; 1276 } 1277 else if (v6dhcpParms.useNTPServers) 1278 { 1279 nextNTP = *v6dhcpParms.useNTPServers; 1280 } 1281 else 1282 { 1283 nextNTP = ethData.NTPEnabled; 1284 } 1285 1286 bool nextUseDomain{}; 1287 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName) 1288 { 1289 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName) 1290 { 1291 messages::generalError(asyncResp->res); 1292 return; 1293 } 1294 nextUseDomain = *v4dhcpParms.useUseDomainName; 1295 } 1296 else if (v4dhcpParms.useUseDomainName) 1297 { 1298 nextUseDomain = *v4dhcpParms.useUseDomainName; 1299 } 1300 else if (v6dhcpParms.useUseDomainName) 1301 { 1302 nextUseDomain = *v6dhcpParms.useUseDomainName; 1303 } 1304 else 1305 { 1306 nextUseDomain = ethData.HostNameEnabled; 1307 } 1308 1309 BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 1310 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 1311 asyncResp); 1312 BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 1313 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1314 BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 1315 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 1316 BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 1317 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1318 } 1319 1320 boost::container::flat_set<IPv4AddressData>::const_iterator 1321 getNextStaticIpEntry( 1322 const boost::container::flat_set<IPv4AddressData>::const_iterator& head, 1323 const boost::container::flat_set<IPv4AddressData>::const_iterator& end) 1324 { 1325 return std::find_if(head, end, [](const IPv4AddressData& value) { 1326 return value.origin == "Static"; 1327 }); 1328 } 1329 1330 boost::container::flat_set<IPv6AddressData>::const_iterator 1331 getNextStaticIpEntry( 1332 const boost::container::flat_set<IPv6AddressData>::const_iterator& head, 1333 const boost::container::flat_set<IPv6AddressData>::const_iterator& end) 1334 { 1335 return std::find_if(head, end, [](const IPv6AddressData& value) { 1336 return value.origin == "Static"; 1337 }); 1338 } 1339 1340 void handleIPv4StaticPatch( 1341 const std::string& ifaceId, nlohmann::json& input, 1342 const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1343 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1344 { 1345 if ((!input.is_array()) || input.empty()) 1346 { 1347 messages::propertyValueTypeError( 1348 asyncResp->res, 1349 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1350 "IPv4StaticAddresses"); 1351 return; 1352 } 1353 1354 unsigned entryIdx = 1; 1355 // Find the first static IP address currently active on the NIC and 1356 // match it to the first JSON element in the IPv4StaticAddresses array. 1357 // Match each subsequent JSON element to the next static IP programmed 1358 // into the NIC. 1359 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry = 1360 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 1361 1362 for (nlohmann::json& thisJson : input) 1363 { 1364 std::string pathString = 1365 "IPv4StaticAddresses/" + std::to_string(entryIdx); 1366 1367 if (!thisJson.is_null() && !thisJson.empty()) 1368 { 1369 std::optional<std::string> address; 1370 std::optional<std::string> subnetMask; 1371 std::optional<std::string> gateway; 1372 1373 if (!json_util::readJson(thisJson, asyncResp->res, "Address", 1374 address, "SubnetMask", subnetMask, 1375 "Gateway", gateway)) 1376 { 1377 messages::propertyValueFormatError( 1378 asyncResp->res, 1379 thisJson.dump(2, ' ', true, 1380 nlohmann::json::error_handler_t::replace), 1381 pathString); 1382 return; 1383 } 1384 1385 // Find the address/subnet/gateway values. Any values that are 1386 // not explicitly provided are assumed to be unmodified from the 1387 // current state of the interface. Merge existing state into the 1388 // current request. 1389 const std::string* addr = nullptr; 1390 const std::string* gw = nullptr; 1391 uint8_t prefixLength = 0; 1392 bool errorInEntry = false; 1393 if (address) 1394 { 1395 if (ipv4VerifyIpAndGetBitcount(*address)) 1396 { 1397 addr = &(*address); 1398 } 1399 else 1400 { 1401 messages::propertyValueFormatError(asyncResp->res, *address, 1402 pathString + "/Address"); 1403 errorInEntry = true; 1404 } 1405 } 1406 else if (niciPentry != ipv4Data.cend()) 1407 { 1408 addr = &(niciPentry->address); 1409 } 1410 else 1411 { 1412 messages::propertyMissing(asyncResp->res, 1413 pathString + "/Address"); 1414 errorInEntry = true; 1415 } 1416 1417 if (subnetMask) 1418 { 1419 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 1420 { 1421 messages::propertyValueFormatError( 1422 asyncResp->res, *subnetMask, 1423 pathString + "/SubnetMask"); 1424 errorInEntry = true; 1425 } 1426 } 1427 else if (niciPentry != ipv4Data.cend()) 1428 { 1429 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask, 1430 &prefixLength)) 1431 { 1432 messages::propertyValueFormatError( 1433 asyncResp->res, niciPentry->netmask, 1434 pathString + "/SubnetMask"); 1435 errorInEntry = true; 1436 } 1437 } 1438 else 1439 { 1440 messages::propertyMissing(asyncResp->res, 1441 pathString + "/SubnetMask"); 1442 errorInEntry = true; 1443 } 1444 1445 if (gateway) 1446 { 1447 if (ipv4VerifyIpAndGetBitcount(*gateway)) 1448 { 1449 gw = &(*gateway); 1450 } 1451 else 1452 { 1453 messages::propertyValueFormatError(asyncResp->res, *gateway, 1454 pathString + "/Gateway"); 1455 errorInEntry = true; 1456 } 1457 } 1458 else if (niciPentry != ipv4Data.cend()) 1459 { 1460 gw = &niciPentry->gateway; 1461 } 1462 else 1463 { 1464 messages::propertyMissing(asyncResp->res, 1465 pathString + "/Gateway"); 1466 errorInEntry = true; 1467 } 1468 1469 if (errorInEntry) 1470 { 1471 return; 1472 } 1473 1474 if (niciPentry != ipv4Data.cend()) 1475 { 1476 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, *gw, 1477 *addr, asyncResp); 1478 niciPentry = 1479 getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); 1480 } 1481 else 1482 { 1483 createIPv4(ifaceId, prefixLength, *gateway, *address, 1484 asyncResp); 1485 } 1486 entryIdx++; 1487 } 1488 else 1489 { 1490 if (niciPentry == ipv4Data.cend()) 1491 { 1492 // Requesting a DELETE/DO NOT MODIFY action for an item 1493 // that isn't present on the eth(n) interface. Input JSON is 1494 // in error, so bail out. 1495 if (thisJson.is_null()) 1496 { 1497 messages::resourceCannotBeDeleted(asyncResp->res); 1498 return; 1499 } 1500 messages::propertyValueFormatError( 1501 asyncResp->res, 1502 thisJson.dump(2, ' ', true, 1503 nlohmann::json::error_handler_t::replace), 1504 pathString); 1505 return; 1506 } 1507 1508 if (thisJson.is_null()) 1509 { 1510 deleteIPv4(ifaceId, niciPentry->id, asyncResp); 1511 } 1512 if (niciPentry != ipv4Data.cend()) 1513 { 1514 niciPentry = 1515 getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); 1516 } 1517 entryIdx++; 1518 } 1519 } 1520 } 1521 1522 void handleStaticNameServersPatch( 1523 const std::string& ifaceId, 1524 const std::vector<std::string>& updatedStaticNameServers, 1525 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1526 { 1527 crow::connections::systemBus->async_method_call( 1528 [asyncResp](const boost::system::error_code ec) { 1529 if (ec) 1530 { 1531 messages::internalError(asyncResp->res); 1532 return; 1533 } 1534 }, 1535 "xyz.openbmc_project.Network", 1536 "/xyz/openbmc_project/network/" + ifaceId, 1537 "org.freedesktop.DBus.Properties", "Set", 1538 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", 1539 std::variant<std::vector<std::string>>{updatedStaticNameServers}); 1540 } 1541 1542 void handleIPv6StaticAddressesPatch( 1543 const std::string& ifaceId, const nlohmann::json& input, 1544 const boost::container::flat_set<IPv6AddressData>& ipv6Data, 1545 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1546 { 1547 if (!input.is_array() || input.empty()) 1548 { 1549 messages::propertyValueTypeError( 1550 asyncResp->res, 1551 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1552 "IPv6StaticAddresses"); 1553 return; 1554 } 1555 size_t entryIdx = 1; 1556 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry = 1557 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1558 for (const nlohmann::json& thisJson : input) 1559 { 1560 std::string pathString = 1561 "IPv6StaticAddresses/" + std::to_string(entryIdx); 1562 1563 if (!thisJson.is_null() && !thisJson.empty()) 1564 { 1565 std::optional<std::string> address; 1566 std::optional<uint8_t> prefixLength; 1567 nlohmann::json thisJsonCopy = thisJson; 1568 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 1569 address, "PrefixLength", prefixLength)) 1570 { 1571 messages::propertyValueFormatError( 1572 asyncResp->res, 1573 thisJson.dump(2, ' ', true, 1574 nlohmann::json::error_handler_t::replace), 1575 pathString); 1576 return; 1577 } 1578 1579 const std::string* addr; 1580 uint8_t prefix; 1581 1582 // Find the address and prefixLength values. Any values that are 1583 // not explicitly provided are assumed to be unmodified from the 1584 // current state of the interface. Merge existing state into the 1585 // current request. 1586 if (address) 1587 { 1588 addr = &(*address); 1589 } 1590 else if (niciPentry != ipv6Data.end()) 1591 { 1592 addr = &(niciPentry->address); 1593 } 1594 else 1595 { 1596 messages::propertyMissing(asyncResp->res, 1597 pathString + "/Address"); 1598 return; 1599 } 1600 1601 if (prefixLength) 1602 { 1603 prefix = *prefixLength; 1604 } 1605 else if (niciPentry != ipv6Data.end()) 1606 { 1607 prefix = niciPentry->prefixLength; 1608 } 1609 else 1610 { 1611 messages::propertyMissing(asyncResp->res, 1612 pathString + "/PrefixLength"); 1613 return; 1614 } 1615 1616 if (niciPentry != ipv6Data.end()) 1617 { 1618 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr, 1619 asyncResp); 1620 niciPentry = 1621 getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); 1622 } 1623 else 1624 { 1625 createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1626 } 1627 entryIdx++; 1628 } 1629 else 1630 { 1631 if (niciPentry == ipv6Data.end()) 1632 { 1633 // Requesting a DELETE/DO NOT MODIFY action for an item 1634 // that isn't present on the eth(n) interface. Input JSON is 1635 // in error, so bail out. 1636 if (thisJson.is_null()) 1637 { 1638 messages::resourceCannotBeDeleted(asyncResp->res); 1639 return; 1640 } 1641 messages::propertyValueFormatError( 1642 asyncResp->res, 1643 thisJson.dump(2, ' ', true, 1644 nlohmann::json::error_handler_t::replace), 1645 pathString); 1646 return; 1647 } 1648 1649 if (thisJson.is_null()) 1650 { 1651 deleteIPv6(ifaceId, niciPentry->id, asyncResp); 1652 } 1653 if (niciPentry != ipv6Data.cend()) 1654 { 1655 niciPentry = 1656 getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); 1657 } 1658 entryIdx++; 1659 } 1660 } 1661 } 1662 1663 void parseInterfaceData( 1664 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1665 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1666 const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1667 const boost::container::flat_set<IPv6AddressData>& ipv6Data) 1668 { 1669 constexpr const std::array<const char*, 1> inventoryForEthernet = { 1670 "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1671 1672 nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 1673 jsonResponse["Id"] = ifaceId; 1674 jsonResponse["@odata.id"] = 1675 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; 1676 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1677 1678 auto health = std::make_shared<HealthPopulate>(asyncResp); 1679 1680 crow::connections::systemBus->async_method_call( 1681 [health](const boost::system::error_code ec, 1682 std::vector<std::string>& resp) { 1683 if (ec) 1684 { 1685 return; 1686 } 1687 1688 health->inventory = std::move(resp); 1689 }, 1690 "xyz.openbmc_project.ObjectMapper", 1691 "/xyz/openbmc_project/object_mapper", 1692 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0), 1693 inventoryForEthernet); 1694 1695 health->populate(); 1696 1697 if (ethData.nicEnabled) 1698 { 1699 jsonResponse["LinkStatus"] = "LinkUp"; 1700 jsonResponse["Status"]["State"] = "Enabled"; 1701 } 1702 else 1703 { 1704 jsonResponse["LinkStatus"] = "NoLink"; 1705 jsonResponse["Status"]["State"] = "Disabled"; 1706 } 1707 1708 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 1709 jsonResponse["SpeedMbps"] = ethData.speed; 1710 jsonResponse["MACAddress"] = ethData.mac_address; 1711 jsonResponse["DHCPv4"]["DHCPEnabled"] = 1712 translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 1713 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled; 1714 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled; 1715 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled; 1716 1717 jsonResponse["DHCPv6"]["OperatingMode"] = 1718 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful" 1719 : "Disabled"; 1720 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled; 1721 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled; 1722 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled; 1723 1724 if (!ethData.hostname.empty()) 1725 { 1726 jsonResponse["HostName"] = ethData.hostname; 1727 1728 // When domain name is empty then it means, that it is a network 1729 // without domain names, and the host name itself must be treated as 1730 // FQDN 1731 std::string fqdn = ethData.hostname; 1732 if (!ethData.domainnames.empty()) 1733 { 1734 fqdn += "." + ethData.domainnames[0]; 1735 } 1736 jsonResponse["FQDN"] = fqdn; 1737 } 1738 1739 jsonResponse["VLANs"] = { 1740 {"@odata.id", 1741 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}}; 1742 1743 jsonResponse["NameServers"] = ethData.nameServers; 1744 jsonResponse["StaticNameServers"] = ethData.staticNameServers; 1745 1746 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 1747 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 1748 ipv4Array = nlohmann::json::array(); 1749 ipv4StaticArray = nlohmann::json::array(); 1750 for (auto& ipv4Config : ipv4Data) 1751 { 1752 1753 std::string gatewayStr = ipv4Config.gateway; 1754 if (gatewayStr.empty()) 1755 { 1756 gatewayStr = "0.0.0.0"; 1757 } 1758 1759 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin}, 1760 {"SubnetMask", ipv4Config.netmask}, 1761 {"Address", ipv4Config.address}, 1762 {"Gateway", gatewayStr}}); 1763 if (ipv4Config.origin == "Static") 1764 { 1765 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin}, 1766 {"SubnetMask", ipv4Config.netmask}, 1767 {"Address", ipv4Config.address}, 1768 {"Gateway", gatewayStr}}); 1769 } 1770 } 1771 1772 std::string ipv6GatewayStr = ethData.ipv6_default_gateway; 1773 if (ipv6GatewayStr.empty()) 1774 { 1775 ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 1776 } 1777 1778 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1779 1780 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 1781 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 1782 ipv6Array = nlohmann::json::array(); 1783 ipv6StaticArray = nlohmann::json::array(); 1784 nlohmann::json& ipv6AddrPolicyTable = 1785 jsonResponse["IPv6AddressPolicyTable"]; 1786 ipv6AddrPolicyTable = nlohmann::json::array(); 1787 for (auto& ipv6Config : ipv6Data) 1788 { 1789 ipv6Array.push_back({{"Address", ipv6Config.address}, 1790 {"PrefixLength", ipv6Config.prefixLength}, 1791 {"AddressOrigin", ipv6Config.origin}, 1792 {"AddressState", nullptr}}); 1793 if (ipv6Config.origin == "Static") 1794 { 1795 ipv6StaticArray.push_back( 1796 {{"Address", ipv6Config.address}, 1797 {"PrefixLength", ipv6Config.prefixLength}, 1798 {"AddressOrigin", ipv6Config.origin}, 1799 {"AddressState", nullptr}}); 1800 } 1801 } 1802 } 1803 1804 void parseInterfaceData(nlohmann::json& jsonResponse, 1805 const std::string& parentIfaceId, 1806 const std::string& ifaceId, 1807 const EthernetInterfaceData& ethData) 1808 { 1809 // Fill out obvious data... 1810 jsonResponse["Id"] = ifaceId; 1811 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1812 parentIfaceId + "/VLANs/" + ifaceId; 1813 1814 jsonResponse["VLANEnable"] = true; 1815 if (!ethData.vlan_id.empty()) 1816 { 1817 jsonResponse["VLANId"] = ethData.vlan_id.back(); 1818 } 1819 } 1820 1821 bool verifyNames(const std::string& parent, const std::string& iface) 1822 { 1823 if (!boost::starts_with(iface, parent + "_")) 1824 { 1825 return false; 1826 } 1827 return true; 1828 } 1829 1830 inline void requestEthernetInterfacesRoutes(App& app) 1831 { 1832 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1833 .privileges({"Login"}) 1834 .methods( 1835 boost::beast::http::verb:: 1836 get)([](const crow::Request&, 1837 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1838 asyncResp->res.jsonValue["@odata.type"] = 1839 "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1840 asyncResp->res.jsonValue["@odata.id"] = 1841 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1842 asyncResp->res.jsonValue["Name"] = 1843 "Ethernet Network Interface Collection"; 1844 asyncResp->res.jsonValue["Description"] = 1845 "Collection of EthernetInterfaces for this Manager"; 1846 1847 // Get eth interface list, and call the below callback for JSON 1848 // preparation 1849 getEthernetIfaceList([asyncResp](const bool& success, 1850 const boost::container::flat_set< 1851 std::string>& ifaceList) { 1852 if (!success) 1853 { 1854 messages::internalError(asyncResp->res); 1855 return; 1856 } 1857 1858 nlohmann::json& ifaceArray = 1859 asyncResp->res.jsonValue["Members"]; 1860 ifaceArray = nlohmann::json::array(); 1861 std::string tag = "_"; 1862 for (const std::string& ifaceItem : ifaceList) 1863 { 1864 std::size_t found = ifaceItem.find(tag); 1865 if (found == std::string::npos) 1866 { 1867 ifaceArray.push_back( 1868 {{"@odata.id", 1869 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1870 ifaceItem}}); 1871 } 1872 } 1873 1874 asyncResp->res.jsonValue["Members@odata.count"] = 1875 ifaceArray.size(); 1876 asyncResp->res.jsonValue["@odata.id"] = 1877 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1878 }); 1879 }); 1880 1881 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1882 .privileges({"Login"}) 1883 .methods(boost::beast::http::verb::get)( 1884 [](const crow::Request&, 1885 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1886 const std::string& ifaceId) { 1887 getEthernetIfaceData( 1888 ifaceId, 1889 [asyncResp, 1890 ifaceId](const bool& success, 1891 const EthernetInterfaceData& ethData, 1892 const boost::container::flat_set<IPv4AddressData>& 1893 ipv4Data, 1894 const boost::container::flat_set<IPv6AddressData>& 1895 ipv6Data) { 1896 if (!success) 1897 { 1898 // TODO(Pawel)consider distinguish between non 1899 // existing object, and other errors 1900 messages::resourceNotFound( 1901 asyncResp->res, "EthernetInterface", ifaceId); 1902 return; 1903 } 1904 1905 asyncResp->res.jsonValue["@odata.type"] = 1906 "#EthernetInterface.v1_4_1.EthernetInterface"; 1907 asyncResp->res.jsonValue["Name"] = 1908 "Manager Ethernet Interface"; 1909 asyncResp->res.jsonValue["Description"] = 1910 "Management Network Interface"; 1911 1912 parseInterfaceData(asyncResp, ifaceId, ethData, 1913 ipv4Data, ipv6Data); 1914 }); 1915 }); 1916 1917 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1918 .privileges({"ConfigureComponents"}) 1919 .methods(boost::beast::http::verb::patch)( 1920 [](const crow::Request& req, 1921 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1922 const std::string& ifaceId) { 1923 std::optional<std::string> hostname; 1924 std::optional<std::string> fqdn; 1925 std::optional<std::string> macAddress; 1926 std::optional<std::string> ipv6DefaultGateway; 1927 std::optional<nlohmann::json> ipv4StaticAddresses; 1928 std::optional<nlohmann::json> ipv6StaticAddresses; 1929 std::optional<std::vector<std::string>> staticNameServers; 1930 std::optional<nlohmann::json> dhcpv4; 1931 std::optional<nlohmann::json> dhcpv6; 1932 std::optional<bool> interfaceEnabled; 1933 DHCPParameters v4dhcpParms; 1934 DHCPParameters v6dhcpParms; 1935 1936 if (!json_util::readJson( 1937 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1938 "IPv4StaticAddresses", ipv4StaticAddresses, 1939 "MACAddress", macAddress, "StaticNameServers", 1940 staticNameServers, "IPv6DefaultGateway", 1941 ipv6DefaultGateway, "IPv6StaticAddresses", 1942 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1943 "InterfaceEnabled", interfaceEnabled)) 1944 { 1945 return; 1946 } 1947 if (dhcpv4) 1948 { 1949 if (!json_util::readJson( 1950 *dhcpv4, asyncResp->res, "DHCPEnabled", 1951 v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 1952 v4dhcpParms.useDNSServers, "UseNTPServers", 1953 v4dhcpParms.useNTPServers, "UseDomainName", 1954 v4dhcpParms.useUseDomainName)) 1955 { 1956 return; 1957 } 1958 } 1959 1960 if (dhcpv6) 1961 { 1962 if (!json_util::readJson( 1963 *dhcpv6, asyncResp->res, "OperatingMode", 1964 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers", 1965 v6dhcpParms.useDNSServers, "UseNTPServers", 1966 v6dhcpParms.useNTPServers, "UseDomainName", 1967 v6dhcpParms.useUseDomainName)) 1968 { 1969 return; 1970 } 1971 } 1972 1973 // Get single eth interface data, and call the below callback 1974 // for JSON preparation 1975 getEthernetIfaceData( 1976 ifaceId, 1977 [asyncResp, ifaceId, hostname = std::move(hostname), 1978 fqdn = std::move(fqdn), macAddress = std::move(macAddress), 1979 ipv4StaticAddresses = std::move(ipv4StaticAddresses), 1980 ipv6DefaultGateway = std::move(ipv6DefaultGateway), 1981 ipv6StaticAddresses = std::move(ipv6StaticAddresses), 1982 staticNameServers = std::move(staticNameServers), 1983 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), 1984 v4dhcpParms = std::move(v4dhcpParms), 1985 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 1986 const bool& success, 1987 const EthernetInterfaceData& ethData, 1988 const boost::container::flat_set<IPv4AddressData>& 1989 ipv4Data, 1990 const boost::container::flat_set<IPv6AddressData>& 1991 ipv6Data) { 1992 if (!success) 1993 { 1994 // ... otherwise return error 1995 // TODO(Pawel)consider distinguish between non 1996 // existing object, and other errors 1997 messages::resourceNotFound( 1998 asyncResp->res, "Ethernet Interface", ifaceId); 1999 return; 2000 } 2001 2002 if (dhcpv4 || dhcpv6) 2003 { 2004 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, 2005 v6dhcpParms, asyncResp); 2006 } 2007 2008 if (hostname) 2009 { 2010 handleHostnamePatch(*hostname, asyncResp); 2011 } 2012 2013 if (fqdn) 2014 { 2015 handleFqdnPatch(ifaceId, *fqdn, asyncResp); 2016 } 2017 2018 if (macAddress) 2019 { 2020 handleMACAddressPatch(ifaceId, *macAddress, 2021 asyncResp); 2022 } 2023 2024 if (ipv4StaticAddresses) 2025 { 2026 // TODO(ed) for some reason the capture of 2027 // ipv4Addresses above is returning a const value, 2028 // not a non-const value. This doesn't really work 2029 // for us, as we need to be able to efficiently move 2030 // out the intermedia nlohmann::json objects. This 2031 // makes a copy of the structure, and operates on 2032 // that, but could be done more efficiently 2033 nlohmann::json ipv4Static = *ipv4StaticAddresses; 2034 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, 2035 asyncResp); 2036 } 2037 2038 if (staticNameServers) 2039 { 2040 handleStaticNameServersPatch( 2041 ifaceId, *staticNameServers, asyncResp); 2042 } 2043 2044 if (ipv6DefaultGateway) 2045 { 2046 messages::propertyNotWritable(asyncResp->res, 2047 "IPv6DefaultGateway"); 2048 } 2049 2050 if (ipv6StaticAddresses) 2051 { 2052 nlohmann::json ipv6Static = *ipv6StaticAddresses; 2053 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, 2054 ipv6Data, asyncResp); 2055 } 2056 2057 if (interfaceEnabled) 2058 { 2059 setEthernetInterfaceBoolProperty( 2060 ifaceId, "NICEnabled", *interfaceEnabled, 2061 asyncResp); 2062 } 2063 }); 2064 }); 2065 2066 BMCWEB_ROUTE( 2067 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2068 .privileges({"Login"}) 2069 .methods(boost::beast::http::verb::get)( 2070 [](const crow::Request& /* req */, 2071 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2072 const std::string& parentIfaceId, const std::string& ifaceId) { 2073 asyncResp->res.jsonValue["@odata.type"] = 2074 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 2075 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 2076 2077 if (!verifyNames(parentIfaceId, ifaceId)) 2078 { 2079 return; 2080 } 2081 2082 // Get single eth interface data, and call the below callback 2083 // for JSON preparation 2084 getEthernetIfaceData( 2085 ifaceId, 2086 [asyncResp, parentIfaceId, ifaceId]( 2087 const bool& success, 2088 const EthernetInterfaceData& ethData, 2089 const boost::container::flat_set<IPv4AddressData>&, 2090 const boost::container::flat_set<IPv6AddressData>&) { 2091 if (success && ethData.vlan_id.size() != 0) 2092 { 2093 parseInterfaceData(asyncResp->res.jsonValue, 2094 parentIfaceId, ifaceId, ethData); 2095 } 2096 else 2097 { 2098 // ... otherwise return error 2099 // TODO(Pawel)consider distinguish between non 2100 // existing object, and other errors 2101 messages::resourceNotFound(asyncResp->res, 2102 "VLAN Network Interface", 2103 ifaceId); 2104 } 2105 }); 2106 }); 2107 2108 BMCWEB_ROUTE( 2109 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2110 .privileges({"ConfigureComponents"}) 2111 .methods(boost::beast::http::verb::patch)( 2112 [](const crow::Request& req, 2113 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2114 const std::string& parentIfaceId, const std::string& ifaceId) { 2115 if (!verifyNames(parentIfaceId, ifaceId)) 2116 { 2117 messages::resourceNotFound( 2118 asyncResp->res, "VLAN Network Interface", ifaceId); 2119 return; 2120 } 2121 2122 bool vlanEnable = false; 2123 uint32_t vlanId = 0; 2124 2125 if (!json_util::readJson(req, asyncResp->res, "VLANEnable", 2126 vlanEnable, "VLANId", vlanId)) 2127 { 2128 return; 2129 } 2130 2131 // Get single eth interface data, and call the below callback 2132 // for JSON preparation 2133 getEthernetIfaceData( 2134 ifaceId, 2135 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId]( 2136 const bool& success, 2137 const EthernetInterfaceData& ethData, 2138 const boost::container::flat_set<IPv4AddressData>&, 2139 const boost::container::flat_set<IPv6AddressData>&) { 2140 if (success && !ethData.vlan_id.empty()) 2141 { 2142 auto callback = 2143 [asyncResp]( 2144 const boost::system::error_code ec) { 2145 if (ec) 2146 { 2147 messages::internalError(asyncResp->res); 2148 } 2149 }; 2150 2151 if (vlanEnable == true) 2152 { 2153 crow::connections::systemBus->async_method_call( 2154 std::move(callback), 2155 "xyz.openbmc_project.Network", 2156 "/xyz/openbmc_project/network/" + ifaceId, 2157 "org.freedesktop.DBus.Properties", "Set", 2158 "xyz.openbmc_project.Network.VLAN", "Id", 2159 std::variant<uint32_t>(vlanId)); 2160 } 2161 else 2162 { 2163 BMCWEB_LOG_DEBUG 2164 << "vlanEnable is false. Deleting the " 2165 "vlan interface"; 2166 crow::connections::systemBus->async_method_call( 2167 std::move(callback), 2168 "xyz.openbmc_project.Network", 2169 std::string( 2170 "/xyz/openbmc_project/network/") + 2171 ifaceId, 2172 "xyz.openbmc_project.Object.Delete", 2173 "Delete"); 2174 } 2175 } 2176 else 2177 { 2178 // TODO(Pawel)consider distinguish between non 2179 // existing object, and other errors 2180 messages::resourceNotFound(asyncResp->res, 2181 "VLAN Network Interface", 2182 ifaceId); 2183 return; 2184 } 2185 }); 2186 }); 2187 2188 BMCWEB_ROUTE( 2189 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2190 .privileges({"ConfigureComponents"}) 2191 .methods(boost::beast::http::verb::delete_)( 2192 [](const crow::Request& /* req */, 2193 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2194 const std::string& parentIfaceId, const std::string& ifaceId) { 2195 if (!verifyNames(parentIfaceId, ifaceId)) 2196 { 2197 messages::resourceNotFound( 2198 asyncResp->res, "VLAN Network Interface", ifaceId); 2199 return; 2200 } 2201 2202 // Get single eth interface data, and call the below callback 2203 // for JSON preparation 2204 getEthernetIfaceData( 2205 ifaceId, 2206 [asyncResp, parentIfaceId, ifaceId]( 2207 const bool& success, 2208 const EthernetInterfaceData& ethData, 2209 const boost::container::flat_set<IPv4AddressData>&, 2210 const boost::container::flat_set<IPv6AddressData>&) { 2211 if (success && !ethData.vlan_id.empty()) 2212 { 2213 auto callback = 2214 [asyncResp]( 2215 const boost::system::error_code ec) { 2216 if (ec) 2217 { 2218 messages::internalError(asyncResp->res); 2219 } 2220 }; 2221 crow::connections::systemBus->async_method_call( 2222 std::move(callback), 2223 "xyz.openbmc_project.Network", 2224 std::string("/xyz/openbmc_project/network/") + 2225 ifaceId, 2226 "xyz.openbmc_project.Object.Delete", "Delete"); 2227 } 2228 else 2229 { 2230 // ... otherwise return error 2231 // TODO(Pawel)consider distinguish between non 2232 // existing object, and other errors 2233 messages::resourceNotFound(asyncResp->res, 2234 "VLAN Network Interface", 2235 ifaceId); 2236 } 2237 }); 2238 }); 2239 2240 BMCWEB_ROUTE(app, 2241 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2242 .privileges({"Login"}) 2243 .methods( 2244 boost::beast::http::verb:: 2245 get)([](const crow::Request& /* req */, 2246 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2247 const std::string& rootInterfaceName) { 2248 // Get eth interface list, and call the below callback for JSON 2249 // preparation 2250 getEthernetIfaceList([asyncResp, rootInterfaceName]( 2251 const bool& success, 2252 const boost::container::flat_set< 2253 std::string>& ifaceList) { 2254 if (!success) 2255 { 2256 messages::internalError(asyncResp->res); 2257 return; 2258 } 2259 2260 if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 2261 { 2262 messages::resourceNotFound(asyncResp->res, 2263 "VLanNetworkInterfaceCollection", 2264 rootInterfaceName); 2265 return; 2266 } 2267 2268 asyncResp->res.jsonValue["@odata.type"] = 2269 "#VLanNetworkInterfaceCollection." 2270 "VLanNetworkInterfaceCollection"; 2271 asyncResp->res.jsonValue["Name"] = 2272 "VLAN Network Interface Collection"; 2273 2274 nlohmann::json ifaceArray = nlohmann::json::array(); 2275 2276 for (const std::string& ifaceItem : ifaceList) 2277 { 2278 if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) 2279 { 2280 std::string path = 2281 "/redfish/v1/Managers/bmc/EthernetInterfaces/"; 2282 path += rootInterfaceName; 2283 path += "/VLANs/"; 2284 path += ifaceItem; 2285 ifaceArray.push_back({{"@odata.id", std::move(path)}}); 2286 } 2287 } 2288 2289 asyncResp->res.jsonValue["Members@odata.count"] = 2290 ifaceArray.size(); 2291 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 2292 asyncResp->res.jsonValue["@odata.id"] = 2293 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 2294 rootInterfaceName + "/VLANs"; 2295 }); 2296 }); 2297 2298 BMCWEB_ROUTE(app, 2299 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2300 .privileges({"ConfigureComponents"}) 2301 .methods(boost::beast::http::verb::post)( 2302 [](const crow::Request& req, 2303 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2304 const std::string& rootInterfaceName) { 2305 bool vlanEnable = false; 2306 uint32_t vlanId = 0; 2307 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId, 2308 "VLANEnable", vlanEnable)) 2309 { 2310 return; 2311 } 2312 // Need both vlanId and vlanEnable to service this request 2313 if (!vlanId) 2314 { 2315 messages::propertyMissing(asyncResp->res, "VLANId"); 2316 } 2317 if (!vlanEnable) 2318 { 2319 messages::propertyMissing(asyncResp->res, "VLANEnable"); 2320 } 2321 if (static_cast<bool>(vlanId) ^ vlanEnable) 2322 { 2323 return; 2324 } 2325 2326 auto callback = 2327 [asyncResp](const boost::system::error_code ec) { 2328 if (ec) 2329 { 2330 // TODO(ed) make more consistent error messages 2331 // based on phosphor-network responses 2332 messages::internalError(asyncResp->res); 2333 return; 2334 } 2335 messages::created(asyncResp->res); 2336 }; 2337 crow::connections::systemBus->async_method_call( 2338 std::move(callback), "xyz.openbmc_project.Network", 2339 "/xyz/openbmc_project/network", 2340 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 2341 rootInterfaceName, vlanId); 2342 }); 2343 } 2344 2345 } // namespace redfish 2346