11285115cSWilliam A. Kennington III // Copyright 2021 Google LLC
21285115cSWilliam A. Kennington III //
31285115cSWilliam A. Kennington III // Licensed under the Apache License, Version 2.0 (the "License");
41285115cSWilliam A. Kennington III // you may not use this file except in compliance with the License.
51285115cSWilliam A. Kennington III // You may obtain a copy of the License at
61285115cSWilliam A. Kennington III //
71285115cSWilliam A. Kennington III //      http://www.apache.org/licenses/LICENSE-2.0
81285115cSWilliam A. Kennington III //
91285115cSWilliam A. Kennington III // Unless required by applicable law or agreed to in writing, software
101285115cSWilliam A. Kennington III // distributed under the License is distributed on an "AS IS" BASIS,
111285115cSWilliam A. Kennington III // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121285115cSWilliam A. Kennington III // See the License for the specific language governing permissions and
131285115cSWilliam A. Kennington III // limitations under the License.
141285115cSWilliam A. Kennington III 
151285115cSWilliam A. Kennington III #include "net_config.h"
161285115cSWilliam A. Kennington III 
171285115cSWilliam A. Kennington III #include <sys/types.h>
181285115cSWilliam A. Kennington III #include <sys/wait.h>
191285115cSWilliam A. Kennington III #include <unistd.h>
201285115cSWilliam A. Kennington III 
211285115cSWilliam A. Kennington III #include <sdbusplus/bus.hpp>
22a5c9d7a4SWilliam A. Kennington III #include <stdplus/fd/create.hpp>
23a5c9d7a4SWilliam A. Kennington III #include <stdplus/fd/ops.hpp>
24253e6463SWilly Tu #include <stdplus/print.hpp>
251285115cSWilliam A. Kennington III #include <stdplus/util/string.hpp>
261285115cSWilliam A. Kennington III 
271285115cSWilliam A. Kennington III #include <cstdio>
281285115cSWilliam A. Kennington III #include <cstring>
29a5c9d7a4SWilliam A. Kennington III #include <filesystem>
30bb53161dSWilly Tu #include <format>
311285115cSWilliam A. Kennington III #include <utility>
321285115cSWilliam A. Kennington III #include <variant>
331285115cSWilliam A. Kennington III 
341285115cSWilliam A. Kennington III /* Most of the code for interacting with DBus is from
351285115cSWilliam A. Kennington III  * phosphor-host-ipmid/utils.cpp
361285115cSWilliam A. Kennington III  */
371285115cSWilliam A. Kennington III 
381285115cSWilliam A. Kennington III namespace net
391285115cSWilliam A. Kennington III {
401285115cSWilliam A. Kennington III 
411285115cSWilliam A. Kennington III namespace
421285115cSWilliam A. Kennington III {
431285115cSWilliam A. Kennington III 
441285115cSWilliam A. Kennington III constexpr auto IFACE_ROOT = "/xyz/openbmc_project/network/";
451285115cSWilliam A. Kennington III constexpr auto MAC_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
461285115cSWilliam A. Kennington III // 2 chars for every byte + 5 colons + Null byte
471285115cSWilliam A. Kennington III constexpr auto MAC_FORMAT_LENGTH = 6 * 2 + 5 + 1;
481285115cSWilliam A. Kennington III constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
491285115cSWilliam A. Kennington III constexpr auto NETWORK_SERVICE = "xyz.openbmc_project.Network";
501285115cSWilliam A. Kennington III constexpr auto PROP_INTERFACE = "org.freedesktop.DBus.Properties";
511285115cSWilliam A. Kennington III 
parse_mac(const std::string & mac_addr,mac_addr_t * mac)521285115cSWilliam A. Kennington III int parse_mac(const std::string& mac_addr, mac_addr_t* mac)
531285115cSWilliam A. Kennington III {
542be45238SPatrick Williams     int ret = sscanf(mac_addr.c_str(), MAC_FORMAT, mac->octet, mac->octet + 1,
552be45238SPatrick Williams                      mac->octet + 2, mac->octet + 3, mac->octet + 4,
562be45238SPatrick Williams                      mac->octet + 5);
571285115cSWilliam A. Kennington III 
581285115cSWilliam A. Kennington III     return ret < 6 ? -1 : 0;
591285115cSWilliam A. Kennington III }
601285115cSWilliam A. Kennington III 
format_mac(const mac_addr_t & mac)611285115cSWilliam A. Kennington III std::string format_mac(const mac_addr_t& mac)
621285115cSWilliam A. Kennington III {
631285115cSWilliam A. Kennington III     // 2 chars for every byte + 5 colons + Null byte
641285115cSWilliam A. Kennington III     char mac_str[MAC_FORMAT_LENGTH];
651285115cSWilliam A. Kennington III     snprintf(mac_str, sizeof(mac_str), MAC_FORMAT, mac.octet[0], mac.octet[1],
661285115cSWilliam A. Kennington III              mac.octet[2], mac.octet[3], mac.octet[4], mac.octet[5]);
671285115cSWilliam A. Kennington III 
681285115cSWilliam A. Kennington III     return std::string{mac_str};
691285115cSWilliam A. Kennington III }
701285115cSWilliam A. Kennington III 
711285115cSWilliam A. Kennington III } // namespace
721285115cSWilliam A. Kennington III 
PhosphorConfig(const std::string & iface_name)731285115cSWilliam A. Kennington III PhosphorConfig::PhosphorConfig(const std::string& iface_name) :
741285115cSWilliam A. Kennington III     iface_name_{iface_name}, iface_path_{std::string(IFACE_ROOT) + iface_name},
751285115cSWilliam A. Kennington III     shared_host_mac_(std::experimental::nullopt),
761285115cSWilliam A. Kennington III     bus(sdbusplus::bus::new_default())
771285115cSWilliam A. Kennington III {}
781285115cSWilliam A. Kennington III 
79*c66ebc35SPatrick Williams sdbusplus::message_t
new_networkd_call(sdbusplus::bus_t * dbus,bool get) const80*c66ebc35SPatrick Williams     PhosphorConfig::new_networkd_call(sdbusplus::bus_t* dbus, bool get) const
811285115cSWilliam A. Kennington III {
821285115cSWilliam A. Kennington III     auto networkd_call =
831285115cSWilliam A. Kennington III         dbus->new_method_call(NETWORK_SERVICE, iface_path_.c_str(),
841285115cSWilliam A. Kennington III                               PROP_INTERFACE, get ? "Get" : "Set");
851285115cSWilliam A. Kennington III 
861285115cSWilliam A. Kennington III     networkd_call.append(MAC_INTERFACE, "MACAddress");
871285115cSWilliam A. Kennington III 
881285115cSWilliam A. Kennington III     return networkd_call;
891285115cSWilliam A. Kennington III }
901285115cSWilliam A. Kennington III 
get_mac_addr(mac_addr_t * mac)911285115cSWilliam A. Kennington III int PhosphorConfig::get_mac_addr(mac_addr_t* mac)
921285115cSWilliam A. Kennington III {
931285115cSWilliam A. Kennington III     if (mac == nullptr)
941285115cSWilliam A. Kennington III     {
95253e6463SWilly Tu         stdplus::println(stderr, "mac is nullptr");
961285115cSWilliam A. Kennington III         return -1;
971285115cSWilliam A. Kennington III     }
981285115cSWilliam A. Kennington III 
991285115cSWilliam A. Kennington III     // Cache hit: we have stored host MAC.
1001285115cSWilliam A. Kennington III     if (shared_host_mac_)
1011285115cSWilliam A. Kennington III     {
1021285115cSWilliam A. Kennington III         *mac = shared_host_mac_.value();
1031285115cSWilliam A. Kennington III     }
1041285115cSWilliam A. Kennington III     else // Cache miss: read MAC over DBus, and store in cache.
1051285115cSWilliam A. Kennington III     {
1061285115cSWilliam A. Kennington III         std::string mac_string;
1071285115cSWilliam A. Kennington III         try
1081285115cSWilliam A. Kennington III         {
1091285115cSWilliam A. Kennington III             auto networkd_call = new_networkd_call(&bus, true);
1101285115cSWilliam A. Kennington III             auto reply = bus.call(networkd_call);
1111285115cSWilliam A. Kennington III             std::variant<std::string> result;
1121285115cSWilliam A. Kennington III             reply.read(result);
1131285115cSWilliam A. Kennington III             mac_string = std::get<std::string>(result);
1141285115cSWilliam A. Kennington III         }
1151285115cSWilliam A. Kennington III         catch (const sdbusplus::exception::SdBusError& ex)
1161285115cSWilliam A. Kennington III         {
117253e6463SWilly Tu             stdplus::println(stderr, "Failed to get MACAddress: {}", ex.what());
1181285115cSWilliam A. Kennington III             return -1;
1191285115cSWilliam A. Kennington III         }
1201285115cSWilliam A. Kennington III 
1211285115cSWilliam A. Kennington III         if (parse_mac(mac_string, mac) < 0)
1221285115cSWilliam A. Kennington III         {
123253e6463SWilly Tu             stdplus::println(stderr, "Failed to parse MAC Address `{}`",
1241285115cSWilliam A. Kennington III                              mac_string);
1251285115cSWilliam A. Kennington III             return -1;
1261285115cSWilliam A. Kennington III         }
1271285115cSWilliam A. Kennington III 
1281285115cSWilliam A. Kennington III         shared_host_mac_ = *mac;
1291285115cSWilliam A. Kennington III     }
1301285115cSWilliam A. Kennington III 
1311285115cSWilliam A. Kennington III     return 0;
1321285115cSWilliam A. Kennington III }
1331285115cSWilliam A. Kennington III 
set_mac_addr(const mac_addr_t & mac)1341285115cSWilliam A. Kennington III int PhosphorConfig::set_mac_addr(const mac_addr_t& mac)
1351285115cSWilliam A. Kennington III {
1361285115cSWilliam A. Kennington III     auto networkd_call = new_networkd_call(&bus, false);
1371285115cSWilliam A. Kennington III     std::variant<std::string> mac_value(format_mac(mac));
1381285115cSWilliam A. Kennington III     networkd_call.append(mac_value);
1391285115cSWilliam A. Kennington III 
1401285115cSWilliam A. Kennington III     try
1411285115cSWilliam A. Kennington III     {
142bb53161dSWilly Tu         auto netdir = std::format("/run/systemd/network/00-bmc-{}.network.d",
143a5c9d7a4SWilliam A. Kennington III                                   iface_name_);
144a5c9d7a4SWilliam A. Kennington III         std::filesystem::create_directories(netdir);
145bb53161dSWilly Tu         auto netfile = std::format("{}/60-ncsi-mac.conf", netdir);
146a5c9d7a4SWilliam A. Kennington III         auto fd = stdplus::fd::open(
147a5c9d7a4SWilliam A. Kennington III             netfile,
148a5c9d7a4SWilliam A. Kennington III             stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::WriteOnly)
149a5c9d7a4SWilliam A. Kennington III                 .set(stdplus::fd::OpenFlag::Create),
150a5c9d7a4SWilliam A. Kennington III             0644);
151bb53161dSWilly Tu         auto contents = std::format("[Link]\nMACAddress={}\n",
152a5c9d7a4SWilliam A. Kennington III                                     std::get<std::string>(mac_value));
153a5c9d7a4SWilliam A. Kennington III         stdplus::fd::writeExact(fd, contents);
154a5c9d7a4SWilliam A. Kennington III     }
155a5c9d7a4SWilliam A. Kennington III     catch (const std::exception& ex)
156a5c9d7a4SWilliam A. Kennington III     {
157253e6463SWilly Tu         stdplus::println(stderr, "Failed to set MAC Addr `{}` writing file: {}",
158a5c9d7a4SWilliam A. Kennington III                          std::get<std::string>(mac_value), ex.what());
159a5c9d7a4SWilliam A. Kennington III         return -1;
160a5c9d7a4SWilliam A. Kennington III     }
161a5c9d7a4SWilliam A. Kennington III 
162a5c9d7a4SWilliam A. Kennington III     try
163a5c9d7a4SWilliam A. Kennington III     {
1641285115cSWilliam A. Kennington III         auto reply = bus.call(networkd_call);
1651285115cSWilliam A. Kennington III     }
1661285115cSWilliam A. Kennington III     catch (const sdbusplus::exception::SdBusError& ex)
1671285115cSWilliam A. Kennington III     {
168253e6463SWilly Tu         stdplus::println(stderr, "Failed to set MAC Addr `{}`: {}",
1691285115cSWilliam A. Kennington III                          std::get<std::string>(mac_value), ex.what());
1701285115cSWilliam A. Kennington III         return -1;
1711285115cSWilliam A. Kennington III     }
1721285115cSWilliam A. Kennington III 
1731285115cSWilliam A. Kennington III     shared_host_mac_ = std::experimental::nullopt;
1741285115cSWilliam A. Kennington III     return 0;
1751285115cSWilliam A. Kennington III }
1761285115cSWilliam A. Kennington III 
set_nic_hostless(bool is_nic_hostless)1771285115cSWilliam A. Kennington III int PhosphorConfig::set_nic_hostless(bool is_nic_hostless)
1781285115cSWilliam A. Kennington III {
1791285115cSWilliam A. Kennington III     // Ensure that we don't trigger the target multiple times. This is
1801285115cSWilliam A. Kennington III     // undesirable because it will cause any inactive services to re-trigger
1811285115cSWilliam A. Kennington III     // every time we run this code. Since the loop calling this executes this
1821285115cSWilliam A. Kennington III     // code every 1s, we don't want to keep re-executing services. A fresh
1831285115cSWilliam A. Kennington III     // start of the daemon will always trigger the service to ensure system
1841285115cSWilliam A. Kennington III     // consistency.
1851285115cSWilliam A. Kennington III     if (was_nic_hostless_ && is_nic_hostless == *was_nic_hostless_)
1861285115cSWilliam A. Kennington III     {
1871285115cSWilliam A. Kennington III         return 0;
1881285115cSWilliam A. Kennington III     }
1891285115cSWilliam A. Kennington III 
1901285115cSWilliam A. Kennington III     static constexpr auto systemdService = "org.freedesktop.systemd1";
1911285115cSWilliam A. Kennington III     static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
1921285115cSWilliam A. Kennington III     static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
1931285115cSWilliam A. Kennington III 
1941285115cSWilliam A. Kennington III     auto method = bus.new_method_call(systemdService, systemdRoot,
1951285115cSWilliam A. Kennington III                                       systemdInterface, "StartUnit");
1961285115cSWilliam A. Kennington III     if (is_nic_hostless)
1971285115cSWilliam A. Kennington III     {
1981285115cSWilliam A. Kennington III         method.append(
1991285115cSWilliam A. Kennington III             stdplus::util::strCat("nic-hostless@", iface_name_, ".target"));
2001285115cSWilliam A. Kennington III     }
2011285115cSWilliam A. Kennington III     else
2021285115cSWilliam A. Kennington III     {
2031285115cSWilliam A. Kennington III         method.append(
2041285115cSWilliam A. Kennington III             stdplus::util::strCat("nic-hostful@", iface_name_, ".target"));
2051285115cSWilliam A. Kennington III     }
2061285115cSWilliam A. Kennington III 
2071285115cSWilliam A. Kennington III     // Specify --job-mode (see systemctl(1) for detail).
2081285115cSWilliam A. Kennington III     method.append("replace");
2091285115cSWilliam A. Kennington III 
2101285115cSWilliam A. Kennington III     try
2111285115cSWilliam A. Kennington III     {
2121285115cSWilliam A. Kennington III         bus.call_noreply(method);
2131285115cSWilliam A. Kennington III         was_nic_hostless_ = is_nic_hostless;
2141285115cSWilliam A. Kennington III         return 0;
2151285115cSWilliam A. Kennington III     }
2161285115cSWilliam A. Kennington III     catch (const sdbusplus::exception::SdBusError& ex)
2171285115cSWilliam A. Kennington III     {
218253e6463SWilly Tu         stdplus::println(stderr, "Failed to set systemd nic status: {}",
219253e6463SWilly Tu                          ex.what());
2201285115cSWilliam A. Kennington III         return 1;
2211285115cSWilliam A. Kennington III     }
2221285115cSWilliam A. Kennington III }
2231285115cSWilliam A. Kennington III 
2241285115cSWilliam A. Kennington III } // namespace net
225