blob: e34fb51b4ed22cd6d151c61a1a6f110ccd47bd2c [file] [log] [blame]
Patrick Venture690a2342020-05-17 11:51:31 -07001#pragma once
2
John Wang8a7236a2021-01-04 15:31:44 +08003#include "app/channel.hpp"
4#include "user_channel/cipher_mgmt.hpp"
5
6#include <arpa/inet.h>
7#include <netinet/ether.h>
8
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05009#include <ipmid/api-types.hpp>
10#include <ipmid/api.hpp>
11#include <ipmid/message.hpp>
12#include <ipmid/message/types.hpp>
13#include <ipmid/types.hpp>
14#include <ipmid/utils.hpp>
15#include <phosphor-logging/elog-errors.hpp>
16#include <phosphor-logging/elog.hpp>
17#include <phosphor-logging/log.hpp>
18#include <sdbusplus/bus.hpp>
19#include <sdbusplus/exception.hpp>
William A. Kennington III7a3831b2023-06-21 01:10:49 -070020#include <stdplus/raw.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050021#include <user_channel/channel_layer.hpp>
22#include <xyz/openbmc_project/Common/error.hpp>
23#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
24#include <xyz/openbmc_project/Network/IP/server.hpp>
25#include <xyz/openbmc_project/Network/Neighbor/server.hpp>
26
John Wang8a7236a2021-01-04 15:31:44 +080027#include <bitset>
28#include <cinttypes>
Patrick Venture690a2342020-05-17 11:51:31 -070029#include <cstdint>
John Wang8a7236a2021-01-04 15:31:44 +080030#include <fstream>
31#include <functional>
John Wang8a7236a2021-01-04 15:31:44 +080032#include <optional>
John Wang8a7236a2021-01-04 15:31:44 +080033#include <string>
34#include <string_view>
John Wang8a7236a2021-01-04 15:31:44 +080035#include <unordered_map>
36#include <unordered_set>
John Wang8a7236a2021-01-04 15:31:44 +080037#include <utility>
38#include <vector>
Patrick Venture690a2342020-05-17 11:51:31 -070039
40namespace ipmi
41{
42namespace transport
43{
44
John Wang8a7236a2021-01-04 15:31:44 +080045// D-Bus Network Daemon definitions
46constexpr auto PATH_ROOT = "/xyz/openbmc_project/network";
John Wang8a7236a2021-01-04 15:31:44 +080047constexpr auto INTF_ETHERNET = "xyz.openbmc_project.Network.EthernetInterface";
48constexpr auto INTF_IP = "xyz.openbmc_project.Network.IP";
49constexpr auto INTF_IP_CREATE = "xyz.openbmc_project.Network.IP.Create";
50constexpr auto INTF_MAC = "xyz.openbmc_project.Network.MACAddress";
51constexpr auto INTF_NEIGHBOR = "xyz.openbmc_project.Network.Neighbor";
52constexpr auto INTF_NEIGHBOR_CREATE_STATIC =
53 "xyz.openbmc_project.Network.Neighbor.CreateStatic";
54constexpr auto INTF_VLAN = "xyz.openbmc_project.Network.VLAN";
55constexpr auto INTF_VLAN_CREATE = "xyz.openbmc_project.Network.VLAN.Create";
56
Patrick Venture690a2342020-05-17 11:51:31 -070057/** @brief IPMI LAN Parameters */
58enum class LanParam : uint8_t
59{
60 SetStatus = 0,
61 AuthSupport = 1,
62 AuthEnables = 2,
63 IP = 3,
64 IPSrc = 4,
65 MAC = 5,
66 SubnetMask = 6,
67 Gateway1 = 12,
68 Gateway1MAC = 13,
69 VLANId = 20,
70 CiphersuiteSupport = 22,
71 CiphersuiteEntries = 23,
72 cipherSuitePrivilegeLevels = 24,
73 IPFamilySupport = 50,
74 IPFamilyEnables = 51,
75 IPv6Status = 55,
76 IPv6StaticAddresses = 56,
77 IPv6DynamicAddresses = 59,
78 IPv6RouterControl = 64,
79 IPv6StaticRouter1IP = 65,
80 IPv6StaticRouter1MAC = 66,
81 IPv6StaticRouter1PrefixLength = 67,
82 IPv6StaticRouter1PrefixValue = 68,
83};
84
85/** @brief IPMI IP Origin Types */
86enum class IPSrc : uint8_t
87{
88 Unspecified = 0,
89 Static = 1,
90 DHCP = 2,
91 BIOS = 3,
92 BMC = 4,
93};
94
95/** @brief IPMI Set Status */
96enum class SetStatus : uint8_t
97{
98 Complete = 0,
99 InProgress = 1,
100 Commit = 2,
101};
102
103/** @brief IPMI Family Suport Bits */
104namespace IPFamilySupportFlag
105{
106constexpr uint8_t IPv6Only = 0;
107constexpr uint8_t DualStack = 1;
108constexpr uint8_t IPv6Alerts = 2;
109} // namespace IPFamilySupportFlag
110
111/** @brief IPMI IPFamily Enables Flag */
112enum class IPFamilyEnables : uint8_t
113{
114 IPv4Only = 0,
115 IPv6Only = 1,
116 DualStack = 2,
117};
118
119/** @brief IPMI IPv6 Dyanmic Status Bits */
120namespace IPv6StatusFlag
121{
122constexpr uint8_t DHCP = 0;
123constexpr uint8_t SLAAC = 1;
124}; // namespace IPv6StatusFlag
125
126/** @brief IPMI IPv6 Source */
127enum class IPv6Source : uint8_t
128{
129 Static = 0,
130 SLAAC = 1,
131 DHCP = 2,
132};
133
134/** @brief IPMI IPv6 Address Status */
135enum class IPv6AddressStatus : uint8_t
136{
137 Active = 0,
138 Disabled = 1,
139};
140
141namespace IPv6RouterControlFlag
142{
143constexpr uint8_t Static = 0;
144constexpr uint8_t Dynamic = 1;
145}; // namespace IPv6RouterControlFlag
146
147// LAN Handler specific response codes
148constexpr Cc ccParamNotSupported = 0x80;
149constexpr Cc ccParamSetLocked = 0x81;
150constexpr Cc ccParamReadOnly = 0x82;
151
152// VLANs are a 12-bit value
153constexpr uint16_t VLAN_VALUE_MASK = 0x0fff;
154constexpr uint16_t VLAN_ENABLE_FLAG = 0x8000;
155
156// Arbitrary v6 Address Limits to prevent too much output in ipmitool
157constexpr uint8_t MAX_IPV6_STATIC_ADDRESSES = 15;
158constexpr uint8_t MAX_IPV6_DYNAMIC_ADDRESSES = 15;
159
Jiaqing Zhao6d4a44e2022-01-24 15:04:00 +0800160// Prefix length limits of phosphor-networkd
Jiaqing Zhao6d4a44e2022-01-24 15:04:00 +0800161constexpr uint8_t MIN_IPV6_PREFIX_LENGTH = 1;
162constexpr uint8_t MAX_IPV6_PREFIX_LENGTH = 128;
163
John Wang8a7236a2021-01-04 15:31:44 +0800164/** @brief The dbus parameters for the interface corresponding to a channel
165 * This helps reduce the number of mapper lookups we need for each
166 * query and simplifies finding the VLAN interface if needed.
167 */
168struct ChannelParams
169{
170 /** @brief The channel ID */
171 int id;
172 /** @brief channel name for the interface */
173 std::string ifname;
174 /** @brief Name of the service on the bus */
175 std::string service;
176 /** @brief Lower level adapter path that is guaranteed to not be a VLAN */
177 std::string ifPath;
178 /** @brief Logical adapter path used for address assignment */
179 std::string logicalPath;
180};
181
John Wang8a7236a2021-01-04 15:31:44 +0800182/** @brief Determines the ethernet interface name corresponding to a channel
183 * Tries to map a VLAN object first so that the address information
184 * is accurate. Otherwise it gets the standard ethernet interface.
185 *
186 * @param[in] bus - The bus object used for lookups
187 * @param[in] channel - The channel id corresponding to an ethernet interface
188 * @return Ethernet interface service and object path if it exists
189 */
Patrick Williams5d82f472022-07-22 19:26:53 -0500190std::optional<ChannelParams> maybeGetChannelParams(sdbusplus::bus_t& bus,
John Wang8a7236a2021-01-04 15:31:44 +0800191 uint8_t channel);
192
193/** @brief A trivial helper around maybeGetChannelParams() that throws an
194 * exception when it is unable to acquire parameters for the channel.
195 *
196 * @param[in] bus - The bus object used for lookups
197 * @param[in] channel - The channel id corresponding to an ethernet interface
198 * @return Ethernet interface service and object path
199 */
Patrick Williams5d82f472022-07-22 19:26:53 -0500200ChannelParams getChannelParams(sdbusplus::bus_t& bus, uint8_t channel);
John Wang8a7236a2021-01-04 15:31:44 +0800201
202/** @brief Trivializes using parameter getter functions by providing a bus
203 * and channel parameters automatically.
204 *
205 * @param[in] channel - The channel id corresponding to an ethernet interface
206 * ...
207 */
208template <auto func, typename... Args>
209auto channelCall(uint8_t channel, Args&&... args)
210{
Patrick Williams5d82f472022-07-22 19:26:53 -0500211 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
John Wang8a7236a2021-01-04 15:31:44 +0800212 auto params = getChannelParams(bus, channel);
213 return std::invoke(func, bus, params, std::forward<Args>(args)...);
214}
215
216/** @brief Generic paramters for different address families */
217template <int family>
218struct AddrFamily
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500219{};
John Wang8a7236a2021-01-04 15:31:44 +0800220
221/** @brief Parameter specialization for IPv4 */
222template <>
223struct AddrFamily<AF_INET>
224{
225 using addr = in_addr;
226 static constexpr auto protocol =
Willy Tu523e2d12023-09-05 11:36:48 -0700227 sdbusplus::server::xyz::openbmc_project::network::IP::Protocol::IPv4;
John Wang8a7236a2021-01-04 15:31:44 +0800228 static constexpr size_t maxStrLen = INET6_ADDRSTRLEN;
229 static constexpr uint8_t defaultPrefix = 32;
230 static constexpr char propertyGateway[] = "DefaultGateway";
231};
232
233/** @brief Parameter specialization for IPv6 */
234template <>
235struct AddrFamily<AF_INET6>
236{
237 using addr = in6_addr;
238 static constexpr auto protocol =
Willy Tu523e2d12023-09-05 11:36:48 -0700239 sdbusplus::server::xyz::openbmc_project::network::IP::Protocol::IPv6;
John Wang8a7236a2021-01-04 15:31:44 +0800240 static constexpr size_t maxStrLen = INET6_ADDRSTRLEN;
241 static constexpr uint8_t defaultPrefix = 128;
242 static constexpr char propertyGateway[] = "DefaultGateway6";
243};
244
245/** @brief Interface Neighbor configuration parameters */
246template <int family>
247struct IfNeigh
248{
249 std::string path;
250 typename AddrFamily<family>::addr ip;
251 ether_addr mac;
252};
253
254/** @brief Interface IP Address configuration parameters */
255template <int family>
256struct IfAddr
257{
258 std::string path;
259 typename AddrFamily<family>::addr address;
Willy Tu523e2d12023-09-05 11:36:48 -0700260 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin origin;
John Wang8a7236a2021-01-04 15:31:44 +0800261 uint8_t prefix;
262};
263
264/** @brief Valid address origins for IPv6 */
265static inline const std::unordered_set<
Willy Tu523e2d12023-09-05 11:36:48 -0700266 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin>
267 originsV6Static = {sdbusplus::server::xyz::openbmc_project::network::IP::
John Wang8a7236a2021-01-04 15:31:44 +0800268 AddressOrigin::Static};
269static inline const std::unordered_set<
Willy Tu523e2d12023-09-05 11:36:48 -0700270 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin>
John Wang8a7236a2021-01-04 15:31:44 +0800271 originsV6Dynamic = {
Willy Tu523e2d12023-09-05 11:36:48 -0700272 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin::
John Wang8a7236a2021-01-04 15:31:44 +0800273 DHCP,
Willy Tu523e2d12023-09-05 11:36:48 -0700274 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin::
John Wang8a7236a2021-01-04 15:31:44 +0800275 SLAAC,
276};
277
278/** @brief A lazy lookup mechanism for iterating over object properties stored
279 * in DBus. This will only perform the object lookup when needed, and
280 * retains a cache of previous lookups to speed up future iterations.
281 */
282class ObjectLookupCache
283{
284 public:
285 using PropertiesCache = std::unordered_map<std::string, PropertyMap>;
286
287 /** @brief Creates a new ObjectLookupCache for the interface on the bus
288 * NOTE: The inputs to this object must outlive the object since
289 * they are only referenced by it.
290 *
291 * @param[in] bus - The bus object used for lookups
292 * @param[in] params - The parameters for the channel
293 * @param[in] intf - The interface we are looking up
294 */
Patrick Williams5d82f472022-07-22 19:26:53 -0500295 ObjectLookupCache(sdbusplus::bus_t& bus, const ChannelParams& params,
John Wang8a7236a2021-01-04 15:31:44 +0800296 const char* intf) :
297 bus(bus),
298 params(params), intf(intf),
299 objs(getAllDbusObjects(bus, params.logicalPath, intf, ""))
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500300 {}
John Wang8a7236a2021-01-04 15:31:44 +0800301
302 class iterator : public ObjectTree::const_iterator
303 {
304 public:
305 using value_type = PropertiesCache::value_type;
306
307 iterator(ObjectTree::const_iterator it, ObjectLookupCache& container) :
308 ObjectTree::const_iterator(it), container(container),
309 ret(container.cache.end())
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500310 {}
John Wang8a7236a2021-01-04 15:31:44 +0800311 value_type& operator*()
312 {
313 ret = container.get(ObjectTree::const_iterator::operator*().first);
314 return *ret;
315 }
316 value_type* operator->()
317 {
318 return &operator*();
319 }
320
321 private:
322 ObjectLookupCache& container;
323 PropertiesCache::iterator ret;
324 };
325
326 iterator begin() noexcept
327 {
328 return iterator(objs.begin(), *this);
329 }
330
331 iterator end() noexcept
332 {
333 return iterator(objs.end(), *this);
334 }
335
336 private:
Patrick Williams5d82f472022-07-22 19:26:53 -0500337 sdbusplus::bus_t& bus;
John Wang8a7236a2021-01-04 15:31:44 +0800338 const ChannelParams& params;
339 const char* const intf;
340 const ObjectTree objs;
341 PropertiesCache cache;
342
343 /** @brief Gets a cached copy of the object properties if possible
344 * Otherwise performs a query on DBus to look them up
345 *
346 * @param[in] path - The object path to lookup
347 * @return An iterator for the specified object path + properties
348 */
349 PropertiesCache::iterator get(const std::string& path)
350 {
351 auto it = cache.find(path);
352 if (it != cache.end())
353 {
354 return it;
355 }
356 auto properties = getAllDbusProperties(bus, params.service, path, intf);
357 return cache.insert({path, std::move(properties)}).first;
358 }
359};
360
361/** @brief Turns an IP address string into the network byte order form
362 * NOTE: This version strictly validates family matches
363 *
364 * @param[in] address - The string form of the address
365 * @return A network byte order address or none if conversion failed
366 */
367template <int family>
368std::optional<typename AddrFamily<family>::addr>
369 maybeStringToAddr(const char* address)
370{
371 typename AddrFamily<family>::addr ret;
372 if (inet_pton(family, address, &ret) == 1)
373 {
374 return ret;
375 }
376 return std::nullopt;
377}
378
379/** @brief Turns an IP address string into the network byte order form
380 * NOTE: This version strictly validates family matches
381 *
382 * @param[in] address - The string form of the address
383 * @return A network byte order address
384 */
385template <int family>
386typename AddrFamily<family>::addr stringToAddr(const char* address)
387{
388 auto ret = maybeStringToAddr<family>(address);
389 if (!ret)
390 {
391 phosphor::logging::log<phosphor::logging::level::ERR>(
392 "Failed to convert IP Address",
393 phosphor::logging::entry("FAMILY=%d", family),
394 phosphor::logging::entry("ADDRESS=%s", address));
395 phosphor::logging::elog<
Willy Tu523e2d12023-09-05 11:36:48 -0700396 sdbusplus::error::xyz::openbmc_project::common::InternalFailure>();
John Wang8a7236a2021-01-04 15:31:44 +0800397 }
398 return *ret;
399}
400
401/** @brief Turns an IP address in network byte order into a string
402 *
403 * @param[in] address - The string form of the address
404 * @return A network byte order address
405 */
406template <int family>
407std::string addrToString(const typename AddrFamily<family>::addr& address)
408{
409 std::string ret(AddrFamily<family>::maxStrLen, '\0');
410 inet_ntop(family, &address, ret.data(), ret.size());
411 ret.resize(strlen(ret.c_str()));
412 return ret;
413}
414
415/** @brief Converts a human readable MAC string into MAC bytes
416 *
417 * @param[in] mac - The MAC string
418 * @return MAC in bytes
419 */
420ether_addr stringToMAC(const char* mac);
421/** @brief Searches the ip object lookup cache for an address matching
422 * the input parameters. NOTE: The index lacks stability across address
423 * changes since the network daemon has no notion of stable indicies.
424 *
425 * @param[in] bus - The bus object used for lookups
426 * @param[in] params - The parameters for the channel
427 * @param[in] idx - The index of the desired address on the interface
428 * @param[in] origins - The allowed origins for the address objects
429 * @param[in] ips - The object lookup cache holding all of the address info
430 * @return The address and prefix if it was found
431 */
432template <int family>
433std::optional<IfAddr<family>> findIfAddr(
Patrick Williams5d82f472022-07-22 19:26:53 -0500434 [[maybe_unused]] sdbusplus::bus_t& bus,
Willy Tu11d68892022-01-20 10:37:34 -0800435 [[maybe_unused]] const ChannelParams& params, uint8_t idx,
John Wang8a7236a2021-01-04 15:31:44 +0800436 const std::unordered_set<
Willy Tu523e2d12023-09-05 11:36:48 -0700437 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin>&
John Wang8a7236a2021-01-04 15:31:44 +0800438 origins,
439 ObjectLookupCache& ips)
440{
441 for (const auto& [path, properties] : ips)
442 {
443 const auto& addrStr = std::get<std::string>(properties.at("Address"));
444 auto addr = maybeStringToAddr<family>(addrStr.c_str());
445 if (!addr)
446 {
447 continue;
448 }
449
Willy Tu523e2d12023-09-05 11:36:48 -0700450 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin
451 origin = sdbusplus::server::xyz::openbmc_project::network::IP::
John Wang8a7236a2021-01-04 15:31:44 +0800452 convertAddressOriginFromString(
453 std::get<std::string>(properties.at("Origin")));
454 if (origins.find(origin) == origins.end())
455 {
456 continue;
457 }
458
459 if (idx > 0)
460 {
461 idx--;
462 continue;
463 }
464
465 IfAddr<family> ifaddr;
466 ifaddr.path = path;
467 ifaddr.address = *addr;
468 ifaddr.prefix = std::get<uint8_t>(properties.at("PrefixLength"));
469 ifaddr.origin = origin;
Willy Tu11d68892022-01-20 10:37:34 -0800470 return ifaddr;
John Wang8a7236a2021-01-04 15:31:44 +0800471 }
472
473 return std::nullopt;
474}
475/** @brief Trivial helper around findIfAddr that simplifies calls
476 * for one off lookups. Don't use this if you intend to do multiple
477 * lookups at a time.
478 *
479 * @param[in] bus - The bus object used for lookups
480 * @param[in] params - The parameters for the channel
481 * @param[in] idx - The index of the desired address on the interface
482 * @param[in] origins - The allowed origins for the address objects
483 * @return The address and prefix if it was found
484 */
485template <int family>
486auto getIfAddr(
Patrick Williams5d82f472022-07-22 19:26:53 -0500487 sdbusplus::bus_t& bus, const ChannelParams& params, uint8_t idx,
John Wang8a7236a2021-01-04 15:31:44 +0800488 const std::unordered_set<
Willy Tu523e2d12023-09-05 11:36:48 -0700489 sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin>&
John Wang8a7236a2021-01-04 15:31:44 +0800490 origins)
491{
492 ObjectLookupCache ips(bus, params, INTF_IP);
493 return findIfAddr<family>(bus, params, idx, origins, ips);
494}
495
John Wang8a7236a2021-01-04 15:31:44 +0800496/** @brief Reconfigures the IPv6 address info configured for the interface
497 *
498 * @param[in] bus - The bus object used for lookups
499 * @param[in] params - The parameters for the channel
500 * @param[in] idx - The address index to operate on
501 * @param[in] address - The new address
502 * @param[in] prefix - The new address prefix
503 */
Patrick Williams5d82f472022-07-22 19:26:53 -0500504void reconfigureIfAddr6(sdbusplus::bus_t& bus, const ChannelParams& params,
John Wang8a7236a2021-01-04 15:31:44 +0800505 uint8_t idx, const in6_addr& address, uint8_t prefix);
506
507/** @brief Retrieves the current gateway for the address family on the system
Lei YUd1bd8c42021-08-11 17:13:56 +0800508 * NOTE: The gateway is per channel instead of the system wide one.
John Wang8a7236a2021-01-04 15:31:44 +0800509 *
510 * @param[in] bus - The bus object used for lookups
511 * @param[in] params - The parameters for the channel
512 * @return An address representing the gateway address if it exists
513 */
514template <int family>
515std::optional<typename AddrFamily<family>::addr>
Patrick Williams5d82f472022-07-22 19:26:53 -0500516 getGatewayProperty(sdbusplus::bus_t& bus, const ChannelParams& params)
John Wang8a7236a2021-01-04 15:31:44 +0800517{
Lei YUd1bd8c42021-08-11 17:13:56 +0800518 auto objPath = "/xyz/openbmc_project/network/" + params.ifname;
519 auto gatewayStr = std::get<std::string>(
520 getDbusProperty(bus, params.service, objPath, INTF_ETHERNET,
521 AddrFamily<family>::propertyGateway));
John Wang8a7236a2021-01-04 15:31:44 +0800522 if (gatewayStr.empty())
523 {
524 return std::nullopt;
525 }
526 return stringToAddr<family>(gatewayStr.c_str());
527}
528
529template <int family>
530std::optional<IfNeigh<family>>
Patrick Williams5d82f472022-07-22 19:26:53 -0500531 findStaticNeighbor(sdbusplus::bus_t&, const ChannelParams&,
John Wang8a7236a2021-01-04 15:31:44 +0800532 const typename AddrFamily<family>::addr& ip,
533 ObjectLookupCache& neighbors)
534{
Willy Tu523e2d12023-09-05 11:36:48 -0700535 using sdbusplus::server::xyz::openbmc_project::network::Neighbor;
John Wang8a7236a2021-01-04 15:31:44 +0800536 const auto state =
Willy Tu523e2d12023-09-05 11:36:48 -0700537 sdbusplus::common::xyz::openbmc_project::network::convertForMessage(
John Wang8a7236a2021-01-04 15:31:44 +0800538 Neighbor::State::Permanent);
539 for (const auto& [path, neighbor] : neighbors)
540 {
541 const auto& ipStr = std::get<std::string>(neighbor.at("IPAddress"));
542 auto neighIP = maybeStringToAddr<family>(ipStr.c_str());
543 if (!neighIP)
544 {
545 continue;
546 }
William A. Kennington III7a3831b2023-06-21 01:10:49 -0700547 if (!stdplus::raw::equal(*neighIP, ip))
John Wang8a7236a2021-01-04 15:31:44 +0800548 {
549 continue;
550 }
551 if (state != std::get<std::string>(neighbor.at("State")))
552 {
553 continue;
554 }
555
556 IfNeigh<family> ret;
557 ret.path = path;
558 ret.ip = ip;
559 const auto& macStr = std::get<std::string>(neighbor.at("MACAddress"));
560 ret.mac = stringToMAC(macStr.c_str());
Willy Tu11d68892022-01-20 10:37:34 -0800561 return ret;
John Wang8a7236a2021-01-04 15:31:44 +0800562 }
563
564 return std::nullopt;
565}
566
567template <int family>
Patrick Williams5d82f472022-07-22 19:26:53 -0500568void createNeighbor(sdbusplus::bus_t& bus, const ChannelParams& params,
John Wang8a7236a2021-01-04 15:31:44 +0800569 const typename AddrFamily<family>::addr& address,
570 const ether_addr& mac)
571{
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500572 auto newreq = bus.new_method_call(params.service.c_str(),
573 params.logicalPath.c_str(),
574 INTF_NEIGHBOR_CREATE_STATIC, "Neighbor");
John Wang8a7236a2021-01-04 15:31:44 +0800575 std::string macStr = ether_ntoa(&mac);
576 newreq.append(addrToString<family>(address), macStr);
577 bus.call_noreply(newreq);
578}
579
580/** @brief Deletes the dbus object. Ignores empty objects or objects that are
581 * missing from the bus.
582 *
583 * @param[in] bus - The bus object used for lookups
584 * @param[in] service - The name of the service
585 * @param[in] path - The path of the object to delete
586 */
Patrick Williams5d82f472022-07-22 19:26:53 -0500587void deleteObjectIfExists(sdbusplus::bus_t& bus, const std::string& service,
John Wang8a7236a2021-01-04 15:31:44 +0800588 const std::string& path);
589
Lei YUd1bd8c42021-08-11 17:13:56 +0800590/** @brief Sets the value for the default gateway of the channel
John Wang8a7236a2021-01-04 15:31:44 +0800591 *
592 * @param[in] bus - The bus object used for lookups
593 * @param[in] params - The parameters for the channel
594 * @param[in] gateway - Gateway address to apply
595 */
596template <int family>
Patrick Williams5d82f472022-07-22 19:26:53 -0500597void setGatewayProperty(sdbusplus::bus_t& bus, const ChannelParams& params,
John Wang8a7236a2021-01-04 15:31:44 +0800598 const typename AddrFamily<family>::addr& address)
599{
600 // Save the old gateway MAC address if it exists so we can recreate it
601 auto gateway = getGatewayProperty<family>(bus, params);
602 std::optional<IfNeigh<family>> neighbor;
603 if (gateway)
604 {
605 ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR);
606 neighbor = findStaticNeighbor<family>(bus, params, *gateway, neighbors);
607 }
608
Lei YUd1bd8c42021-08-11 17:13:56 +0800609 auto objPath = "/xyz/openbmc_project/network/" + params.ifname;
610 setDbusProperty(bus, params.service, objPath, INTF_ETHERNET,
John Wang8a7236a2021-01-04 15:31:44 +0800611 AddrFamily<family>::propertyGateway,
612 addrToString<family>(address));
613
614 // Restore the gateway MAC if we had one
615 if (neighbor)
616 {
617 deleteObjectIfExists(bus, params.service, neighbor->path);
618 createNeighbor<family>(bus, params, address, neighbor->mac);
619 }
620}
621
Jian Zhang23f44652022-03-17 17:13:10 +0800622/** @enum SolConfParam
623 *
624 * using for Set/Get SOL configuration parameters command.
625 */
626enum class SolConfParam : uint8_t
627{
628 Progress, //!< Set In Progress.
629 Enable, //!< SOL Enable.
630 Authentication, //!< SOL Authentication.
631 Accumulate, //!< Character Accumulate Interval & Send Threshold.
632 Retry, //!< SOL Retry.
633 NonVbitrate, //!< SOL non-volatile bit rate.
634 Vbitrate, //!< SOL volatile bit rate.
635 Channel, //!< SOL payload channel.
636 Port, //!< SOL payload port.
637};
638
639constexpr uint8_t ipmiCCParamNotSupported = 0x80;
640constexpr uint8_t ipmiCCWriteReadParameter = 0x82;
641
Patrick Venture690a2342020-05-17 11:51:31 -0700642} // namespace transport
643} // namespace ipmi