Johnathan Mantey | affadb5 | 2019-10-07 10:13:53 -0700 | [diff] [blame] | 1 | #include "app/channel.hpp" |
| 2 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 3 | #include <arpa/inet.h> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 4 | #include <netinet/ether.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 5 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 6 | #include <array> |
| 7 | #include <bitset> |
| 8 | #include <cinttypes> |
| 9 | #include <cstdint> |
| 10 | #include <cstring> |
Johnathan Mantey | affadb5 | 2019-10-07 10:13:53 -0700 | [diff] [blame] | 11 | #include <fstream> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 12 | #include <functional> |
Vernon Mauery | e08fbff | 2019-04-03 09:19:34 -0700 | [diff] [blame] | 13 | #include <ipmid/api.hpp> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 14 | #include <ipmid/message.hpp> |
| 15 | #include <ipmid/message/types.hpp> |
| 16 | #include <ipmid/types.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 17 | #include <ipmid/utils.hpp> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 18 | #include <optional> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 19 | #include <phosphor-logging/elog-errors.hpp> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 20 | #include <phosphor-logging/elog.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 22 | #include <sdbusplus/bus.hpp> |
| 23 | #include <sdbusplus/exception.hpp> |
tomjose | 26e1773 | 2016-03-03 08:52:51 -0600 | [diff] [blame] | 24 | #include <string> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 25 | #include <string_view> |
| 26 | #include <type_traits> |
| 27 | #include <unordered_map> |
| 28 | #include <unordered_set> |
| 29 | #include <user_channel/channel_layer.hpp> |
| 30 | #include <utility> |
| 31 | #include <vector> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 32 | #include <xyz/openbmc_project/Common/error.hpp> |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 33 | #include <xyz/openbmc_project/Network/IP/server.hpp> |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 34 | #include <xyz/openbmc_project/Network/Neighbor/server.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 35 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 36 | using phosphor::logging::commit; |
| 37 | using phosphor::logging::elog; |
| 38 | using phosphor::logging::entry; |
| 39 | using phosphor::logging::level; |
| 40 | using phosphor::logging::log; |
| 41 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 42 | using sdbusplus::xyz::openbmc_project::Network::server::IP; |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 43 | using sdbusplus::xyz::openbmc_project::Network::server::Neighbor; |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 44 | |
Johnathan Mantey | affadb5 | 2019-10-07 10:13:53 -0700 | [diff] [blame] | 45 | namespace cipher |
| 46 | { |
| 47 | |
| 48 | std::vector<uint8_t> getCipherList() |
| 49 | { |
| 50 | std::vector<uint8_t> cipherList; |
| 51 | |
| 52 | std::ifstream jsonFile(cipher::configFile); |
| 53 | if (!jsonFile.is_open()) |
| 54 | { |
| 55 | log<level::ERR>("Channel Cipher suites file not found"); |
| 56 | elog<InternalFailure>(); |
| 57 | } |
| 58 | |
| 59 | auto data = Json::parse(jsonFile, nullptr, false); |
| 60 | if (data.is_discarded()) |
| 61 | { |
| 62 | log<level::ERR>("Parsing channel cipher suites JSON failed"); |
| 63 | elog<InternalFailure>(); |
| 64 | } |
| 65 | |
| 66 | // Byte 1 is reserved |
| 67 | cipherList.push_back(0x00); |
| 68 | |
| 69 | for (const auto& record : data) |
| 70 | { |
| 71 | cipherList.push_back(record.value(cipher, 0)); |
| 72 | } |
| 73 | |
| 74 | return cipherList; |
| 75 | } |
| 76 | } // namespace cipher |
| 77 | |
| 78 | namespace ipmi |
| 79 | { |
| 80 | namespace transport |
| 81 | { |
| 82 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 83 | // LAN Handler specific response codes |
| 84 | constexpr Cc ccParamNotSupported = 0x80; |
| 85 | constexpr Cc ccParamSetLocked = 0x81; |
| 86 | constexpr Cc ccParamReadOnly = 0x82; |
| 87 | |
| 88 | // VLANs are a 12-bit value |
| 89 | constexpr uint16_t VLAN_VALUE_MASK = 0x0fff; |
| 90 | constexpr uint16_t VLAN_ENABLE_FLAG = 0x8000; |
| 91 | |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 92 | // Arbitrary v6 Address Limits to prevent too much output in ipmitool |
| 93 | constexpr uint8_t MAX_IPV6_STATIC_ADDRESSES = 15; |
| 94 | constexpr uint8_t MAX_IPV6_DYNAMIC_ADDRESSES = 15; |
| 95 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 96 | // D-Bus Network Daemon definitions |
| 97 | constexpr auto PATH_ROOT = "/xyz/openbmc_project/network"; |
| 98 | constexpr auto PATH_SYSTEMCONFIG = "/xyz/openbmc_project/network/config"; |
| 99 | |
| 100 | constexpr auto INTF_SYSTEMCONFIG = |
| 101 | "xyz.openbmc_project.Network.SystemConfiguration"; |
| 102 | constexpr auto INTF_ETHERNET = "xyz.openbmc_project.Network.EthernetInterface"; |
| 103 | constexpr auto INTF_IP = "xyz.openbmc_project.Network.IP"; |
| 104 | constexpr auto INTF_IP_CREATE = "xyz.openbmc_project.Network.IP.Create"; |
| 105 | constexpr auto INTF_MAC = "xyz.openbmc_project.Network.MACAddress"; |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 106 | constexpr auto INTF_NEIGHBOR = "xyz.openbmc_project.Network.Neighbor"; |
| 107 | constexpr auto INTF_NEIGHBOR_CREATE_STATIC = |
| 108 | "xyz.openbmc_project.Network.Neighbor.CreateStatic"; |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 109 | constexpr auto INTF_VLAN = "xyz.openbmc_project.Network.VLAN"; |
| 110 | constexpr auto INTF_VLAN_CREATE = "xyz.openbmc_project.Network.VLAN.Create"; |
| 111 | |
| 112 | /** @brief Generic paramters for different address families */ |
| 113 | template <int family> |
| 114 | struct AddrFamily |
| 115 | { |
| 116 | }; |
| 117 | |
| 118 | /** @brief Parameter specialization for IPv4 */ |
| 119 | template <> |
| 120 | struct AddrFamily<AF_INET> |
| 121 | { |
| 122 | using addr = in_addr; |
| 123 | static constexpr auto protocol = IP::Protocol::IPv4; |
| 124 | static constexpr size_t maxStrLen = INET6_ADDRSTRLEN; |
| 125 | static constexpr uint8_t defaultPrefix = 32; |
| 126 | static constexpr char propertyGateway[] = "DefaultGateway"; |
| 127 | }; |
| 128 | |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 129 | /** @brief Parameter specialization for IPv6 */ |
| 130 | template <> |
| 131 | struct AddrFamily<AF_INET6> |
| 132 | { |
| 133 | using addr = in6_addr; |
| 134 | static constexpr auto protocol = IP::Protocol::IPv6; |
| 135 | static constexpr size_t maxStrLen = INET6_ADDRSTRLEN; |
| 136 | static constexpr uint8_t defaultPrefix = 128; |
| 137 | static constexpr char propertyGateway[] = "DefaultGateway6"; |
| 138 | }; |
| 139 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 140 | /** @brief Valid address origins for IPv4 */ |
| 141 | const std::unordered_set<IP::AddressOrigin> originsV4 = { |
| 142 | IP::AddressOrigin::Static, |
| 143 | IP::AddressOrigin::DHCP, |
| 144 | }; |
| 145 | |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 146 | /** @brief Valid address origins for IPv6 */ |
| 147 | const std::unordered_set<IP::AddressOrigin> originsV6Static = { |
| 148 | IP::AddressOrigin::Static}; |
| 149 | const std::unordered_set<IP::AddressOrigin> originsV6Dynamic = { |
| 150 | IP::AddressOrigin::DHCP, |
| 151 | IP::AddressOrigin::SLAAC, |
| 152 | }; |
| 153 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 154 | /** @brief Interface IP Address configuration parameters */ |
| 155 | template <int family> |
| 156 | struct IfAddr |
| 157 | { |
| 158 | std::string path; |
| 159 | typename AddrFamily<family>::addr address; |
| 160 | IP::AddressOrigin origin; |
| 161 | uint8_t prefix; |
| 162 | }; |
| 163 | |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 164 | /** @brief Interface Neighbor configuration parameters */ |
| 165 | template <int family> |
| 166 | struct IfNeigh |
| 167 | { |
| 168 | std::string path; |
| 169 | typename AddrFamily<family>::addr ip; |
| 170 | ether_addr mac; |
| 171 | }; |
| 172 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 173 | /** @brief IPMI LAN Parameters */ |
| 174 | enum class LanParam : uint8_t |
| 175 | { |
| 176 | SetStatus = 0, |
| 177 | AuthSupport = 1, |
| 178 | AuthEnables = 2, |
| 179 | IP = 3, |
| 180 | IPSrc = 4, |
| 181 | MAC = 5, |
| 182 | SubnetMask = 6, |
| 183 | Gateway1 = 12, |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 184 | Gateway1MAC = 13, |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 185 | VLANId = 20, |
| 186 | CiphersuiteSupport = 22, |
| 187 | CiphersuiteEntries = 23, |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 188 | IPFamilySupport = 50, |
| 189 | IPFamilyEnables = 51, |
| 190 | IPv6Status = 55, |
| 191 | IPv6StaticAddresses = 56, |
| 192 | IPv6DynamicAddresses = 59, |
| 193 | IPv6RouterControl = 64, |
| 194 | IPv6StaticRouter1IP = 65, |
| 195 | IPv6StaticRouter1MAC = 66, |
| 196 | IPv6StaticRouter1PrefixLength = 67, |
| 197 | IPv6StaticRouter1PrefixValue = 68, |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Johnathan Mantey | b87034e | 2019-09-16 10:50:50 -0700 | [diff] [blame] | 200 | static constexpr uint8_t oemCmdStart = 192; |
| 201 | static constexpr uint8_t oemCmdEnd = 255; |
| 202 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 203 | /** @brief IPMI IP Origin Types */ |
| 204 | enum class IPSrc : uint8_t |
| 205 | { |
| 206 | Unspecified = 0, |
| 207 | Static = 1, |
| 208 | DHCP = 2, |
| 209 | BIOS = 3, |
| 210 | BMC = 4, |
| 211 | }; |
| 212 | |
| 213 | /** @brief IPMI Set Status */ |
| 214 | enum class SetStatus : uint8_t |
| 215 | { |
| 216 | Complete = 0, |
| 217 | InProgress = 1, |
| 218 | Commit = 2, |
| 219 | }; |
| 220 | |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 221 | /** @brief IPMI Family Suport Bits */ |
| 222 | namespace IPFamilySupportFlag |
| 223 | { |
| 224 | constexpr uint8_t IPv6Only = 0; |
| 225 | constexpr uint8_t DualStack = 1; |
| 226 | constexpr uint8_t IPv6Alerts = 2; |
| 227 | } // namespace IPFamilySupportFlag |
| 228 | |
| 229 | /** @brief IPMI IPFamily Enables Flag */ |
| 230 | enum class IPFamilyEnables : uint8_t |
| 231 | { |
| 232 | IPv4Only = 0, |
| 233 | IPv6Only = 1, |
| 234 | DualStack = 2, |
| 235 | }; |
| 236 | |
| 237 | /** @brief IPMI IPv6 Dyanmic Status Bits */ |
| 238 | namespace IPv6StatusFlag |
| 239 | { |
| 240 | constexpr uint8_t DHCP = 0; |
| 241 | constexpr uint8_t SLAAC = 1; |
| 242 | }; // namespace IPv6StatusFlag |
| 243 | |
| 244 | /** @brief IPMI IPv6 Source */ |
| 245 | enum class IPv6Source : uint8_t |
| 246 | { |
| 247 | Static = 0, |
| 248 | SLAAC = 1, |
| 249 | DHCP = 2, |
| 250 | }; |
| 251 | |
| 252 | /** @brief IPMI IPv6 Address Status */ |
| 253 | enum class IPv6AddressStatus : uint8_t |
| 254 | { |
| 255 | Active = 0, |
| 256 | Disabled = 1, |
| 257 | }; |
| 258 | |
| 259 | namespace IPv6RouterControlFlag |
| 260 | { |
| 261 | constexpr uint8_t Static = 0; |
| 262 | constexpr uint8_t Dynamic = 1; |
| 263 | }; // namespace IPv6RouterControlFlag |
| 264 | |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 265 | /** @brief A trivial helper used to determine if two PODs are equal |
| 266 | * |
| 267 | * @params[in] a - The first object to compare |
| 268 | * @params[in] b - The second object to compare |
| 269 | * @return True if the objects are the same bytewise |
| 270 | */ |
| 271 | template <typename T> |
| 272 | bool equal(const T& a, const T& b) |
| 273 | { |
| 274 | static_assert(std::is_trivially_copyable_v<T>); |
| 275 | return std::memcmp(&a, &b, sizeof(T)) == 0; |
| 276 | } |
| 277 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 278 | /** @brief Copies bytes from an array into a trivially copyable container |
| 279 | * |
| 280 | * @params[out] t - The container receiving the data |
| 281 | * @params[in] bytes - The data to copy |
| 282 | */ |
| 283 | template <size_t N, typename T> |
| 284 | void copyInto(T& t, const std::array<uint8_t, N>& bytes) |
| 285 | { |
| 286 | static_assert(std::is_trivially_copyable_v<T>); |
| 287 | static_assert(N == sizeof(T)); |
| 288 | std::memcpy(&t, bytes.data(), bytes.size()); |
Patrick Venture | c7c1c3c | 2017-11-15 14:29:18 -0800 | [diff] [blame] | 289 | } |
| 290 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 291 | /** @brief Gets a generic view of the bytes in the input container |
| 292 | * |
| 293 | * @params[in] t - The data to reference |
| 294 | * @return A string_view referencing the bytes in the container |
| 295 | */ |
| 296 | template <typename T> |
| 297 | std::string_view dataRef(const T& t) |
tomjose | 26e1773 | 2016-03-03 08:52:51 -0600 | [diff] [blame] | 298 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 299 | static_assert(std::is_trivially_copyable_v<T>); |
| 300 | return {reinterpret_cast<const char*>(&t), sizeof(T)}; |
| 301 | } |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 302 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 303 | /** @brief The dbus parameters for the interface corresponding to a channel |
| 304 | * This helps reduce the number of mapper lookups we need for each |
| 305 | * query and simplifies finding the VLAN interface if needed. |
| 306 | */ |
| 307 | struct ChannelParams |
| 308 | { |
| 309 | /** @brief The channel ID */ |
| 310 | int id; |
| 311 | /** @brief channel name for the interface */ |
| 312 | std::string ifname; |
| 313 | /** @brief Name of the service on the bus */ |
| 314 | std::string service; |
| 315 | /** @brief Lower level adapter path that is guaranteed to not be a VLAN */ |
| 316 | std::string ifPath; |
| 317 | /** @brief Logical adapter path used for address assignment */ |
| 318 | std::string logicalPath; |
| 319 | }; |
| 320 | |
| 321 | /** @brief Determines the ethernet interface name corresponding to a channel |
| 322 | * Tries to map a VLAN object first so that the address information |
| 323 | * is accurate. Otherwise it gets the standard ethernet interface. |
| 324 | * |
| 325 | * @param[in] bus - The bus object used for lookups |
| 326 | * @param[in] channel - The channel id corresponding to an ethernet interface |
| 327 | * @return Ethernet interface service and object path if it exists |
| 328 | */ |
| 329 | std::optional<ChannelParams> maybeGetChannelParams(sdbusplus::bus::bus& bus, |
| 330 | uint8_t channel) |
| 331 | { |
| 332 | auto ifname = getChannelName(channel); |
| 333 | if (ifname.empty()) |
Patrick Venture | c7c1c3c | 2017-11-15 14:29:18 -0800 | [diff] [blame] | 334 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 335 | return std::nullopt; |
Patrick Venture | c7c1c3c | 2017-11-15 14:29:18 -0800 | [diff] [blame] | 336 | } |
Patrick Venture | c7c1c3c | 2017-11-15 14:29:18 -0800 | [diff] [blame] | 337 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 338 | // Enumerate all VLAN + ETHERNET interfaces |
| 339 | auto req = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_INTF, |
| 340 | "GetSubTree"); |
| 341 | req.append(PATH_ROOT, 0, |
| 342 | std::vector<std::string>{INTF_VLAN, INTF_ETHERNET}); |
| 343 | auto reply = bus.call(req); |
| 344 | ObjectTree objs; |
| 345 | reply.read(objs); |
| 346 | |
| 347 | ChannelParams params; |
| 348 | for (const auto& [path, impls] : objs) |
| 349 | { |
| 350 | if (path.find(ifname) == path.npos) |
| 351 | { |
| 352 | continue; |
| 353 | } |
| 354 | for (const auto& [service, intfs] : impls) |
| 355 | { |
| 356 | bool vlan = false; |
| 357 | bool ethernet = false; |
| 358 | for (const auto& intf : intfs) |
| 359 | { |
| 360 | if (intf == INTF_VLAN) |
| 361 | { |
| 362 | vlan = true; |
| 363 | } |
| 364 | else if (intf == INTF_ETHERNET) |
| 365 | { |
| 366 | ethernet = true; |
| 367 | } |
| 368 | } |
| 369 | if (params.service.empty() && (vlan || ethernet)) |
| 370 | { |
| 371 | params.service = service; |
| 372 | } |
| 373 | if (params.ifPath.empty() && !vlan && ethernet) |
| 374 | { |
| 375 | params.ifPath = path; |
| 376 | } |
| 377 | if (params.logicalPath.empty() && vlan) |
| 378 | { |
| 379 | params.logicalPath = path; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // We must have a path for the underlying interface |
| 385 | if (params.ifPath.empty()) |
| 386 | { |
| 387 | return std::nullopt; |
| 388 | } |
| 389 | // We don't have a VLAN so the logical path is the same |
| 390 | if (params.logicalPath.empty()) |
| 391 | { |
| 392 | params.logicalPath = params.ifPath; |
| 393 | } |
| 394 | |
| 395 | params.id = channel; |
| 396 | params.ifname = std::move(ifname); |
| 397 | return std::move(params); |
| 398 | } |
| 399 | |
| 400 | /** @brief A trivial helper around maybeGetChannelParams() that throws an |
| 401 | * exception when it is unable to acquire parameters for the channel. |
| 402 | * |
| 403 | * @param[in] bus - The bus object used for lookups |
| 404 | * @param[in] channel - The channel id corresponding to an ethernet interface |
| 405 | * @return Ethernet interface service and object path |
| 406 | */ |
| 407 | ChannelParams getChannelParams(sdbusplus::bus::bus& bus, uint8_t channel) |
| 408 | { |
| 409 | auto params = maybeGetChannelParams(bus, channel); |
| 410 | if (!params) |
| 411 | { |
| 412 | log<level::ERR>("Failed to get channel params", |
| 413 | entry("CHANNEL=%" PRIu8, channel)); |
| 414 | elog<InternalFailure>(); |
| 415 | } |
| 416 | return std::move(*params); |
| 417 | } |
| 418 | |
| 419 | /** @brief Wraps the phosphor logging method to insert some additional metadata |
| 420 | * |
| 421 | * @param[in] params - The parameters for the channel |
| 422 | * ... |
| 423 | */ |
| 424 | template <auto level, typename... Args> |
| 425 | auto logWithChannel(const ChannelParams& params, Args&&... args) |
| 426 | { |
| 427 | return log<level>(std::forward<Args>(args)..., |
| 428 | entry("CHANNEL=%d", params.id), |
| 429 | entry("IFNAME=%s", params.ifname.c_str())); |
| 430 | } |
| 431 | template <auto level, typename... Args> |
| 432 | auto logWithChannel(const std::optional<ChannelParams>& params, Args&&... args) |
| 433 | { |
| 434 | if (params) |
| 435 | { |
| 436 | return logWithChannel<level>(*params, std::forward<Args>(args)...); |
| 437 | } |
| 438 | return log<level>(std::forward<Args>(args)...); |
| 439 | } |
| 440 | |
| 441 | /** @brief Trivializes using parameter getter functions by providing a bus |
| 442 | * and channel parameters automatically. |
| 443 | * |
| 444 | * @param[in] channel - The channel id corresponding to an ethernet interface |
| 445 | * ... |
| 446 | */ |
| 447 | template <auto func, typename... Args> |
| 448 | auto channelCall(uint8_t channel, Args&&... args) |
| 449 | { |
| 450 | sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection()); |
| 451 | auto params = getChannelParams(bus, channel); |
| 452 | return std::invoke(func, bus, params, std::forward<Args>(args)...); |
| 453 | } |
| 454 | |
| 455 | /** @brief Determines if the ethernet interface is using DHCP |
| 456 | * |
| 457 | * @param[in] bus - The bus object used for lookups |
| 458 | * @param[in] params - The parameters for the channel |
| 459 | * @return True if DHCP is enabled, false otherwise |
| 460 | */ |
| 461 | bool getDHCPProperty(sdbusplus::bus::bus& bus, const ChannelParams& params) |
| 462 | { |
| 463 | return std::get<bool>(getDbusProperty( |
| 464 | bus, params.service, params.logicalPath, INTF_ETHERNET, "DHCPEnabled")); |
| 465 | } |
| 466 | |
| 467 | /** @brief Sets the system value for DHCP on the given interface |
| 468 | * |
| 469 | * @param[in] bus - The bus object used for lookups |
| 470 | * @param[in] params - The parameters for the channel |
| 471 | * @param[in] on - Whether or not to enable DHCP |
| 472 | */ |
| 473 | void setDHCPProperty(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 474 | bool on) |
| 475 | { |
| 476 | setDbusProperty(bus, params.service, params.logicalPath, INTF_ETHERNET, |
| 477 | "DHCPEnabled", on); |
| 478 | } |
| 479 | |
| 480 | /** @brief Converts a human readable MAC string into MAC bytes |
| 481 | * |
| 482 | * @param[in] mac - The MAC string |
| 483 | * @return MAC in bytes |
| 484 | */ |
| 485 | ether_addr stringToMAC(const char* mac) |
| 486 | { |
| 487 | const ether_addr* ret = ether_aton(mac); |
| 488 | if (ret == nullptr) |
| 489 | { |
| 490 | log<level::ERR>("Invalid MAC Address", entry("MAC=%s", mac)); |
| 491 | elog<InternalFailure>(); |
| 492 | } |
| 493 | return *ret; |
| 494 | } |
| 495 | |
| 496 | /** @brief Determines the MAC of the ethernet interface |
| 497 | * |
| 498 | * @param[in] bus - The bus object used for lookups |
| 499 | * @param[in] params - The parameters for the channel |
| 500 | * @return The configured mac address |
| 501 | */ |
| 502 | ether_addr getMACProperty(sdbusplus::bus::bus& bus, const ChannelParams& params) |
| 503 | { |
| 504 | auto macStr = std::get<std::string>(getDbusProperty( |
| 505 | bus, params.service, params.ifPath, INTF_MAC, "MACAddress")); |
| 506 | return stringToMAC(macStr.c_str()); |
| 507 | } |
| 508 | |
| 509 | /** @brief Sets the system value for MAC address on the given interface |
| 510 | * |
| 511 | * @param[in] bus - The bus object used for lookups |
| 512 | * @param[in] params - The parameters for the channel |
| 513 | * @param[in] mac - MAC address to apply |
| 514 | */ |
| 515 | void setMACProperty(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 516 | const ether_addr& mac) |
| 517 | { |
| 518 | std::string macStr = ether_ntoa(&mac); |
| 519 | setDbusProperty(bus, params.service, params.ifPath, INTF_MAC, "MACAddress", |
| 520 | macStr); |
| 521 | } |
| 522 | |
| 523 | /** @brief Turns an IP address string into the network byte order form |
| 524 | * NOTE: This version strictly validates family matches |
| 525 | * |
| 526 | * @param[in] address - The string form of the address |
| 527 | * @return A network byte order address or none if conversion failed |
| 528 | */ |
| 529 | template <int family> |
| 530 | std::optional<typename AddrFamily<family>::addr> |
| 531 | maybeStringToAddr(const char* address) |
| 532 | { |
| 533 | typename AddrFamily<family>::addr ret; |
| 534 | if (inet_pton(family, address, &ret) == 1) |
| 535 | { |
| 536 | return ret; |
| 537 | } |
| 538 | return std::nullopt; |
| 539 | } |
| 540 | |
| 541 | /** @brief Turns an IP address string into the network byte order form |
| 542 | * NOTE: This version strictly validates family matches |
| 543 | * |
| 544 | * @param[in] address - The string form of the address |
| 545 | * @return A network byte order address |
| 546 | */ |
| 547 | template <int family> |
| 548 | typename AddrFamily<family>::addr stringToAddr(const char* address) |
| 549 | { |
| 550 | auto ret = maybeStringToAddr<family>(address); |
| 551 | if (!ret) |
| 552 | { |
| 553 | log<level::ERR>("Failed to convert IP Address", |
| 554 | entry("FAMILY=%d", family), |
| 555 | entry("ADDRESS=%s", address)); |
| 556 | elog<InternalFailure>(); |
| 557 | } |
| 558 | return *ret; |
| 559 | } |
| 560 | |
| 561 | /** @brief Turns an IP address in network byte order into a string |
| 562 | * |
| 563 | * @param[in] address - The string form of the address |
| 564 | * @return A network byte order address |
| 565 | */ |
| 566 | template <int family> |
| 567 | std::string addrToString(const typename AddrFamily<family>::addr& address) |
| 568 | { |
| 569 | std::string ret(AddrFamily<family>::maxStrLen, '\0'); |
| 570 | inet_ntop(family, &address, ret.data(), ret.size()); |
| 571 | ret.resize(strlen(ret.c_str())); |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | /** @brief Retrieves the current gateway for the address family on the system |
| 576 | * NOTE: The gateway is currently system wide and not per channel |
| 577 | * |
| 578 | * @param[in] bus - The bus object used for lookups |
| 579 | * @param[in] params - The parameters for the channel |
| 580 | * @return An address representing the gateway address if it exists |
| 581 | */ |
| 582 | template <int family> |
| 583 | std::optional<typename AddrFamily<family>::addr> |
| 584 | getGatewayProperty(sdbusplus::bus::bus& bus, const ChannelParams& params) |
| 585 | { |
| 586 | auto gatewayStr = std::get<std::string>(getDbusProperty( |
| 587 | bus, params.service, PATH_SYSTEMCONFIG, INTF_SYSTEMCONFIG, |
| 588 | AddrFamily<family>::propertyGateway)); |
| 589 | if (gatewayStr.empty()) |
| 590 | { |
| 591 | return std::nullopt; |
| 592 | } |
| 593 | return stringToAddr<family>(gatewayStr.c_str()); |
| 594 | } |
| 595 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 596 | /** @brief A lazy lookup mechanism for iterating over object properties stored |
| 597 | * in DBus. This will only perform the object lookup when needed, and |
| 598 | * retains a cache of previous lookups to speed up future iterations. |
| 599 | */ |
| 600 | class ObjectLookupCache |
| 601 | { |
| 602 | public: |
| 603 | using PropertiesCache = std::unordered_map<std::string, PropertyMap>; |
| 604 | |
| 605 | /** @brief Creates a new ObjectLookupCache for the interface on the bus |
| 606 | * NOTE: The inputs to this object must outlive the object since |
| 607 | * they are only referenced by it. |
| 608 | * |
| 609 | * @param[in] bus - The bus object used for lookups |
| 610 | * @param[in] params - The parameters for the channel |
| 611 | * @param[in] intf - The interface we are looking up |
| 612 | */ |
| 613 | ObjectLookupCache(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 614 | const char* intf) : |
| 615 | bus(bus), |
| 616 | params(params), intf(intf), |
| 617 | objs(getAllDbusObjects(bus, params.logicalPath, intf, "")) |
| 618 | { |
| 619 | } |
| 620 | |
| 621 | class iterator : public ObjectTree::const_iterator |
| 622 | { |
| 623 | public: |
| 624 | using value_type = PropertiesCache::value_type; |
| 625 | |
| 626 | iterator(ObjectTree::const_iterator it, ObjectLookupCache& container) : |
| 627 | ObjectTree::const_iterator(it), container(container), |
| 628 | ret(container.cache.end()) |
| 629 | { |
| 630 | } |
| 631 | value_type& operator*() |
| 632 | { |
| 633 | ret = container.get(ObjectTree::const_iterator::operator*().first); |
| 634 | return *ret; |
| 635 | } |
| 636 | value_type* operator->() |
| 637 | { |
| 638 | return &operator*(); |
| 639 | } |
| 640 | |
| 641 | private: |
| 642 | ObjectLookupCache& container; |
| 643 | PropertiesCache::iterator ret; |
| 644 | }; |
| 645 | |
| 646 | iterator begin() noexcept |
| 647 | { |
| 648 | return iterator(objs.begin(), *this); |
| 649 | } |
| 650 | |
| 651 | iterator end() noexcept |
| 652 | { |
| 653 | return iterator(objs.end(), *this); |
| 654 | } |
| 655 | |
| 656 | private: |
| 657 | sdbusplus::bus::bus& bus; |
| 658 | const ChannelParams& params; |
| 659 | const char* const intf; |
| 660 | const ObjectTree objs; |
| 661 | PropertiesCache cache; |
| 662 | |
| 663 | /** @brief Gets a cached copy of the object properties if possible |
| 664 | * Otherwise performs a query on DBus to look them up |
| 665 | * |
| 666 | * @param[in] path - The object path to lookup |
| 667 | * @return An iterator for the specified object path + properties |
| 668 | */ |
| 669 | PropertiesCache::iterator get(const std::string& path) |
| 670 | { |
| 671 | auto it = cache.find(path); |
| 672 | if (it != cache.end()) |
| 673 | { |
| 674 | return it; |
| 675 | } |
| 676 | auto properties = getAllDbusProperties(bus, params.service, path, intf); |
| 677 | return cache.insert({path, std::move(properties)}).first; |
| 678 | } |
| 679 | }; |
| 680 | |
| 681 | /** @brief Searches the ip object lookup cache for an address matching |
| 682 | * the input parameters. NOTE: The index lacks stability across address |
| 683 | * changes since the network daemon has no notion of stable indicies. |
| 684 | * |
| 685 | * @param[in] bus - The bus object used for lookups |
| 686 | * @param[in] params - The parameters for the channel |
| 687 | * @param[in] idx - The index of the desired address on the interface |
| 688 | * @param[in] origins - The allowed origins for the address objects |
| 689 | * @param[in] ips - The object lookup cache holding all of the address info |
| 690 | * @return The address and prefix if it was found |
| 691 | */ |
| 692 | template <int family> |
| 693 | std::optional<IfAddr<family>> |
| 694 | findIfAddr(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 695 | uint8_t idx, |
| 696 | const std::unordered_set<IP::AddressOrigin>& origins, |
| 697 | ObjectLookupCache& ips) |
| 698 | { |
| 699 | for (const auto& [path, properties] : ips) |
| 700 | { |
| 701 | const auto& addrStr = std::get<std::string>(properties.at("Address")); |
| 702 | auto addr = maybeStringToAddr<family>(addrStr.c_str()); |
| 703 | if (!addr) |
| 704 | { |
| 705 | continue; |
| 706 | } |
| 707 | |
| 708 | IP::AddressOrigin origin = IP::convertAddressOriginFromString( |
| 709 | std::get<std::string>(properties.at("Origin"))); |
| 710 | if (origins.find(origin) == origins.end()) |
| 711 | { |
| 712 | continue; |
| 713 | } |
| 714 | |
| 715 | if (idx > 0) |
| 716 | { |
| 717 | idx--; |
| 718 | continue; |
| 719 | } |
| 720 | |
| 721 | IfAddr<family> ifaddr; |
| 722 | ifaddr.path = path; |
| 723 | ifaddr.address = *addr; |
| 724 | ifaddr.prefix = std::get<uint8_t>(properties.at("PrefixLength")); |
| 725 | ifaddr.origin = origin; |
| 726 | return std::move(ifaddr); |
| 727 | } |
| 728 | |
| 729 | return std::nullopt; |
| 730 | } |
| 731 | |
| 732 | /** @brief Trivial helper around findIfAddr that simplifies calls |
| 733 | * for one off lookups. Don't use this if you intend to do multiple |
| 734 | * lookups at a time. |
| 735 | * |
| 736 | * @param[in] bus - The bus object used for lookups |
| 737 | * @param[in] params - The parameters for the channel |
| 738 | * @param[in] idx - The index of the desired address on the interface |
| 739 | * @param[in] origins - The allowed origins for the address objects |
| 740 | * @return The address and prefix if it was found |
| 741 | */ |
| 742 | template <int family> |
| 743 | auto getIfAddr(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 744 | uint8_t idx, |
| 745 | const std::unordered_set<IP::AddressOrigin>& origins) |
| 746 | { |
| 747 | ObjectLookupCache ips(bus, params, INTF_IP); |
| 748 | return findIfAddr<family>(bus, params, idx, origins, ips); |
| 749 | } |
| 750 | |
| 751 | /** @brief Deletes the dbus object. Ignores empty objects or objects that are |
| 752 | * missing from the bus. |
| 753 | * |
| 754 | * @param[in] bus - The bus object used for lookups |
| 755 | * @param[in] service - The name of the service |
| 756 | * @param[in] path - The path of the object to delete |
| 757 | */ |
| 758 | void deleteObjectIfExists(sdbusplus::bus::bus& bus, const std::string& service, |
| 759 | const std::string& path) |
| 760 | { |
| 761 | if (path.empty()) |
| 762 | { |
| 763 | return; |
| 764 | } |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 765 | try |
tomjose | 26e1773 | 2016-03-03 08:52:51 -0600 | [diff] [blame] | 766 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 767 | auto req = bus.new_method_call(service.c_str(), path.c_str(), |
| 768 | ipmi::DELETE_INTERFACE, "Delete"); |
| 769 | bus.call_noreply(req); |
| 770 | } |
| 771 | catch (const sdbusplus::exception::SdBusError& e) |
| 772 | { |
| 773 | if (strcmp(e.name(), "org.freedesktop.DBus.Error.UnknownObject") != 0) |
tomjose | 26e1773 | 2016-03-03 08:52:51 -0600 | [diff] [blame] | 774 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 775 | // We want to rethrow real errors |
| 776 | throw; |
tomjose | 26e1773 | 2016-03-03 08:52:51 -0600 | [diff] [blame] | 777 | } |
| 778 | } |
tomjose | 26e1773 | 2016-03-03 08:52:51 -0600 | [diff] [blame] | 779 | } |
| 780 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 781 | /** @brief Sets the address info configured for the interface |
| 782 | * If a previous address path exists then it will be removed |
| 783 | * before the new address is added. |
| 784 | * |
| 785 | * @param[in] bus - The bus object used for lookups |
| 786 | * @param[in] params - The parameters for the channel |
| 787 | * @param[in] address - The address of the new IP |
| 788 | * @param[in] prefix - The prefix of the new IP |
| 789 | */ |
| 790 | template <int family> |
| 791 | void createIfAddr(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 792 | const typename AddrFamily<family>::addr& address, |
| 793 | uint8_t prefix) |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 794 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 795 | auto newreq = |
| 796 | bus.new_method_call(params.service.c_str(), params.logicalPath.c_str(), |
| 797 | INTF_IP_CREATE, "IP"); |
| 798 | std::string protocol = |
| 799 | sdbusplus::xyz::openbmc_project::Network::server::convertForMessage( |
| 800 | AddrFamily<family>::protocol); |
| 801 | newreq.append(protocol, addrToString<family>(address), prefix, ""); |
| 802 | bus.call_noreply(newreq); |
| 803 | } |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 804 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 805 | /** @brief Trivial helper for getting the IPv4 address from getIfAddrs() |
| 806 | * |
| 807 | * @param[in] bus - The bus object used for lookups |
| 808 | * @param[in] params - The parameters for the channel |
| 809 | * @return The address and prefix if found |
| 810 | */ |
| 811 | auto getIfAddr4(sdbusplus::bus::bus& bus, const ChannelParams& params) |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 812 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 813 | return getIfAddr<AF_INET>(bus, params, 0, originsV4); |
| 814 | } |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 815 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 816 | /** @brief Reconfigures the IPv4 address info configured for the interface |
| 817 | * |
| 818 | * @param[in] bus - The bus object used for lookups |
| 819 | * @param[in] params - The parameters for the channel |
| 820 | * @param[in] address - The new address if specified |
| 821 | * @param[in] prefix - The new address prefix if specified |
| 822 | */ |
| 823 | void reconfigureIfAddr4(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 824 | const std::optional<in_addr>& address, |
| 825 | std::optional<uint8_t> prefix) |
| 826 | { |
| 827 | auto ifaddr = getIfAddr4(bus, params); |
| 828 | if (!ifaddr && !address) |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 829 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 830 | log<level::ERR>("Missing address for IPv4 assignment"); |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 831 | elog<InternalFailure>(); |
| 832 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 833 | uint8_t fallbackPrefix = AddrFamily<AF_INET>::defaultPrefix; |
| 834 | if (ifaddr) |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 835 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 836 | fallbackPrefix = ifaddr->prefix; |
| 837 | deleteObjectIfExists(bus, params.service, ifaddr->path); |
| 838 | } |
| 839 | createIfAddr<AF_INET>(bus, params, address.value_or(ifaddr->address), |
| 840 | prefix.value_or(fallbackPrefix)); |
| 841 | } |
| 842 | |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 843 | template <int family> |
| 844 | std::optional<IfNeigh<family>> |
| 845 | findStaticNeighbor(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 846 | const typename AddrFamily<family>::addr& ip, |
| 847 | ObjectLookupCache& neighbors) |
| 848 | { |
| 849 | const auto state = |
| 850 | sdbusplus::xyz::openbmc_project::Network::server::convertForMessage( |
| 851 | Neighbor::State::Permanent); |
| 852 | for (const auto& [path, neighbor] : neighbors) |
| 853 | { |
| 854 | const auto& ipStr = std::get<std::string>(neighbor.at("IPAddress")); |
| 855 | auto neighIP = maybeStringToAddr<family>(ipStr.c_str()); |
| 856 | if (!neighIP) |
| 857 | { |
| 858 | continue; |
| 859 | } |
| 860 | if (!equal(*neighIP, ip)) |
| 861 | { |
| 862 | continue; |
| 863 | } |
| 864 | if (state != std::get<std::string>(neighbor.at("State"))) |
| 865 | { |
| 866 | continue; |
| 867 | } |
| 868 | |
| 869 | IfNeigh<family> ret; |
| 870 | ret.path = path; |
| 871 | ret.ip = ip; |
| 872 | const auto& macStr = std::get<std::string>(neighbor.at("MACAddress")); |
| 873 | ret.mac = stringToMAC(macStr.c_str()); |
| 874 | return std::move(ret); |
| 875 | } |
| 876 | |
| 877 | return std::nullopt; |
| 878 | } |
| 879 | |
| 880 | template <int family> |
| 881 | void createNeighbor(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 882 | const typename AddrFamily<family>::addr& address, |
| 883 | const ether_addr& mac) |
| 884 | { |
| 885 | auto newreq = |
| 886 | bus.new_method_call(params.service.c_str(), params.logicalPath.c_str(), |
| 887 | INTF_NEIGHBOR_CREATE_STATIC, "Neighbor"); |
| 888 | std::string macStr = ether_ntoa(&mac); |
| 889 | newreq.append(addrToString<family>(address), macStr); |
| 890 | bus.call_noreply(newreq); |
| 891 | } |
| 892 | |
| 893 | /** @brief Sets the system wide value for the default gateway |
| 894 | * |
| 895 | * @param[in] bus - The bus object used for lookups |
| 896 | * @param[in] params - The parameters for the channel |
| 897 | * @param[in] gateway - Gateway address to apply |
| 898 | */ |
| 899 | template <int family> |
| 900 | void setGatewayProperty(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 901 | const typename AddrFamily<family>::addr& address) |
| 902 | { |
| 903 | // Save the old gateway MAC address if it exists so we can recreate it |
| 904 | auto gateway = getGatewayProperty<family>(bus, params); |
| 905 | std::optional<IfNeigh<family>> neighbor; |
| 906 | if (gateway) |
| 907 | { |
| 908 | ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR); |
| 909 | neighbor = findStaticNeighbor<family>(bus, params, *gateway, neighbors); |
| 910 | } |
| 911 | |
| 912 | setDbusProperty(bus, params.service, PATH_SYSTEMCONFIG, INTF_SYSTEMCONFIG, |
| 913 | AddrFamily<family>::propertyGateway, |
| 914 | addrToString<family>(address)); |
| 915 | |
| 916 | // Restore the gateway MAC if we had one |
| 917 | if (neighbor) |
| 918 | { |
| 919 | deleteObjectIfExists(bus, params.service, neighbor->path); |
| 920 | createNeighbor<family>(bus, params, address, neighbor->mac); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | template <int family> |
| 925 | std::optional<IfNeigh<family>> findGatewayNeighbor(sdbusplus::bus::bus& bus, |
| 926 | const ChannelParams& params, |
| 927 | ObjectLookupCache& neighbors) |
| 928 | { |
| 929 | auto gateway = getGatewayProperty<family>(bus, params); |
| 930 | if (!gateway) |
| 931 | { |
| 932 | return std::nullopt; |
| 933 | } |
| 934 | |
| 935 | return findStaticNeighbor<family>(bus, params, *gateway, neighbors); |
| 936 | } |
| 937 | |
| 938 | template <int family> |
| 939 | std::optional<IfNeigh<family>> getGatewayNeighbor(sdbusplus::bus::bus& bus, |
| 940 | const ChannelParams& params) |
| 941 | { |
| 942 | ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR); |
| 943 | return findGatewayNeighbor<family>(bus, params, neighbors); |
| 944 | } |
| 945 | |
| 946 | template <int family> |
| 947 | void reconfigureGatewayMAC(sdbusplus::bus::bus& bus, |
| 948 | const ChannelParams& params, const ether_addr& mac) |
| 949 | { |
| 950 | auto gateway = getGatewayProperty<family>(bus, params); |
| 951 | if (!gateway) |
| 952 | { |
| 953 | log<level::ERR>("Tried to set Gateway MAC without Gateway"); |
| 954 | elog<InternalFailure>(); |
| 955 | } |
| 956 | |
| 957 | ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR); |
| 958 | auto neighbor = |
| 959 | findStaticNeighbor<family>(bus, params, *gateway, neighbors); |
| 960 | if (neighbor) |
| 961 | { |
| 962 | deleteObjectIfExists(bus, params.service, neighbor->path); |
| 963 | } |
| 964 | |
| 965 | createNeighbor<family>(bus, params, *gateway, mac); |
| 966 | } |
| 967 | |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 968 | /** @brief Deconfigures the IPv6 address info configured for the interface |
| 969 | * |
| 970 | * @param[in] bus - The bus object used for lookups |
| 971 | * @param[in] params - The parameters for the channel |
| 972 | * @param[in] idx - The address index to operate on |
| 973 | */ |
| 974 | void deconfigureIfAddr6(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 975 | uint8_t idx) |
| 976 | { |
| 977 | auto ifaddr = getIfAddr<AF_INET6>(bus, params, idx, originsV6Static); |
| 978 | if (ifaddr) |
| 979 | { |
| 980 | deleteObjectIfExists(bus, params.service, ifaddr->path); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | /** @brief Reconfigures the IPv6 address info configured for the interface |
| 985 | * |
| 986 | * @param[in] bus - The bus object used for lookups |
| 987 | * @param[in] params - The parameters for the channel |
| 988 | * @param[in] idx - The address index to operate on |
| 989 | * @param[in] address - The new address |
| 990 | * @param[in] prefix - The new address prefix |
| 991 | */ |
| 992 | void reconfigureIfAddr6(sdbusplus::bus::bus& bus, const ChannelParams& params, |
| 993 | uint8_t idx, const in6_addr& address, uint8_t prefix) |
| 994 | { |
| 995 | deconfigureIfAddr6(bus, params, idx); |
| 996 | createIfAddr<AF_INET6>(bus, params, address, prefix); |
| 997 | } |
| 998 | |
| 999 | /** @brief Converts the AddressOrigin into an IPv6Source |
| 1000 | * |
| 1001 | * @param[in] origin - The DBus Address Origin to convert |
| 1002 | * @return The IPv6Source version of the origin |
| 1003 | */ |
| 1004 | IPv6Source originToSourceType(IP::AddressOrigin origin) |
| 1005 | { |
| 1006 | switch (origin) |
| 1007 | { |
| 1008 | case IP::AddressOrigin::Static: |
| 1009 | return IPv6Source::Static; |
| 1010 | case IP::AddressOrigin::DHCP: |
| 1011 | return IPv6Source::DHCP; |
| 1012 | case IP::AddressOrigin::SLAAC: |
| 1013 | return IPv6Source::SLAAC; |
| 1014 | default: |
| 1015 | { |
| 1016 | auto originStr = sdbusplus::xyz::openbmc_project::Network::server:: |
| 1017 | convertForMessage(origin); |
| 1018 | log<level::ERR>( |
| 1019 | "Invalid IP::AddressOrigin conversion to IPv6Source", |
| 1020 | entry("ORIGIN=%s", originStr.c_str())); |
| 1021 | elog<InternalFailure>(); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /** @brief Packs the IPMI message response with IPv6 address data |
| 1027 | * |
| 1028 | * @param[out] ret - The IPMI response payload to be packed |
| 1029 | * @param[in] channel - The channel id corresponding to an ethernet interface |
| 1030 | * @param[in] set - The set selector for determining address index |
| 1031 | * @param[in] origins - Set of valid origins for address filtering |
| 1032 | */ |
| 1033 | void getLanIPv6Address(message::Payload& ret, uint8_t channel, uint8_t set, |
| 1034 | const std::unordered_set<IP::AddressOrigin>& origins) |
| 1035 | { |
| 1036 | auto source = IPv6Source::Static; |
| 1037 | bool enabled = false; |
| 1038 | in6_addr addr{}; |
| 1039 | uint8_t prefix = AddrFamily<AF_INET6>::defaultPrefix; |
| 1040 | auto status = IPv6AddressStatus::Disabled; |
| 1041 | |
| 1042 | auto ifaddr = channelCall<getIfAddr<AF_INET6>>(channel, set, origins); |
| 1043 | if (ifaddr) |
| 1044 | { |
| 1045 | source = originToSourceType(ifaddr->origin); |
| 1046 | enabled = true; |
| 1047 | addr = ifaddr->address; |
| 1048 | prefix = ifaddr->prefix; |
| 1049 | status = IPv6AddressStatus::Active; |
| 1050 | } |
| 1051 | |
| 1052 | ret.pack(set); |
| 1053 | ret.pack(static_cast<uint4_t>(source), uint3_t{}, enabled); |
| 1054 | ret.pack(std::string_view(reinterpret_cast<char*>(&addr), sizeof(addr))); |
| 1055 | ret.pack(prefix); |
| 1056 | ret.pack(static_cast<uint8_t>(status)); |
| 1057 | } |
| 1058 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1059 | /** @brief Gets the vlan ID configured on the interface |
| 1060 | * |
| 1061 | * @param[in] bus - The bus object used for lookups |
| 1062 | * @param[in] params - The parameters for the channel |
| 1063 | * @return VLAN id or the standard 0 for no VLAN |
| 1064 | */ |
| 1065 | uint16_t getVLANProperty(sdbusplus::bus::bus& bus, const ChannelParams& params) |
| 1066 | { |
| 1067 | // VLAN devices will always have a separate logical object |
| 1068 | if (params.ifPath == params.logicalPath) |
| 1069 | { |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | auto vlan = std::get<uint32_t>(getDbusProperty( |
| 1074 | bus, params.service, params.logicalPath, INTF_VLAN, "Id")); |
| 1075 | if ((vlan & VLAN_VALUE_MASK) != vlan) |
| 1076 | { |
| 1077 | logWithChannel<level::ERR>(params, "networkd returned an invalid vlan", |
| 1078 | entry("VLAN=%" PRIu32, vlan)); |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 1079 | elog<InternalFailure>(); |
| 1080 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1081 | return vlan; |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 1082 | } |
| 1083 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1084 | /** @brief Deletes all of the possible configuration parameters for a channel |
| 1085 | * |
| 1086 | * @param[in] bus - The bus object used for lookups |
| 1087 | * @param[in] params - The parameters for the channel |
| 1088 | */ |
| 1089 | void deconfigureChannel(sdbusplus::bus::bus& bus, ChannelParams& params) |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1090 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1091 | // Delete all objects associated with the interface |
| 1092 | auto objreq = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_INTF, |
| 1093 | "GetSubTree"); |
| 1094 | objreq.append(PATH_ROOT, 0, std::vector<std::string>{DELETE_INTERFACE}); |
| 1095 | auto objreply = bus.call(objreq); |
| 1096 | ObjectTree objs; |
| 1097 | objreply.read(objs); |
| 1098 | for (const auto& [path, impls] : objs) |
| 1099 | { |
| 1100 | if (path.find(params.ifname) == path.npos) |
| 1101 | { |
| 1102 | continue; |
| 1103 | } |
| 1104 | for (const auto& [service, intfs] : impls) |
| 1105 | { |
| 1106 | deleteObjectIfExists(bus, service, path); |
| 1107 | } |
| 1108 | // Update params to reflect the deletion of vlan |
| 1109 | if (path == params.logicalPath) |
| 1110 | { |
| 1111 | params.logicalPath = params.ifPath; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | // Clear out any settings on the lower physical interface |
| 1116 | setDHCPProperty(bus, params, false); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1117 | } |
| 1118 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1119 | /** @brief Creates a new VLAN on the specified interface |
| 1120 | * |
| 1121 | * @param[in] bus - The bus object used for lookups |
| 1122 | * @param[in] params - The parameters for the channel |
| 1123 | * @param[in] vlan - The id of the new vlan |
| 1124 | */ |
| 1125 | void createVLAN(sdbusplus::bus::bus& bus, ChannelParams& params, uint16_t vlan) |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1126 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1127 | if (vlan == 0) |
Richard Marian Thomaiyar | 75b480b | 2019-01-22 00:20:15 +0530 | [diff] [blame] | 1128 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1129 | return; |
Richard Marian Thomaiyar | 75b480b | 2019-01-22 00:20:15 +0530 | [diff] [blame] | 1130 | } |
| 1131 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1132 | auto req = bus.new_method_call(params.service.c_str(), PATH_ROOT, |
| 1133 | INTF_VLAN_CREATE, "VLAN"); |
| 1134 | req.append(params.ifname, static_cast<uint32_t>(vlan)); |
| 1135 | auto reply = bus.call(req); |
| 1136 | sdbusplus::message::object_path newPath; |
| 1137 | reply.read(newPath); |
| 1138 | params.logicalPath = std::move(newPath); |
Richard Marian Thomaiyar | 75b480b | 2019-01-22 00:20:15 +0530 | [diff] [blame] | 1139 | } |
| 1140 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1141 | /** @brief Performs the necessary reconfiguration to change the VLAN |
| 1142 | * |
| 1143 | * @param[in] bus - The bus object used for lookups |
| 1144 | * @param[in] params - The parameters for the channel |
| 1145 | * @param[in] vlan - The new vlan id to use |
| 1146 | */ |
| 1147 | void reconfigureVLAN(sdbusplus::bus::bus& bus, ChannelParams& params, |
| 1148 | uint16_t vlan) |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1149 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1150 | // Unfortunatetly we don't have built-in functions to migrate our interface |
| 1151 | // customizations to new VLAN interfaces, or have some kind of decoupling. |
| 1152 | // We therefore must retain all of our old information, setup the new VLAN |
| 1153 | // configuration, then restore the old info. |
Nan Li | 3d0df91 | 2016-10-18 19:51:41 +0800 | [diff] [blame] | 1154 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1155 | // Save info from the old logical interface |
| 1156 | ObjectLookupCache ips(bus, params, INTF_IP); |
| 1157 | auto ifaddr4 = findIfAddr<AF_INET>(bus, params, 0, originsV4, ips); |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1158 | std::vector<IfAddr<AF_INET6>> ifaddrs6; |
| 1159 | for (uint8_t i = 0; i < MAX_IPV6_STATIC_ADDRESSES; ++i) |
| 1160 | { |
| 1161 | auto ifaddr6 = |
| 1162 | findIfAddr<AF_INET6>(bus, params, i, originsV6Static, ips); |
| 1163 | if (!ifaddr6) |
| 1164 | { |
| 1165 | break; |
| 1166 | } |
| 1167 | ifaddrs6.push_back(std::move(*ifaddr6)); |
| 1168 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1169 | auto dhcp = getDHCPProperty(bus, params); |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 1170 | ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR); |
| 1171 | auto neighbor4 = findGatewayNeighbor<AF_INET>(bus, params, neighbors); |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1172 | auto neighbor6 = findGatewayNeighbor<AF_INET6>(bus, params, neighbors); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1173 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1174 | deconfigureChannel(bus, params); |
| 1175 | createVLAN(bus, params, vlan); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1176 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1177 | // Re-establish the saved settings |
| 1178 | setDHCPProperty(bus, params, dhcp); |
| 1179 | if (ifaddr4) |
Patrick Venture | c7c1c3c | 2017-11-15 14:29:18 -0800 | [diff] [blame] | 1180 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1181 | createIfAddr<AF_INET>(bus, params, ifaddr4->address, ifaddr4->prefix); |
Patrick Venture | c7c1c3c | 2017-11-15 14:29:18 -0800 | [diff] [blame] | 1182 | } |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1183 | for (const auto& ifaddr6 : ifaddrs6) |
| 1184 | { |
| 1185 | createIfAddr<AF_INET6>(bus, params, ifaddr6.address, ifaddr6.prefix); |
| 1186 | } |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 1187 | if (neighbor4) |
| 1188 | { |
| 1189 | createNeighbor<AF_INET>(bus, params, neighbor4->ip, neighbor4->mac); |
| 1190 | } |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1191 | if (neighbor6) |
| 1192 | { |
| 1193 | createNeighbor<AF_INET6>(bus, params, neighbor6->ip, neighbor6->mac); |
| 1194 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1195 | } |
Patrick Venture | c7c1c3c | 2017-11-15 14:29:18 -0800 | [diff] [blame] | 1196 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1197 | /** @brief Turns a prefix into a netmask |
| 1198 | * |
| 1199 | * @param[in] prefix - The prefix length |
| 1200 | * @return The netmask |
| 1201 | */ |
| 1202 | in_addr prefixToNetmask(uint8_t prefix) |
| 1203 | { |
| 1204 | if (prefix > 32) |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1205 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1206 | log<level::ERR>("Invalid prefix", entry("PREFIX=%" PRIu8, prefix)); |
| 1207 | elog<InternalFailure>(); |
| 1208 | } |
| 1209 | if (prefix == 0) |
| 1210 | { |
| 1211 | // Avoids 32-bit lshift by 32 UB |
| 1212 | return {}; |
| 1213 | } |
| 1214 | return {htobe32(~UINT32_C(0) << (32 - prefix))}; |
| 1215 | } |
| 1216 | |
| 1217 | /** @brief Turns a a netmask into a prefix length |
| 1218 | * |
| 1219 | * @param[in] netmask - The netmask in byte form |
| 1220 | * @return The prefix length |
| 1221 | */ |
| 1222 | uint8_t netmaskToPrefix(in_addr netmask) |
| 1223 | { |
| 1224 | uint32_t x = be32toh(netmask.s_addr); |
| 1225 | if ((~x & (~x + 1)) != 0) |
| 1226 | { |
| 1227 | char maskStr[INET_ADDRSTRLEN]; |
| 1228 | inet_ntop(AF_INET, &netmask, maskStr, sizeof(maskStr)); |
| 1229 | log<level::ERR>("Invalid netmask", entry("NETMASK=%s", maskStr)); |
| 1230 | elog<InternalFailure>(); |
| 1231 | } |
Johnathan Mantey | 62c05dd | 2019-11-20 14:07:44 -0800 | [diff] [blame] | 1232 | return static_cast<bool>(x) |
| 1233 | ? AddrFamily<AF_INET>::defaultPrefix - __builtin_ctz(x) |
| 1234 | : 0; |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | // We need to store this value so it can be returned to the client |
| 1238 | // It is volatile so safe to store in daemon memory. |
| 1239 | static std::unordered_map<uint8_t, SetStatus> setStatus; |
| 1240 | |
| 1241 | // Until we have good support for fixed versions of IPMI tool |
| 1242 | // we need to return the VLAN id for disabled VLANs. The value is only |
| 1243 | // used for verification that a disable operation succeeded and will only |
| 1244 | // be sent if our system indicates that vlans are disabled. |
| 1245 | static std::unordered_map<uint8_t, uint16_t> lastDisabledVlan; |
| 1246 | |
| 1247 | /** @brief Gets the set status for the channel if it exists |
| 1248 | * Otherise populates and returns the default value. |
| 1249 | * |
| 1250 | * @param[in] channel - The channel id corresponding to an ethernet interface |
| 1251 | * @return A reference to the SetStatus for the channel |
| 1252 | */ |
| 1253 | SetStatus& getSetStatus(uint8_t channel) |
| 1254 | { |
| 1255 | auto it = setStatus.find(channel); |
| 1256 | if (it != setStatus.end()) |
| 1257 | { |
| 1258 | return it->second; |
| 1259 | } |
| 1260 | return setStatus[channel] = SetStatus::Complete; |
| 1261 | } |
| 1262 | |
Johnathan Mantey | b87034e | 2019-09-16 10:50:50 -0700 | [diff] [blame] | 1263 | /** |
| 1264 | * Define placeholder command handlers for the OEM Extension bytes for the Set |
| 1265 | * LAN Configuration Parameters and Get LAN Configuration Parameters |
| 1266 | * commands. Using "weak" linking allows the placeholder setLanOem/getLanOem |
| 1267 | * functions below to be overridden. |
| 1268 | * To create handlers for your own proprietary command set: |
| 1269 | * Create/modify a phosphor-ipmi-host Bitbake append file within your Yocto |
| 1270 | * recipe |
| 1271 | * Create C++ file(s) that define IPMI handler functions matching the |
| 1272 | * function names below (i.e. setLanOem). The default name for the |
| 1273 | * transport IPMI commands is transporthandler_oem.cpp. |
| 1274 | * Add: |
| 1275 | * EXTRA_OECONF_append = " --enable-transport-oem=yes" |
| 1276 | * Create a do_compile_prepend()/do_install_append method in your |
| 1277 | * bbappend file to copy the file to the build directory. |
| 1278 | * Add: |
| 1279 | * PROJECT_SRC_DIR := "${THISDIR}/${PN}" |
| 1280 | * # Copy the "strong" functions into the working directory, overriding the |
| 1281 | * # placeholder functions. |
| 1282 | * do_compile_prepend(){ |
| 1283 | * cp -f ${PROJECT_SRC_DIR}/transporthandler_oem.cpp ${S} |
| 1284 | * } |
| 1285 | * |
| 1286 | * # Clean up after complilation has completed |
| 1287 | * do_install_append(){ |
| 1288 | * rm -f ${S}/transporthandler_oem.cpp |
| 1289 | * } |
| 1290 | * |
| 1291 | */ |
| 1292 | |
| 1293 | /** |
| 1294 | * Define the placeholder OEM commands as having weak linkage. Create |
| 1295 | * setLanOem, and getLanOem functions in the transporthandler_oem.cpp |
| 1296 | * file. The functions defined there must not have the "weak" attribute |
| 1297 | * applied to them. |
| 1298 | */ |
| 1299 | RspType<> setLanOem(uint8_t channel, uint8_t parameter, message::Payload& req) |
| 1300 | __attribute__((weak)); |
| 1301 | RspType<message::Payload> getLanOem(uint8_t channel, uint8_t parameter, |
| 1302 | uint8_t set, uint8_t block) |
| 1303 | __attribute__((weak)); |
| 1304 | |
| 1305 | RspType<> setLanOem(uint8_t channel, uint8_t parameter, message::Payload& req) |
| 1306 | { |
| 1307 | req.trailingOk = true; |
| 1308 | return response(ccParamNotSupported); |
| 1309 | } |
| 1310 | |
| 1311 | RspType<message::Payload> getLanOem(uint8_t channel, uint8_t parameter, |
| 1312 | uint8_t set, uint8_t block) |
| 1313 | { |
| 1314 | return response(ccParamNotSupported); |
| 1315 | } |
Rajashekar Gade Reddy | 0b993fd | 2019-12-24 16:37:15 +0530 | [diff] [blame] | 1316 | /** |
| 1317 | * @brief is MAC address valid. |
| 1318 | * |
| 1319 | * This function checks whether the MAC address is valid or not. |
| 1320 | * |
| 1321 | * @param[in] mac - MAC address. |
| 1322 | * @return true if MAC address is valid else retun false. |
| 1323 | **/ |
| 1324 | bool isValidMACAddress(const ether_addr& mac) |
| 1325 | { |
| 1326 | // check if mac address is empty |
| 1327 | if (equal(mac, ether_addr{})) |
| 1328 | { |
| 1329 | return false; |
| 1330 | } |
| 1331 | // we accept only unicast MAC addresses and same thing has been checked in |
| 1332 | // phosphor-network layer. If the least significant bit of the first octet |
| 1333 | // is set to 1, it is multicast MAC else it is unicast MAC address. |
| 1334 | if (mac.ether_addr_octet[0] & 1) |
| 1335 | { |
| 1336 | return false; |
| 1337 | } |
| 1338 | return true; |
| 1339 | } |
Johnathan Mantey | b87034e | 2019-09-16 10:50:50 -0700 | [diff] [blame] | 1340 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1341 | RspType<> setLan(uint4_t channelBits, uint4_t, uint8_t parameter, |
| 1342 | message::Payload& req) |
| 1343 | { |
| 1344 | auto channel = static_cast<uint8_t>(channelBits); |
| 1345 | if (!doesDeviceExist(channel)) |
| 1346 | { |
| 1347 | req.trailingOk = true; |
| 1348 | return responseInvalidFieldRequest(); |
| 1349 | } |
| 1350 | |
| 1351 | switch (static_cast<LanParam>(parameter)) |
| 1352 | { |
| 1353 | case LanParam::SetStatus: |
| 1354 | { |
| 1355 | uint2_t flag; |
| 1356 | uint6_t rsvd; |
| 1357 | if (req.unpack(flag, rsvd) != 0 || !req.fullyUnpacked()) |
| 1358 | { |
| 1359 | return responseReqDataLenInvalid(); |
| 1360 | } |
Johnathan Mantey | 4a15685 | 2019-12-11 13:47:43 -0800 | [diff] [blame] | 1361 | if (rsvd) |
| 1362 | { |
| 1363 | return responseInvalidFieldRequest(); |
| 1364 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1365 | auto status = static_cast<SetStatus>(static_cast<uint8_t>(flag)); |
| 1366 | switch (status) |
| 1367 | { |
| 1368 | case SetStatus::Complete: |
| 1369 | { |
| 1370 | getSetStatus(channel) = status; |
| 1371 | return responseSuccess(); |
| 1372 | } |
| 1373 | case SetStatus::InProgress: |
| 1374 | { |
| 1375 | auto& storedStatus = getSetStatus(channel); |
| 1376 | if (storedStatus == SetStatus::InProgress) |
| 1377 | { |
| 1378 | return response(ccParamSetLocked); |
| 1379 | } |
| 1380 | storedStatus = status; |
| 1381 | return responseSuccess(); |
| 1382 | } |
| 1383 | case SetStatus::Commit: |
| 1384 | if (getSetStatus(channel) != SetStatus::InProgress) |
| 1385 | { |
| 1386 | return responseInvalidFieldRequest(); |
| 1387 | } |
| 1388 | return responseSuccess(); |
| 1389 | } |
| 1390 | return response(ccParamNotSupported); |
| 1391 | } |
| 1392 | case LanParam::AuthSupport: |
| 1393 | { |
| 1394 | req.trailingOk = true; |
| 1395 | return response(ccParamReadOnly); |
| 1396 | } |
| 1397 | case LanParam::AuthEnables: |
| 1398 | { |
| 1399 | req.trailingOk = true; |
Johnathan Mantey | 76ce9c7 | 2019-11-14 14:41:46 -0800 | [diff] [blame] | 1400 | return response(ccParamReadOnly); |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1401 | } |
William A. Kennington III | aab2023 | 2018-11-19 18:20:39 -0800 | [diff] [blame] | 1402 | case LanParam::IP: |
Hariharasubramanian R | 8395191 | 2016-01-20 07:06:36 -0600 | [diff] [blame] | 1403 | { |
Johnathan Mantey | 930104a | 2019-12-17 09:18:34 -0800 | [diff] [blame] | 1404 | if (channelCall<getDHCPProperty>(channel)) |
| 1405 | { |
| 1406 | return responseCommandNotAvailable(); |
| 1407 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1408 | in_addr ip; |
| 1409 | std::array<uint8_t, sizeof(ip)> bytes; |
| 1410 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
| 1411 | { |
| 1412 | return responseReqDataLenInvalid(); |
| 1413 | } |
| 1414 | copyInto(ip, bytes); |
| 1415 | channelCall<reconfigureIfAddr4>(channel, ip, std::nullopt); |
| 1416 | return responseSuccess(); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1417 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1418 | case LanParam::IPSrc: |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 1419 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1420 | uint4_t flag; |
| 1421 | uint4_t rsvd; |
| 1422 | if (req.unpack(flag, rsvd) != 0 || !req.fullyUnpacked()) |
| 1423 | { |
| 1424 | return responseReqDataLenInvalid(); |
| 1425 | } |
Johnathan Mantey | 4a15685 | 2019-12-11 13:47:43 -0800 | [diff] [blame] | 1426 | if (rsvd) |
| 1427 | { |
| 1428 | return responseInvalidFieldRequest(); |
| 1429 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1430 | switch (static_cast<IPSrc>(static_cast<uint8_t>(flag))) |
| 1431 | { |
| 1432 | case IPSrc::DHCP: |
| 1433 | { |
| 1434 | channelCall<setDHCPProperty>(channel, true); |
| 1435 | return responseSuccess(); |
| 1436 | } |
| 1437 | case IPSrc::Unspecified: |
| 1438 | case IPSrc::Static: |
| 1439 | case IPSrc::BIOS: |
| 1440 | case IPSrc::BMC: |
| 1441 | { |
| 1442 | channelCall<setDHCPProperty>(channel, false); |
| 1443 | return responseSuccess(); |
| 1444 | } |
| 1445 | } |
| 1446 | return response(ccParamNotSupported); |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 1447 | } |
William A. Kennington III | aab2023 | 2018-11-19 18:20:39 -0800 | [diff] [blame] | 1448 | case LanParam::MAC: |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1449 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1450 | ether_addr mac; |
| 1451 | std::array<uint8_t, sizeof(mac)> bytes; |
| 1452 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
Suryakanth Sekar | 0a327e1 | 2019-08-08 14:30:19 +0530 | [diff] [blame] | 1453 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1454 | return responseReqDataLenInvalid(); |
Suryakanth Sekar | 0a327e1 | 2019-08-08 14:30:19 +0530 | [diff] [blame] | 1455 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1456 | copyInto(mac, bytes); |
Rajashekar Gade Reddy | 0b993fd | 2019-12-24 16:37:15 +0530 | [diff] [blame] | 1457 | |
| 1458 | if (!isValidMACAddress(mac)) |
| 1459 | { |
| 1460 | return responseInvalidFieldRequest(); |
| 1461 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1462 | channelCall<setMACProperty>(channel, mac); |
| 1463 | return responseSuccess(); |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 1464 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1465 | case LanParam::SubnetMask: |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1466 | { |
Johnathan Mantey | 930104a | 2019-12-17 09:18:34 -0800 | [diff] [blame] | 1467 | if (channelCall<getDHCPProperty>(channel)) |
| 1468 | { |
| 1469 | return responseCommandNotAvailable(); |
| 1470 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1471 | in_addr netmask; |
| 1472 | std::array<uint8_t, sizeof(netmask)> bytes; |
| 1473 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1474 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1475 | return responseReqDataLenInvalid(); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1476 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1477 | copyInto(netmask, bytes); |
| 1478 | channelCall<reconfigureIfAddr4>(channel, std::nullopt, |
| 1479 | netmaskToPrefix(netmask)); |
| 1480 | return responseSuccess(); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1481 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1482 | case LanParam::Gateway1: |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1483 | { |
Johnathan Mantey | 930104a | 2019-12-17 09:18:34 -0800 | [diff] [blame] | 1484 | if (channelCall<getDHCPProperty>(channel)) |
| 1485 | { |
| 1486 | return responseCommandNotAvailable(); |
| 1487 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1488 | in_addr gateway; |
| 1489 | std::array<uint8_t, sizeof(gateway)> bytes; |
| 1490 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
| 1491 | { |
| 1492 | return responseReqDataLenInvalid(); |
| 1493 | } |
| 1494 | copyInto(gateway, bytes); |
| 1495 | channelCall<setGatewayProperty<AF_INET>>(channel, gateway); |
| 1496 | return responseSuccess(); |
| 1497 | } |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 1498 | case LanParam::Gateway1MAC: |
| 1499 | { |
| 1500 | ether_addr gatewayMAC; |
| 1501 | std::array<uint8_t, sizeof(gatewayMAC)> bytes; |
| 1502 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
| 1503 | { |
| 1504 | return responseReqDataLenInvalid(); |
| 1505 | } |
| 1506 | copyInto(gatewayMAC, bytes); |
| 1507 | channelCall<reconfigureGatewayMAC<AF_INET>>(channel, gatewayMAC); |
| 1508 | return responseSuccess(); |
| 1509 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1510 | case LanParam::VLANId: |
| 1511 | { |
Suryakanth Sekar | 8e8c8e2 | 2019-08-30 11:54:20 +0530 | [diff] [blame] | 1512 | uint12_t vlanData = 0; |
| 1513 | uint3_t reserved = 0; |
| 1514 | bool vlanEnable = 0; |
| 1515 | |
| 1516 | if (req.unpack(vlanData) || req.unpack(reserved) || |
| 1517 | req.unpack(vlanEnable) || !req.fullyUnpacked()) |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1518 | { |
| 1519 | return responseReqDataLenInvalid(); |
| 1520 | } |
Suryakanth Sekar | 8e8c8e2 | 2019-08-30 11:54:20 +0530 | [diff] [blame] | 1521 | |
| 1522 | if (reserved) |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1523 | { |
Suryakanth Sekar | 8e8c8e2 | 2019-08-30 11:54:20 +0530 | [diff] [blame] | 1524 | return responseInvalidFieldRequest(); |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1525 | } |
Suryakanth Sekar | 8e8c8e2 | 2019-08-30 11:54:20 +0530 | [diff] [blame] | 1526 | |
| 1527 | uint16_t vlan = static_cast<uint16_t>(vlanData); |
| 1528 | |
| 1529 | if (!vlanEnable) |
| 1530 | { |
| 1531 | lastDisabledVlan[channel] = vlan; |
| 1532 | vlan = 0; |
| 1533 | } |
| 1534 | channelCall<reconfigureVLAN>(channel, vlan); |
| 1535 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1536 | return responseSuccess(); |
| 1537 | } |
| 1538 | case LanParam::CiphersuiteSupport: |
| 1539 | case LanParam::CiphersuiteEntries: |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1540 | case LanParam::IPFamilySupport: |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1541 | { |
| 1542 | req.trailingOk = true; |
| 1543 | return response(ccParamReadOnly); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1544 | } |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1545 | case LanParam::IPFamilyEnables: |
| 1546 | { |
| 1547 | uint8_t enables; |
| 1548 | if (req.unpack(enables) != 0 || !req.fullyUnpacked()) |
| 1549 | { |
| 1550 | return responseReqDataLenInvalid(); |
| 1551 | } |
| 1552 | switch (static_cast<IPFamilyEnables>(enables)) |
| 1553 | { |
| 1554 | case IPFamilyEnables::DualStack: |
| 1555 | return responseSuccess(); |
| 1556 | case IPFamilyEnables::IPv4Only: |
| 1557 | case IPFamilyEnables::IPv6Only: |
| 1558 | return response(ccParamNotSupported); |
| 1559 | } |
| 1560 | return response(ccParamNotSupported); |
| 1561 | } |
| 1562 | case LanParam::IPv6Status: |
| 1563 | { |
| 1564 | req.trailingOk = true; |
| 1565 | return response(ccParamReadOnly); |
| 1566 | } |
| 1567 | case LanParam::IPv6StaticAddresses: |
| 1568 | { |
| 1569 | uint8_t set; |
| 1570 | uint7_t rsvd; |
| 1571 | bool enabled; |
| 1572 | in6_addr ip; |
| 1573 | std::array<uint8_t, sizeof(ip)> ipbytes; |
| 1574 | uint8_t prefix; |
| 1575 | uint8_t status; |
| 1576 | if (req.unpack(set, rsvd, enabled, ipbytes, prefix, status) != 0 || |
| 1577 | !req.fullyUnpacked()) |
| 1578 | { |
| 1579 | return responseReqDataLenInvalid(); |
| 1580 | } |
Johnathan Mantey | 4a15685 | 2019-12-11 13:47:43 -0800 | [diff] [blame] | 1581 | if (rsvd) |
| 1582 | { |
| 1583 | return responseInvalidFieldRequest(); |
| 1584 | } |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1585 | copyInto(ip, ipbytes); |
| 1586 | if (enabled) |
| 1587 | { |
| 1588 | channelCall<reconfigureIfAddr6>(channel, set, ip, prefix); |
| 1589 | } |
| 1590 | else |
| 1591 | { |
| 1592 | channelCall<deconfigureIfAddr6>(channel, set); |
| 1593 | } |
| 1594 | return responseSuccess(); |
| 1595 | } |
| 1596 | case LanParam::IPv6DynamicAddresses: |
| 1597 | { |
| 1598 | req.trailingOk = true; |
| 1599 | return response(ccParamReadOnly); |
| 1600 | } |
| 1601 | case LanParam::IPv6RouterControl: |
| 1602 | { |
| 1603 | std::bitset<8> control; |
| 1604 | if (req.unpack(control) != 0 || !req.fullyUnpacked()) |
| 1605 | { |
| 1606 | return responseReqDataLenInvalid(); |
| 1607 | } |
| 1608 | std::bitset<8> expected; |
| 1609 | if (channelCall<getDHCPProperty>(channel)) |
| 1610 | { |
| 1611 | expected[IPv6RouterControlFlag::Dynamic] = 1; |
| 1612 | } |
| 1613 | else |
| 1614 | { |
| 1615 | expected[IPv6RouterControlFlag::Static] = 1; |
| 1616 | } |
| 1617 | if (expected != control) |
| 1618 | { |
| 1619 | return responseInvalidFieldRequest(); |
| 1620 | } |
| 1621 | return responseSuccess(); |
| 1622 | } |
| 1623 | case LanParam::IPv6StaticRouter1IP: |
| 1624 | { |
| 1625 | in6_addr gateway; |
| 1626 | std::array<uint8_t, sizeof(gateway)> bytes; |
| 1627 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
| 1628 | { |
| 1629 | return responseReqDataLenInvalid(); |
| 1630 | } |
| 1631 | copyInto(gateway, bytes); |
| 1632 | channelCall<setGatewayProperty<AF_INET6>>(channel, gateway); |
| 1633 | return responseSuccess(); |
| 1634 | } |
| 1635 | case LanParam::IPv6StaticRouter1MAC: |
| 1636 | { |
| 1637 | ether_addr mac; |
| 1638 | std::array<uint8_t, sizeof(mac)> bytes; |
| 1639 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
| 1640 | { |
| 1641 | return responseReqDataLenInvalid(); |
| 1642 | } |
| 1643 | copyInto(mac, bytes); |
| 1644 | channelCall<reconfigureGatewayMAC<AF_INET6>>(channel, mac); |
| 1645 | return responseSuccess(); |
| 1646 | } |
| 1647 | case LanParam::IPv6StaticRouter1PrefixLength: |
| 1648 | { |
| 1649 | uint8_t prefix; |
| 1650 | if (req.unpack(prefix) != 0 || !req.fullyUnpacked()) |
| 1651 | { |
| 1652 | return responseReqDataLenInvalid(); |
| 1653 | } |
| 1654 | if (prefix != 0) |
| 1655 | { |
| 1656 | return responseInvalidFieldRequest(); |
| 1657 | } |
| 1658 | return responseSuccess(); |
| 1659 | } |
| 1660 | case LanParam::IPv6StaticRouter1PrefixValue: |
| 1661 | { |
| 1662 | std::array<uint8_t, sizeof(in6_addr)> bytes; |
| 1663 | if (req.unpack(bytes) != 0 || !req.fullyUnpacked()) |
| 1664 | { |
| 1665 | return responseReqDataLenInvalid(); |
| 1666 | } |
| 1667 | // Accept any prefix value since our prefix length has to be 0 |
| 1668 | return responseSuccess(); |
| 1669 | } |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1670 | } |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 1671 | |
Johnathan Mantey | b87034e | 2019-09-16 10:50:50 -0700 | [diff] [blame] | 1672 | if ((parameter >= oemCmdStart) && (parameter <= oemCmdEnd)) |
| 1673 | { |
| 1674 | return setLanOem(channel, parameter, req); |
| 1675 | } |
| 1676 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1677 | req.trailingOk = true; |
| 1678 | return response(ccParamNotSupported); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1679 | } |
| 1680 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1681 | RspType<message::Payload> getLan(uint4_t channelBits, uint3_t, bool revOnly, |
| 1682 | uint8_t parameter, uint8_t set, uint8_t block) |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1683 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1684 | message::Payload ret; |
| 1685 | constexpr uint8_t current_revision = 0x11; |
| 1686 | ret.pack(current_revision); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1687 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1688 | if (revOnly) |
Suryakanth Sekar | e405440 | 2019-08-08 15:16:52 +0530 | [diff] [blame] | 1689 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1690 | return responseSuccess(std::move(ret)); |
Suryakanth Sekar | e405440 | 2019-08-08 15:16:52 +0530 | [diff] [blame] | 1691 | } |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1692 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1693 | auto channel = static_cast<uint8_t>(channelBits); |
| 1694 | if (!doesDeviceExist(channel)) |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1695 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1696 | return responseInvalidFieldRequest(); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1697 | } |
| 1698 | |
Johnathan Mantey | affadb5 | 2019-10-07 10:13:53 -0700 | [diff] [blame] | 1699 | static std::vector<uint8_t> cipherList; |
| 1700 | static bool listInit = false; |
| 1701 | if (!listInit) |
| 1702 | { |
| 1703 | try |
| 1704 | { |
| 1705 | cipherList = cipher::getCipherList(); |
| 1706 | listInit = true; |
| 1707 | } |
| 1708 | catch (const std::exception& e) |
| 1709 | { |
| 1710 | } |
| 1711 | } |
| 1712 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1713 | switch (static_cast<LanParam>(parameter)) |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 1714 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1715 | case LanParam::SetStatus: |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 1716 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1717 | SetStatus status; |
| 1718 | try |
| 1719 | { |
| 1720 | status = setStatus.at(channel); |
| 1721 | } |
| 1722 | catch (const std::out_of_range&) |
| 1723 | { |
| 1724 | status = SetStatus::Complete; |
| 1725 | } |
| 1726 | ret.pack(static_cast<uint2_t>(status), uint6_t{}); |
| 1727 | return responseSuccess(std::move(ret)); |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 1728 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1729 | case LanParam::AuthSupport: |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 1730 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1731 | std::bitset<6> support; |
| 1732 | ret.pack(support, uint2_t{}); |
| 1733 | return responseSuccess(std::move(ret)); |
Tom Joseph | a30c8d3 | 2018-03-22 02:15:03 +0530 | [diff] [blame] | 1734 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1735 | case LanParam::AuthEnables: |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 1736 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1737 | std::bitset<6> enables; |
| 1738 | ret.pack(enables, uint2_t{}); // Callback |
| 1739 | ret.pack(enables, uint2_t{}); // User |
| 1740 | ret.pack(enables, uint2_t{}); // Operator |
| 1741 | ret.pack(enables, uint2_t{}); // Admin |
| 1742 | ret.pack(enables, uint2_t{}); // OEM |
| 1743 | return responseSuccess(std::move(ret)); |
William A. Kennington III | 39f94ef | 2018-11-19 22:36:16 -0800 | [diff] [blame] | 1744 | } |
William A. Kennington III | aab2023 | 2018-11-19 18:20:39 -0800 | [diff] [blame] | 1745 | case LanParam::IP: |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1746 | { |
| 1747 | auto ifaddr = channelCall<getIfAddr4>(channel); |
| 1748 | in_addr addr{}; |
| 1749 | if (ifaddr) |
| 1750 | { |
| 1751 | addr = ifaddr->address; |
| 1752 | } |
| 1753 | ret.pack(dataRef(addr)); |
| 1754 | return responseSuccess(std::move(ret)); |
| 1755 | } |
| 1756 | case LanParam::IPSrc: |
| 1757 | { |
| 1758 | auto src = IPSrc::Static; |
| 1759 | if (channelCall<getDHCPProperty>(channel)) |
| 1760 | { |
| 1761 | src = IPSrc::DHCP; |
| 1762 | } |
| 1763 | ret.pack(static_cast<uint4_t>(src), uint4_t{}); |
| 1764 | return responseSuccess(std::move(ret)); |
| 1765 | } |
William A. Kennington III | aab2023 | 2018-11-19 18:20:39 -0800 | [diff] [blame] | 1766 | case LanParam::MAC: |
William A. Kennington III | 39f94ef | 2018-11-19 22:36:16 -0800 | [diff] [blame] | 1767 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1768 | ether_addr mac = channelCall<getMACProperty>(channel); |
| 1769 | ret.pack(dataRef(mac)); |
| 1770 | return responseSuccess(std::move(ret)); |
| 1771 | } |
| 1772 | case LanParam::SubnetMask: |
| 1773 | { |
| 1774 | auto ifaddr = channelCall<getIfAddr4>(channel); |
| 1775 | uint8_t prefix = AddrFamily<AF_INET>::defaultPrefix; |
| 1776 | if (ifaddr) |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1777 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1778 | prefix = ifaddr->prefix; |
| 1779 | } |
| 1780 | in_addr netmask = prefixToNetmask(prefix); |
| 1781 | ret.pack(dataRef(netmask)); |
| 1782 | return responseSuccess(std::move(ret)); |
| 1783 | } |
| 1784 | case LanParam::Gateway1: |
| 1785 | { |
| 1786 | auto gateway = |
| 1787 | channelCall<getGatewayProperty<AF_INET>>(channel).value_or( |
| 1788 | in_addr{}); |
| 1789 | ret.pack(dataRef(gateway)); |
| 1790 | return responseSuccess(std::move(ret)); |
| 1791 | } |
William A. Kennington III | 4bbc3db | 2019-04-15 00:02:10 -0700 | [diff] [blame] | 1792 | case LanParam::Gateway1MAC: |
| 1793 | { |
| 1794 | ether_addr mac{}; |
| 1795 | auto neighbor = channelCall<getGatewayNeighbor<AF_INET>>(channel); |
| 1796 | if (neighbor) |
| 1797 | { |
| 1798 | mac = neighbor->mac; |
| 1799 | } |
| 1800 | ret.pack(dataRef(mac)); |
| 1801 | return responseSuccess(std::move(ret)); |
| 1802 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1803 | case LanParam::VLANId: |
| 1804 | { |
| 1805 | uint16_t vlan = channelCall<getVLANProperty>(channel); |
| 1806 | if (vlan != 0) |
| 1807 | { |
| 1808 | vlan |= VLAN_ENABLE_FLAG; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1809 | } |
| 1810 | else |
| 1811 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1812 | vlan = lastDisabledVlan[channel]; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1813 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1814 | ret.pack(vlan); |
| 1815 | return responseSuccess(std::move(ret)); |
Adriana Kobylak | 342df10 | 2016-02-10 13:48:16 -0600 | [diff] [blame] | 1816 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1817 | case LanParam::CiphersuiteSupport: |
Johnathan Mantey | affadb5 | 2019-10-07 10:13:53 -0700 | [diff] [blame] | 1818 | { |
| 1819 | if (!listInit) |
| 1820 | { |
| 1821 | return responseUnspecifiedError(); |
| 1822 | } |
| 1823 | ret.pack(static_cast<uint8_t>(cipherList.size() - 1)); |
| 1824 | return responseSuccess(std::move(ret)); |
| 1825 | } |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1826 | case LanParam::CiphersuiteEntries: |
Johnathan Mantey | affadb5 | 2019-10-07 10:13:53 -0700 | [diff] [blame] | 1827 | { |
| 1828 | if (!listInit) |
| 1829 | { |
| 1830 | return responseUnspecifiedError(); |
| 1831 | } |
| 1832 | ret.pack(cipherList); |
| 1833 | return responseSuccess(std::move(ret)); |
| 1834 | } |
William A. Kennington III | 16064aa | 2019-04-13 17:44:53 -0700 | [diff] [blame] | 1835 | case LanParam::IPFamilySupport: |
| 1836 | { |
| 1837 | std::bitset<8> support; |
| 1838 | support[IPFamilySupportFlag::IPv6Only] = 0; |
| 1839 | support[IPFamilySupportFlag::DualStack] = 1; |
| 1840 | support[IPFamilySupportFlag::IPv6Alerts] = 1; |
| 1841 | ret.pack(support); |
| 1842 | return responseSuccess(std::move(ret)); |
| 1843 | } |
| 1844 | case LanParam::IPFamilyEnables: |
| 1845 | { |
| 1846 | ret.pack(static_cast<uint8_t>(IPFamilyEnables::DualStack)); |
| 1847 | return responseSuccess(std::move(ret)); |
| 1848 | } |
| 1849 | case LanParam::IPv6Status: |
| 1850 | { |
| 1851 | ret.pack(MAX_IPV6_STATIC_ADDRESSES); |
| 1852 | ret.pack(MAX_IPV6_DYNAMIC_ADDRESSES); |
| 1853 | std::bitset<8> support; |
| 1854 | support[IPv6StatusFlag::DHCP] = 1; |
| 1855 | support[IPv6StatusFlag::SLAAC] = 1; |
| 1856 | ret.pack(support); |
| 1857 | return responseSuccess(std::move(ret)); |
| 1858 | } |
| 1859 | case LanParam::IPv6StaticAddresses: |
| 1860 | { |
| 1861 | if (set >= MAX_IPV6_STATIC_ADDRESSES) |
| 1862 | { |
| 1863 | return responseParmOutOfRange(); |
| 1864 | } |
| 1865 | getLanIPv6Address(ret, channel, set, originsV6Static); |
| 1866 | return responseSuccess(std::move(ret)); |
| 1867 | } |
| 1868 | case LanParam::IPv6DynamicAddresses: |
| 1869 | { |
| 1870 | if (set >= MAX_IPV6_DYNAMIC_ADDRESSES) |
| 1871 | { |
| 1872 | return responseParmOutOfRange(); |
| 1873 | } |
| 1874 | getLanIPv6Address(ret, channel, set, originsV6Dynamic); |
| 1875 | return responseSuccess(std::move(ret)); |
| 1876 | } |
| 1877 | case LanParam::IPv6RouterControl: |
| 1878 | { |
| 1879 | std::bitset<8> control; |
| 1880 | if (channelCall<getDHCPProperty>(channel)) |
| 1881 | { |
| 1882 | control[IPv6RouterControlFlag::Dynamic] = 1; |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | control[IPv6RouterControlFlag::Static] = 1; |
| 1887 | } |
| 1888 | ret.pack(control); |
| 1889 | return responseSuccess(std::move(ret)); |
| 1890 | } |
| 1891 | case LanParam::IPv6StaticRouter1IP: |
| 1892 | { |
| 1893 | in6_addr gateway{}; |
| 1894 | if (!channelCall<getDHCPProperty>(channel)) |
| 1895 | { |
| 1896 | gateway = |
| 1897 | channelCall<getGatewayProperty<AF_INET6>>(channel).value_or( |
| 1898 | in6_addr{}); |
| 1899 | } |
| 1900 | ret.pack(dataRef(gateway)); |
| 1901 | return responseSuccess(std::move(ret)); |
| 1902 | } |
| 1903 | case LanParam::IPv6StaticRouter1MAC: |
| 1904 | { |
| 1905 | ether_addr mac{}; |
| 1906 | auto neighbor = channelCall<getGatewayNeighbor<AF_INET6>>(channel); |
| 1907 | if (neighbor) |
| 1908 | { |
| 1909 | mac = neighbor->mac; |
| 1910 | } |
| 1911 | ret.pack(dataRef(mac)); |
| 1912 | return responseSuccess(std::move(ret)); |
| 1913 | } |
| 1914 | case LanParam::IPv6StaticRouter1PrefixLength: |
| 1915 | { |
| 1916 | ret.pack(UINT8_C(0)); |
| 1917 | return responseSuccess(std::move(ret)); |
| 1918 | } |
| 1919 | case LanParam::IPv6StaticRouter1PrefixValue: |
| 1920 | { |
| 1921 | in6_addr prefix{}; |
| 1922 | ret.pack(dataRef(prefix)); |
| 1923 | return responseSuccess(std::move(ret)); |
| 1924 | } |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1925 | } |
| 1926 | |
Johnathan Mantey | b87034e | 2019-09-16 10:50:50 -0700 | [diff] [blame] | 1927 | if ((parameter >= oemCmdStart) && (parameter <= oemCmdEnd)) |
| 1928 | { |
| 1929 | return getLanOem(channel, parameter, set, block); |
| 1930 | } |
| 1931 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1932 | return response(ccParamNotSupported); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1933 | } |
| 1934 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1935 | } // namespace transport |
| 1936 | } // namespace ipmi |
Ratan Gupta | 1247e0b | 2018-03-07 10:47:25 +0530 | [diff] [blame] | 1937 | |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1938 | void register_netfn_transport_functions() __attribute__((constructor)); |
Ratan Gupta | 1247e0b | 2018-03-07 10:47:25 +0530 | [diff] [blame] | 1939 | |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1940 | void register_netfn_transport_functions() |
| 1941 | { |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 1942 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnTransport, |
| 1943 | ipmi::transport::cmdSetLanConfigParameters, |
| 1944 | ipmi::Privilege::Admin, ipmi::transport::setLan); |
| 1945 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnTransport, |
| 1946 | ipmi::transport::cmdGetLanConfigParameters, |
Johnathan Mantey | 34698d5 | 2019-11-19 14:47:30 -0800 | [diff] [blame] | 1947 | ipmi::Privilege::Operator, ipmi::transport::getLan); |
Adriana Kobylak | 5d6481f | 2015-10-29 21:44:55 -0500 | [diff] [blame] | 1948 | } |