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 <fmt/format.h>
181285115cSWilliam A. Kennington III #include <sys/types.h>
191285115cSWilliam A. Kennington III #include <sys/wait.h>
201285115cSWilliam A. Kennington III #include <unistd.h>
211285115cSWilliam A. Kennington III 
221285115cSWilliam A. Kennington III #include <sdbusplus/bus.hpp>
23a5c9d7a4SWilliam A. Kennington III #include <stdplus/fd/create.hpp>
24a5c9d7a4SWilliam A. Kennington III #include <stdplus/fd/ops.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>
301285115cSWilliam A. Kennington III #include <utility>
311285115cSWilliam A. Kennington III #include <variant>
321285115cSWilliam A. Kennington III 
331285115cSWilliam A. Kennington III /* Most of the code for interacting with DBus is from
341285115cSWilliam A. Kennington III  * phosphor-host-ipmid/utils.cpp
351285115cSWilliam A. Kennington III  */
361285115cSWilliam A. Kennington III 
371285115cSWilliam A. Kennington III namespace net
381285115cSWilliam A. Kennington III {
391285115cSWilliam A. Kennington III 
401285115cSWilliam A. Kennington III namespace
411285115cSWilliam A. Kennington III {
421285115cSWilliam A. Kennington III 
431285115cSWilliam A. Kennington III constexpr auto IFACE_ROOT = "/xyz/openbmc_project/network/";
441285115cSWilliam A. Kennington III constexpr auto MAC_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
451285115cSWilliam A. Kennington III // 2 chars for every byte + 5 colons + Null byte
461285115cSWilliam A. Kennington III constexpr auto MAC_FORMAT_LENGTH = 6 * 2 + 5 + 1;
471285115cSWilliam A. Kennington III constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
481285115cSWilliam A. Kennington III constexpr auto NETWORK_SERVICE = "xyz.openbmc_project.Network";
491285115cSWilliam A. Kennington III constexpr auto PROP_INTERFACE = "org.freedesktop.DBus.Properties";
501285115cSWilliam A. Kennington III 
511285115cSWilliam A. Kennington III int parse_mac(const std::string& mac_addr, mac_addr_t* mac)
521285115cSWilliam A. Kennington III {
53*2be45238SPatrick Williams     int ret = sscanf(mac_addr.c_str(), MAC_FORMAT, mac->octet, mac->octet + 1,
54*2be45238SPatrick Williams                      mac->octet + 2, mac->octet + 3, mac->octet + 4,
55*2be45238SPatrick Williams                      mac->octet + 5);
561285115cSWilliam A. Kennington III 
571285115cSWilliam A. Kennington III     return ret < 6 ? -1 : 0;
581285115cSWilliam A. Kennington III }
591285115cSWilliam A. Kennington III 
601285115cSWilliam A. Kennington III std::string format_mac(const mac_addr_t& mac)
611285115cSWilliam A. Kennington III {
621285115cSWilliam A. Kennington III     // 2 chars for every byte + 5 colons + Null byte
631285115cSWilliam A. Kennington III     char mac_str[MAC_FORMAT_LENGTH];
641285115cSWilliam A. Kennington III     snprintf(mac_str, sizeof(mac_str), MAC_FORMAT, mac.octet[0], mac.octet[1],
651285115cSWilliam A. Kennington III              mac.octet[2], mac.octet[3], mac.octet[4], mac.octet[5]);
661285115cSWilliam A. Kennington III 
671285115cSWilliam A. Kennington III     return std::string{mac_str};
681285115cSWilliam A. Kennington III }
691285115cSWilliam A. Kennington III 
701285115cSWilliam A. Kennington III } // namespace
711285115cSWilliam A. Kennington III 
721285115cSWilliam A. Kennington III PhosphorConfig::PhosphorConfig(const std::string& iface_name) :
731285115cSWilliam A. Kennington III     iface_name_{iface_name}, iface_path_{std::string(IFACE_ROOT) + iface_name},
741285115cSWilliam A. Kennington III     shared_host_mac_(std::experimental::nullopt),
751285115cSWilliam A. Kennington III     bus(sdbusplus::bus::new_default())
761285115cSWilliam A. Kennington III {}
771285115cSWilliam A. Kennington III 
7859ac2c2dSPatrick Williams sdbusplus::message_t PhosphorConfig::new_networkd_call(sdbusplus::bus_t* dbus,
7959ac2c2dSPatrick Williams                                                        bool get) const
801285115cSWilliam A. Kennington III {
811285115cSWilliam A. Kennington III     auto networkd_call =
821285115cSWilliam A. Kennington III         dbus->new_method_call(NETWORK_SERVICE, iface_path_.c_str(),
831285115cSWilliam A. Kennington III                               PROP_INTERFACE, get ? "Get" : "Set");
841285115cSWilliam A. Kennington III 
851285115cSWilliam A. Kennington III     networkd_call.append(MAC_INTERFACE, "MACAddress");
861285115cSWilliam A. Kennington III 
871285115cSWilliam A. Kennington III     return networkd_call;
881285115cSWilliam A. Kennington III }
891285115cSWilliam A. Kennington III 
901285115cSWilliam A. Kennington III int PhosphorConfig::get_mac_addr(mac_addr_t* mac)
911285115cSWilliam A. Kennington III {
921285115cSWilliam A. Kennington III     if (mac == nullptr)
931285115cSWilliam A. Kennington III     {
941285115cSWilliam A. Kennington III         fmt::print(stderr, "mac is nullptr\n");
951285115cSWilliam A. Kennington III         return -1;
961285115cSWilliam A. Kennington III     }
971285115cSWilliam A. Kennington III 
981285115cSWilliam A. Kennington III     // Cache hit: we have stored host MAC.
991285115cSWilliam A. Kennington III     if (shared_host_mac_)
1001285115cSWilliam A. Kennington III     {
1011285115cSWilliam A. Kennington III         *mac = shared_host_mac_.value();
1021285115cSWilliam A. Kennington III     }
1031285115cSWilliam A. Kennington III     else // Cache miss: read MAC over DBus, and store in cache.
1041285115cSWilliam A. Kennington III     {
1051285115cSWilliam A. Kennington III         std::string mac_string;
1061285115cSWilliam A. Kennington III         try
1071285115cSWilliam A. Kennington III         {
1081285115cSWilliam A. Kennington III             auto networkd_call = new_networkd_call(&bus, true);
1091285115cSWilliam A. Kennington III             auto reply = bus.call(networkd_call);
1101285115cSWilliam A. Kennington III             std::variant<std::string> result;
1111285115cSWilliam A. Kennington III             reply.read(result);
1121285115cSWilliam A. Kennington III             mac_string = std::get<std::string>(result);
1131285115cSWilliam A. Kennington III         }
1141285115cSWilliam A. Kennington III         catch (const sdbusplus::exception::SdBusError& ex)
1151285115cSWilliam A. Kennington III         {
1161285115cSWilliam A. Kennington III             fmt::print(stderr, "Failed to get MACAddress: {}\n", ex.what());
1171285115cSWilliam A. Kennington III             return -1;
1181285115cSWilliam A. Kennington III         }
1191285115cSWilliam A. Kennington III 
1201285115cSWilliam A. Kennington III         if (parse_mac(mac_string, mac) < 0)
1211285115cSWilliam A. Kennington III         {
1221285115cSWilliam A. Kennington III             fmt::print(stderr, "Failed to parse MAC Address `{}`\n",
1231285115cSWilliam A. Kennington III                        mac_string);
1241285115cSWilliam A. Kennington III             return -1;
1251285115cSWilliam A. Kennington III         }
1261285115cSWilliam A. Kennington III 
1271285115cSWilliam A. Kennington III         shared_host_mac_ = *mac;
1281285115cSWilliam A. Kennington III     }
1291285115cSWilliam A. Kennington III 
1301285115cSWilliam A. Kennington III     return 0;
1311285115cSWilliam A. Kennington III }
1321285115cSWilliam A. Kennington III 
1331285115cSWilliam A. Kennington III int PhosphorConfig::set_mac_addr(const mac_addr_t& mac)
1341285115cSWilliam A. Kennington III {
1351285115cSWilliam A. Kennington III     auto networkd_call = new_networkd_call(&bus, false);
1361285115cSWilliam A. Kennington III     std::variant<std::string> mac_value(format_mac(mac));
1371285115cSWilliam A. Kennington III     networkd_call.append(mac_value);
1381285115cSWilliam A. Kennington III 
1391285115cSWilliam A. Kennington III     try
1401285115cSWilliam A. Kennington III     {
141a5c9d7a4SWilliam A. Kennington III         auto netdir = fmt::format("/run/systemd/network/00-bmc-{}.network.d",
142a5c9d7a4SWilliam A. Kennington III                                   iface_name_);
143a5c9d7a4SWilliam A. Kennington III         std::filesystem::create_directories(netdir);
144a5c9d7a4SWilliam A. Kennington III         auto netfile = fmt::format("{}/60-ncsi-mac.conf", netdir);
145a5c9d7a4SWilliam A. Kennington III         auto fd = stdplus::fd::open(
146a5c9d7a4SWilliam A. Kennington III             netfile,
147a5c9d7a4SWilliam A. Kennington III             stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::WriteOnly)
148a5c9d7a4SWilliam A. Kennington III                 .set(stdplus::fd::OpenFlag::Create),
149a5c9d7a4SWilliam A. Kennington III             0644);
150a5c9d7a4SWilliam A. Kennington III         auto contents = fmt::format("[Link]\nMACAddress={}\n",
151a5c9d7a4SWilliam A. Kennington III                                     std::get<std::string>(mac_value));
152a5c9d7a4SWilliam A. Kennington III         stdplus::fd::writeExact(fd, contents);
153a5c9d7a4SWilliam A. Kennington III     }
154a5c9d7a4SWilliam A. Kennington III     catch (const std::exception& ex)
155a5c9d7a4SWilliam A. Kennington III     {
156a5c9d7a4SWilliam A. Kennington III         fmt::print(stderr, "Failed to set MAC Addr `{}` writing file: {}\n",
157a5c9d7a4SWilliam A. Kennington III                    std::get<std::string>(mac_value), ex.what());
158a5c9d7a4SWilliam A. Kennington III         return -1;
159a5c9d7a4SWilliam A. Kennington III     }
160a5c9d7a4SWilliam A. Kennington III 
161a5c9d7a4SWilliam A. Kennington III     try
162a5c9d7a4SWilliam A. Kennington III     {
1631285115cSWilliam A. Kennington III         auto reply = bus.call(networkd_call);
1641285115cSWilliam A. Kennington III     }
1651285115cSWilliam A. Kennington III     catch (const sdbusplus::exception::SdBusError& ex)
1661285115cSWilliam A. Kennington III     {
1671285115cSWilliam A. Kennington III         fmt::print(stderr, "Failed to set MAC Addr `{}`: {}\n",
1681285115cSWilliam A. Kennington III                    std::get<std::string>(mac_value), ex.what());
1691285115cSWilliam A. Kennington III         return -1;
1701285115cSWilliam A. Kennington III     }
1711285115cSWilliam A. Kennington III 
1721285115cSWilliam A. Kennington III     shared_host_mac_ = std::experimental::nullopt;
1731285115cSWilliam A. Kennington III     return 0;
1741285115cSWilliam A. Kennington III }
1751285115cSWilliam A. Kennington III 
1761285115cSWilliam A. Kennington III int PhosphorConfig::set_nic_hostless(bool is_nic_hostless)
1771285115cSWilliam A. Kennington III {
1781285115cSWilliam A. Kennington III     // Ensure that we don't trigger the target multiple times. This is
1791285115cSWilliam A. Kennington III     // undesirable because it will cause any inactive services to re-trigger
1801285115cSWilliam A. Kennington III     // every time we run this code. Since the loop calling this executes this
1811285115cSWilliam A. Kennington III     // code every 1s, we don't want to keep re-executing services. A fresh
1821285115cSWilliam A. Kennington III     // start of the daemon will always trigger the service to ensure system
1831285115cSWilliam A. Kennington III     // consistency.
1841285115cSWilliam A. Kennington III     if (was_nic_hostless_ && is_nic_hostless == *was_nic_hostless_)
1851285115cSWilliam A. Kennington III     {
1861285115cSWilliam A. Kennington III         return 0;
1871285115cSWilliam A. Kennington III     }
1881285115cSWilliam A. Kennington III 
1891285115cSWilliam A. Kennington III     static constexpr auto systemdService = "org.freedesktop.systemd1";
1901285115cSWilliam A. Kennington III     static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
1911285115cSWilliam A. Kennington III     static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
1921285115cSWilliam A. Kennington III 
1931285115cSWilliam A. Kennington III     auto method = bus.new_method_call(systemdService, systemdRoot,
1941285115cSWilliam A. Kennington III                                       systemdInterface, "StartUnit");
1951285115cSWilliam A. Kennington III     if (is_nic_hostless)
1961285115cSWilliam A. Kennington III     {
1971285115cSWilliam A. Kennington III         method.append(
1981285115cSWilliam A. Kennington III             stdplus::util::strCat("nic-hostless@", iface_name_, ".target"));
1991285115cSWilliam A. Kennington III     }
2001285115cSWilliam A. Kennington III     else
2011285115cSWilliam A. Kennington III     {
2021285115cSWilliam A. Kennington III         method.append(
2031285115cSWilliam A. Kennington III             stdplus::util::strCat("nic-hostful@", iface_name_, ".target"));
2041285115cSWilliam A. Kennington III     }
2051285115cSWilliam A. Kennington III 
2061285115cSWilliam A. Kennington III     // Specify --job-mode (see systemctl(1) for detail).
2071285115cSWilliam A. Kennington III     method.append("replace");
2081285115cSWilliam A. Kennington III 
2091285115cSWilliam A. Kennington III     try
2101285115cSWilliam A. Kennington III     {
2111285115cSWilliam A. Kennington III         bus.call_noreply(method);
2121285115cSWilliam A. Kennington III         was_nic_hostless_ = is_nic_hostless;
2131285115cSWilliam A. Kennington III         return 0;
2141285115cSWilliam A. Kennington III     }
2151285115cSWilliam A. Kennington III     catch (const sdbusplus::exception::SdBusError& ex)
2161285115cSWilliam A. Kennington III     {
2171285115cSWilliam A. Kennington III         fmt::print(stderr, "Failed to set systemd nic status: {}\n", ex.what());
2181285115cSWilliam A. Kennington III         return 1;
2191285115cSWilliam A. Kennington III     }
2201285115cSWilliam A. Kennington III }
2211285115cSWilliam A. Kennington III 
2221285115cSWilliam A. Kennington III } // namespace net
223