blob: a76250306e5ba20fedbb42cbc0bc813cc1b06201 [file] [log] [blame]
Brandon Kimdab96f12021-02-18 11:21:37 -08001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
William A. Kennington III7d6fa422021-02-08 17:04:02 -080015#include "net_config.h"
16
Mo Elbadryd94dc252024-11-07 21:44:23 +000017#include <linux/if.h>
18#include <net/if_arp.h>
19#include <sys/ioctl.h>
William A. Kennington III7d6fa422021-02-08 17:04:02 -080020#include <sys/types.h>
21#include <sys/wait.h>
22#include <unistd.h>
23
24#include <sdbusplus/bus.hpp>
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -070025#include <stdplus/fd/create.hpp>
26#include <stdplus/fd/ops.hpp>
Willy Tu253e6462023-12-28 09:57:27 -080027#include <stdplus/print.hpp>
Mo Elbadry6461f392024-12-03 21:34:06 +000028#include <stdplus/raw.hpp>
William A. Kennington III7d6fa422021-02-08 17:04:02 -080029#include <stdplus/util/string.hpp>
30
31#include <cstdio>
32#include <cstring>
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -070033#include <filesystem>
Willy Tubb531612023-07-16 01:30:08 -070034#include <format>
Mo Elbadryd94dc252024-11-07 21:44:23 +000035#include <thread>
William A. Kennington III7d6fa422021-02-08 17:04:02 -080036#include <utility>
37#include <variant>
38
39/* Most of the code for interacting with DBus is from
40 * phosphor-host-ipmid/utils.cpp
41 */
42
43namespace net
44{
45
46namespace
47{
48
49constexpr auto IFACE_ROOT = "/xyz/openbmc_project/network/";
50constexpr auto MAC_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
51// 2 chars for every byte + 5 colons + Null byte
52constexpr auto MAC_FORMAT_LENGTH = 6 * 2 + 5 + 1;
53constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
54constexpr auto NETWORK_SERVICE = "xyz.openbmc_project.Network";
55constexpr auto PROP_INTERFACE = "org.freedesktop.DBus.Properties";
56
William A. Kennington III7d6fa422021-02-08 17:04:02 -080057std::string format_mac(const mac_addr_t& mac)
58{
59 // 2 chars for every byte + 5 colons + Null byte
60 char mac_str[MAC_FORMAT_LENGTH];
61 snprintf(mac_str, sizeof(mac_str), MAC_FORMAT, mac.octet[0], mac.octet[1],
62 mac.octet[2], mac.octet[3], mac.octet[4], mac.octet[5]);
63
64 return std::string{mac_str};
65}
66
67} // namespace
68
69PhosphorConfig::PhosphorConfig(const std::string& iface_name) :
70 iface_name_{iface_name}, iface_path_{std::string(IFACE_ROOT) + iface_name},
71 shared_host_mac_(std::experimental::nullopt),
72 bus(sdbusplus::bus::new_default())
73{}
74
William A. Kennington III7d6fa422021-02-08 17:04:02 -080075int PhosphorConfig::get_mac_addr(mac_addr_t* mac)
76{
77 if (mac == nullptr)
78 {
Willy Tu253e6462023-12-28 09:57:27 -080079 stdplus::println(stderr, "mac is nullptr");
William A. Kennington III7d6fa422021-02-08 17:04:02 -080080 return -1;
81 }
82
83 // Cache hit: we have stored host MAC.
84 if (shared_host_mac_)
85 {
86 *mac = shared_host_mac_.value();
87 }
Mo Elbadryd94dc252024-11-07 21:44:23 +000088 else // Cache miss: read from interface, cache it for future requests.
William A. Kennington III7d6fa422021-02-08 17:04:02 -080089 {
Mo Elbadryd94dc252024-11-07 21:44:23 +000090 struct ifreq ifr = {};
William A. Kennington III7d6fa422021-02-08 17:04:02 -080091 try
92 {
Mo Elbadryd94dc252024-11-07 21:44:23 +000093 auto fd = stdplus::fd::socket(stdplus::fd::SocketDomain::INet6,
94 stdplus::fd::SocketType::Datagram,
95 stdplus::fd::SocketProto::IP);
96 call_nic(fd, ifr, SIOCGIFHWADDR);
William A. Kennington III7d6fa422021-02-08 17:04:02 -080097 }
Mo Elbadryd94dc252024-11-07 21:44:23 +000098 catch (const std::exception& ex)
William A. Kennington III7d6fa422021-02-08 17:04:02 -080099 {
Mo Elbadryd94dc252024-11-07 21:44:23 +0000100 stdplus::println(
101 stderr,
102 "Failed to get MAC Addr for Interface {} writing file: {}",
103 iface_name_, ex.what());
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800104 return -1;
105 }
Mo Elbadryd94dc252024-11-07 21:44:23 +0000106 std::copy_n(ifr.ifr_addr.sa_data, sizeof(*mac), mac->octet);
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800107 shared_host_mac_ = *mac;
108 }
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800109 return 0;
110}
111
Mo Elbadryd94dc252024-11-07 21:44:23 +0000112void PhosphorConfig::call_nic(auto fd, struct ifreq& ifr, int op)
113{
114 std::copy_n(iface_name_.c_str(), iface_name_.size() + 1, ifr.ifr_name);
115 fd.ioctl(op, &ifr);
116}
117
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800118int PhosphorConfig::set_mac_addr(const mac_addr_t& mac)
119{
Mo Elbadry6461f392024-12-03 21:34:06 +0000120 std::string mac_value = format_mac(mac);
Mo Elbadryd94dc252024-11-07 21:44:23 +0000121 struct ifreq ifr = {};
122 short flags_copy;
Mo Elbadry6461f392024-12-03 21:34:06 +0000123 mac_addr_t cur_mac;
124 int ret;
125 ret = get_mac_addr(&cur_mac);
126 if (ret == 0)
127 {
128 if (stdplus::raw::equal(cur_mac, mac))
129 {
130 // mac value is the same not doing anything, returning
131 return 0;
132 }
133 }
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800134
135 try
136 {
Willy Tubb531612023-07-16 01:30:08 -0700137 auto netdir = std::format("/run/systemd/network/00-bmc-{}.network.d",
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700138 iface_name_);
139 std::filesystem::create_directories(netdir);
Willy Tubb531612023-07-16 01:30:08 -0700140 auto netfile = std::format("{}/60-ncsi-mac.conf", netdir);
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700141 auto fd = stdplus::fd::open(
142 netfile,
143 stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::WriteOnly)
144 .set(stdplus::fd::OpenFlag::Create),
145 0644);
Mo Elbadry6461f392024-12-03 21:34:06 +0000146 auto contents = std::format("[Link]\nMACAddress={}\n", mac_value);
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700147 stdplus::fd::writeExact(fd, contents);
148 }
149 catch (const std::exception& ex)
150 {
Willy Tu253e6462023-12-28 09:57:27 -0800151 stdplus::println(stderr, "Failed to set MAC Addr `{}` writing file: {}",
Mo Elbadry6461f392024-12-03 21:34:06 +0000152 mac_value, ex.what());
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700153 return -1;
154 }
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700155 try
156 {
Mo Elbadryd94dc252024-11-07 21:44:23 +0000157 auto fd = stdplus::fd::socket(stdplus::fd::SocketDomain::INet6,
158 stdplus::fd::SocketType::Datagram,
159 stdplus::fd::SocketProto::IP);
160 // Try setting MAC Address directly without bringing interface down
161 try
162 {
163 std::copy_n(mac.octet, 6, ifr.ifr_hwaddr.sa_data);
164 call_nic(fd, ifr, SIOCSIFHWADDR);
165 }
166 catch (const std::exception& e)
167 {
168 // Regardless of error attempt to set MAC Address again after
169 // bringing interface down
170 stdplus::println(
171 stderr,
172 "Could not set MAC Address directly, retrying after bringing interface down, error = {}",
173 e.what());
174 try
175 {
176 // Read interface flags configuration and store (once interface
177 // is brought down, existing state is lost)
178 call_nic(fd, ifr, SIOCGIFFLAGS);
179 flags_copy = ifr.ifr_flags;
180 // set interface down
181 ifr.ifr_flags &= ~IFF_UP;
182 call_nic(fd, ifr, SIOCSIFFLAGS);
183 // Wait for 1 milliseconds - sometimes interface is still
184 // going down
185 std::this_thread::sleep_for(std::chrono::milliseconds(1));
186 // set MAC Address
187 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
188 std::copy_n(mac.octet, 6, ifr.ifr_hwaddr.sa_data);
189 call_nic(fd, ifr, SIOCSIFHWADDR);
190 // set interface up with the flags state prior to bringing
191 // it down
192 ifr.ifr_flags = flags_copy | IFF_UP;
193 call_nic(fd, ifr, SIOCSIFFLAGS);
194 }
195 catch (const std::exception& e)
196 {
197 stdplus::println(
198 stderr, "Failed to set MAC Address {} writing file: {}",
Mo Elbadry6461f392024-12-03 21:34:06 +0000199 mac_value, e.what());
Mo Elbadryd94dc252024-11-07 21:44:23 +0000200 return -1;
201 }
202 }
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800203 }
Mo Elbadryd94dc252024-11-07 21:44:23 +0000204 catch (const std::exception& e)
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800205 {
Mo Elbadryd94dc252024-11-07 21:44:23 +0000206 stdplus::println(stderr, "Error creating socket: {}", e.what());
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800207 return -1;
208 }
Mo Elbadryd94dc252024-11-07 21:44:23 +0000209 stdplus::println(stderr, "Success setting Mac address for {}: {}",
Mo Elbadry6461f392024-12-03 21:34:06 +0000210 iface_name_, mac_value);
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800211 shared_host_mac_ = std::experimental::nullopt;
212 return 0;
213}
214
215int PhosphorConfig::set_nic_hostless(bool is_nic_hostless)
216{
217 // Ensure that we don't trigger the target multiple times. This is
218 // undesirable because it will cause any inactive services to re-trigger
219 // every time we run this code. Since the loop calling this executes this
220 // code every 1s, we don't want to keep re-executing services. A fresh
221 // start of the daemon will always trigger the service to ensure system
222 // consistency.
223 if (was_nic_hostless_ && is_nic_hostless == *was_nic_hostless_)
224 {
225 return 0;
226 }
227
228 static constexpr auto systemdService = "org.freedesktop.systemd1";
229 static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
230 static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
231
232 auto method = bus.new_method_call(systemdService, systemdRoot,
233 systemdInterface, "StartUnit");
234 if (is_nic_hostless)
235 {
236 method.append(
237 stdplus::util::strCat("nic-hostless@", iface_name_, ".target"));
238 }
239 else
240 {
241 method.append(
242 stdplus::util::strCat("nic-hostful@", iface_name_, ".target"));
243 }
244
245 // Specify --job-mode (see systemctl(1) for detail).
246 method.append("replace");
247
248 try
249 {
250 bus.call_noreply(method);
251 was_nic_hostless_ = is_nic_hostless;
252 return 0;
253 }
254 catch (const sdbusplus::exception::SdBusError& ex)
255 {
Willy Tu253e6462023-12-28 09:57:27 -0800256 stdplus::println(stderr, "Failed to set systemd nic status: {}",
257 ex.what());
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800258 return 1;
259 }
260}
261
262} // namespace net