| // Copyright 2021 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "net_config.h" |
| |
| #include <linux/if.h> |
| #include <net/if_arp.h> |
| #include <sys/ioctl.h> |
| #include <sys/types.h> |
| #include <sys/wait.h> |
| #include <unistd.h> |
| |
| #include <sdbusplus/bus.hpp> |
| #include <stdplus/fd/create.hpp> |
| #include <stdplus/fd/ops.hpp> |
| #include <stdplus/print.hpp> |
| #include <stdplus/util/string.hpp> |
| |
| #include <cstdio> |
| #include <cstring> |
| #include <filesystem> |
| #include <format> |
| #include <thread> |
| #include <utility> |
| #include <variant> |
| |
| /* Most of the code for interacting with DBus is from |
| * phosphor-host-ipmid/utils.cpp |
| */ |
| |
| namespace net |
| { |
| |
| namespace |
| { |
| |
| constexpr auto IFACE_ROOT = "/xyz/openbmc_project/network/"; |
| constexpr auto MAC_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"; |
| // 2 chars for every byte + 5 colons + Null byte |
| constexpr auto MAC_FORMAT_LENGTH = 6 * 2 + 5 + 1; |
| constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress"; |
| constexpr auto NETWORK_SERVICE = "xyz.openbmc_project.Network"; |
| constexpr auto PROP_INTERFACE = "org.freedesktop.DBus.Properties"; |
| |
| std::string format_mac(const mac_addr_t& mac) |
| { |
| // 2 chars for every byte + 5 colons + Null byte |
| char mac_str[MAC_FORMAT_LENGTH]; |
| snprintf(mac_str, sizeof(mac_str), MAC_FORMAT, mac.octet[0], mac.octet[1], |
| mac.octet[2], mac.octet[3], mac.octet[4], mac.octet[5]); |
| |
| return std::string{mac_str}; |
| } |
| |
| } // namespace |
| |
| PhosphorConfig::PhosphorConfig(const std::string& iface_name) : |
| iface_name_{iface_name}, iface_path_{std::string(IFACE_ROOT) + iface_name}, |
| shared_host_mac_(std::experimental::nullopt), |
| bus(sdbusplus::bus::new_default()) |
| {} |
| |
| int PhosphorConfig::get_mac_addr(mac_addr_t* mac) |
| { |
| if (mac == nullptr) |
| { |
| stdplus::println(stderr, "mac is nullptr"); |
| return -1; |
| } |
| |
| // Cache hit: we have stored host MAC. |
| if (shared_host_mac_) |
| { |
| *mac = shared_host_mac_.value(); |
| } |
| else // Cache miss: read from interface, cache it for future requests. |
| { |
| struct ifreq ifr = {}; |
| try |
| { |
| auto fd = stdplus::fd::socket(stdplus::fd::SocketDomain::INet6, |
| stdplus::fd::SocketType::Datagram, |
| stdplus::fd::SocketProto::IP); |
| call_nic(fd, ifr, SIOCGIFHWADDR); |
| } |
| catch (const std::exception& ex) |
| { |
| stdplus::println( |
| stderr, |
| "Failed to get MAC Addr for Interface {} writing file: {}", |
| iface_name_, ex.what()); |
| return -1; |
| } |
| std::copy_n(ifr.ifr_addr.sa_data, sizeof(*mac), mac->octet); |
| shared_host_mac_ = *mac; |
| } |
| return 0; |
| } |
| |
| void PhosphorConfig::call_nic(auto fd, struct ifreq& ifr, int op) |
| { |
| std::copy_n(iface_name_.c_str(), iface_name_.size() + 1, ifr.ifr_name); |
| fd.ioctl(op, &ifr); |
| } |
| |
| int PhosphorConfig::set_mac_addr(const mac_addr_t& mac) |
| { |
| std::variant<std::string> mac_value(format_mac(mac)); |
| struct ifreq ifr = {}; |
| short flags_copy; |
| |
| try |
| { |
| auto netdir = std::format("/run/systemd/network/00-bmc-{}.network.d", |
| iface_name_); |
| std::filesystem::create_directories(netdir); |
| auto netfile = std::format("{}/60-ncsi-mac.conf", netdir); |
| auto fd = stdplus::fd::open( |
| netfile, |
| stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::WriteOnly) |
| .set(stdplus::fd::OpenFlag::Create), |
| 0644); |
| auto contents = std::format("[Link]\nMACAddress={}\n", |
| std::get<std::string>(mac_value)); |
| stdplus::fd::writeExact(fd, contents); |
| } |
| catch (const std::exception& ex) |
| { |
| stdplus::println(stderr, "Failed to set MAC Addr `{}` writing file: {}", |
| std::get<std::string>(mac_value), ex.what()); |
| return -1; |
| } |
| try |
| { |
| auto fd = stdplus::fd::socket(stdplus::fd::SocketDomain::INet6, |
| stdplus::fd::SocketType::Datagram, |
| stdplus::fd::SocketProto::IP); |
| // Try setting MAC Address directly without bringing interface down |
| try |
| { |
| std::copy_n(mac.octet, 6, ifr.ifr_hwaddr.sa_data); |
| call_nic(fd, ifr, SIOCSIFHWADDR); |
| } |
| catch (const std::exception& e) |
| { |
| // Regardless of error attempt to set MAC Address again after |
| // bringing interface down |
| stdplus::println( |
| stderr, |
| "Could not set MAC Address directly, retrying after bringing interface down, error = {}", |
| e.what()); |
| try |
| { |
| // Read interface flags configuration and store (once interface |
| // is brought down, existing state is lost) |
| call_nic(fd, ifr, SIOCGIFFLAGS); |
| flags_copy = ifr.ifr_flags; |
| // set interface down |
| ifr.ifr_flags &= ~IFF_UP; |
| call_nic(fd, ifr, SIOCSIFFLAGS); |
| // Wait for 1 milliseconds - sometimes interface is still |
| // going down |
| std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| // set MAC Address |
| ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; |
| std::copy_n(mac.octet, 6, ifr.ifr_hwaddr.sa_data); |
| call_nic(fd, ifr, SIOCSIFHWADDR); |
| // set interface up with the flags state prior to bringing |
| // it down |
| ifr.ifr_flags = flags_copy | IFF_UP; |
| call_nic(fd, ifr, SIOCSIFFLAGS); |
| } |
| catch (const std::exception& e) |
| { |
| stdplus::println( |
| stderr, "Failed to set MAC Address {} writing file: {}", |
| std::get<std::string>(mac_value), e.what()); |
| return -1; |
| } |
| } |
| } |
| catch (const std::exception& e) |
| { |
| stdplus::println(stderr, "Error creating socket: {}", e.what()); |
| return -1; |
| } |
| stdplus::println(stderr, "Success setting Mac address for {}: {}", |
| iface_name_, std::get<std::string>(mac_value)); |
| shared_host_mac_ = std::experimental::nullopt; |
| return 0; |
| } |
| |
| int PhosphorConfig::set_nic_hostless(bool is_nic_hostless) |
| { |
| // Ensure that we don't trigger the target multiple times. This is |
| // undesirable because it will cause any inactive services to re-trigger |
| // every time we run this code. Since the loop calling this executes this |
| // code every 1s, we don't want to keep re-executing services. A fresh |
| // start of the daemon will always trigger the service to ensure system |
| // consistency. |
| if (was_nic_hostless_ && is_nic_hostless == *was_nic_hostless_) |
| { |
| return 0; |
| } |
| |
| static constexpr auto systemdService = "org.freedesktop.systemd1"; |
| static constexpr auto systemdRoot = "/org/freedesktop/systemd1"; |
| static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
| |
| auto method = bus.new_method_call(systemdService, systemdRoot, |
| systemdInterface, "StartUnit"); |
| if (is_nic_hostless) |
| { |
| method.append( |
| stdplus::util::strCat("nic-hostless@", iface_name_, ".target")); |
| } |
| else |
| { |
| method.append( |
| stdplus::util::strCat("nic-hostful@", iface_name_, ".target")); |
| } |
| |
| // Specify --job-mode (see systemctl(1) for detail). |
| method.append("replace"); |
| |
| try |
| { |
| bus.call_noreply(method); |
| was_nic_hostless_ = is_nic_hostless; |
| return 0; |
| } |
| catch (const sdbusplus::exception::SdBusError& ex) |
| { |
| stdplus::println(stderr, "Failed to set systemd nic status: {}", |
| ex.what()); |
| return 1; |
| } |
| } |
| |
| } // namespace net |