Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | #include "ethernet_interface.hpp" |
| 4 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 5 | #include "config_parser.hpp" |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 6 | #include "neighbor.hpp" |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 7 | #include "network_manager.hpp" |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 8 | #include "vlan_interface.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 9 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 10 | #include <arpa/inet.h> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 11 | #include <fmt/format.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 12 | #include <linux/ethtool.h> |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 13 | #include <linux/rtnetlink.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 14 | #include <linux/sockios.h> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 15 | #include <net/if.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 16 | #include <netinet/in.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <unistd.h> |
| 20 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 21 | #include <algorithm> |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 22 | #include <filesystem> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 23 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 24 | #include <phosphor-logging/elog-errors.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 26 | #include <sdbusplus/bus/match.hpp> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 27 | #include <sstream> |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 28 | #include <stdplus/raw.hpp> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 29 | #include <string> |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 30 | #include <string_view> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 31 | #include <unordered_map> |
| 32 | #include <variant> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 33 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 34 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 35 | namespace phosphor |
| 36 | { |
| 37 | namespace network |
| 38 | { |
| 39 | |
| 40 | using namespace phosphor::logging; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 41 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 42 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 43 | using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 44 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 45 | constexpr auto RESOLVED_SERVICE = "org.freedesktop.resolve1"; |
| 46 | constexpr auto RESOLVED_INTERFACE = "org.freedesktop.resolve1.Link"; |
| 47 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 48 | constexpr auto RESOLVED_SERVICE_PATH = "/org/freedesktop/resolve1/link/"; |
| 49 | constexpr auto METHOD_GET = "Get"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 50 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 51 | struct EthernetIntfSocket |
| 52 | { |
| 53 | EthernetIntfSocket(int domain, int type, int protocol) |
| 54 | { |
| 55 | if ((sock = socket(domain, type, protocol)) < 0) |
| 56 | { |
| 57 | log<level::ERR>("socket creation failed:", |
| 58 | entry("ERROR=%s", strerror(errno))); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | ~EthernetIntfSocket() |
| 63 | { |
| 64 | if (sock >= 0) |
| 65 | { |
| 66 | close(sock); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | int sock{-1}; |
| 71 | }; |
| 72 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 73 | std::map<EthernetInterface::DHCPConf, std::string> mapDHCPToSystemd = { |
| 74 | {EthernetInterface::DHCPConf::both, "true"}, |
| 75 | {EthernetInterface::DHCPConf::v4, "ipv4"}, |
| 76 | {EthernetInterface::DHCPConf::v6, "ipv6"}, |
| 77 | {EthernetInterface::DHCPConf::none, "false"}}; |
| 78 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 79 | EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus, |
| 80 | const std::string& objPath, |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 81 | DHCPConf dhcpEnabled, Manager& parent, |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 82 | bool emitSignal, |
| 83 | std::optional<bool> enabled) : |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 84 | Ifaces(bus, objPath.c_str(), true), |
| 85 | bus(bus), manager(parent), objPath(objPath) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 86 | { |
| 87 | auto intfName = objPath.substr(objPath.rfind("/") + 1); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 88 | std::replace(intfName.begin(), intfName.end(), '_', '.'); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 89 | interfaceName(intfName); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 90 | EthernetInterfaceIntf::dhcpEnabled(dhcpEnabled); |
| 91 | EthernetInterfaceIntf::ipv6AcceptRA(getIPv6AcceptRAFromConf()); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 92 | EthernetInterfaceIntf::nicEnabled(enabled ? *enabled : queryNicEnabled()); |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 93 | const auto& gatewayList = manager.getRouteTable().getDefaultGateway(); |
| 94 | const auto& gateway6List = manager.getRouteTable().getDefaultGateway6(); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 95 | std::string defaultGateway; |
| 96 | std::string defaultGateway6; |
| 97 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 98 | for (const auto& gateway : gatewayList) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 99 | { |
| 100 | if (gateway.first == intfName) |
| 101 | { |
| 102 | defaultGateway = gateway.second; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 107 | for (const auto& gateway6 : gateway6List) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 108 | { |
| 109 | if (gateway6.first == intfName) |
| 110 | { |
| 111 | defaultGateway6 = gateway6.second; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | EthernetInterfaceIntf::defaultGateway(defaultGateway); |
| 117 | EthernetInterfaceIntf::defaultGateway6(defaultGateway6); |
Ratan Gupta | 99801ce | 2020-01-09 18:37:16 +0530 | [diff] [blame] | 118 | // Don't get the mac address from the system as the mac address |
| 119 | // would be same as parent interface. |
| 120 | if (intfName.find(".") == std::string::npos) |
| 121 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 122 | MacAddressIntf::macAddress(getMACAddress(intfName)); |
Ratan Gupta | 99801ce | 2020-01-09 18:37:16 +0530 | [diff] [blame] | 123 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 124 | EthernetInterfaceIntf::ntpServers(getNTPServersFromConf()); |
Ratan Gupta | 613a012 | 2020-04-24 15:18:53 +0530 | [diff] [blame] | 125 | |
| 126 | EthernetInterfaceIntf::linkUp(linkUp()); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 127 | EthernetInterfaceIntf::mtu(mtu()); |
Ratan Gupta | 613a012 | 2020-04-24 15:18:53 +0530 | [diff] [blame] | 128 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 129 | #ifdef NIC_SUPPORTS_ETHTOOL |
Johnathan Mantey | cb42fe2 | 2019-08-01 13:35:29 -0700 | [diff] [blame] | 130 | InterfaceInfo ifInfo = EthernetInterface::getInterfaceInfo(); |
| 131 | |
| 132 | EthernetInterfaceIntf::autoNeg(std::get<2>(ifInfo)); |
| 133 | EthernetInterfaceIntf::speed(std::get<0>(ifInfo)); |
Johnathan Mantey | ad4bf5c | 2020-01-24 13:30:39 -0800 | [diff] [blame] | 134 | #endif |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 135 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 136 | // Emit deferred signal. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 137 | if (emitSignal) |
| 138 | { |
| 139 | this->emit_object_added(); |
| 140 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 141 | } |
| 142 | |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 143 | static IP::Protocol convertFamily(int family) |
| 144 | { |
| 145 | switch (family) |
| 146 | { |
| 147 | case AF_INET: |
| 148 | return IP::Protocol::IPv4; |
| 149 | case AF_INET6: |
| 150 | return IP::Protocol::IPv6; |
| 151 | } |
| 152 | |
| 153 | throw std::invalid_argument("Bad address family"); |
| 154 | } |
| 155 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 156 | void EthernetInterface::disableDHCP(IP::Protocol protocol) |
| 157 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 158 | DHCPConf dhcpState = EthernetInterfaceIntf::dhcpEnabled(); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 159 | if (dhcpState == EthernetInterface::DHCPConf::both) |
| 160 | { |
| 161 | if (protocol == IP::Protocol::IPv4) |
| 162 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 163 | dhcpEnabled(EthernetInterface::DHCPConf::v6); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 164 | } |
| 165 | else if (protocol == IP::Protocol::IPv6) |
| 166 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 167 | dhcpEnabled(EthernetInterface::DHCPConf::v4); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | else if ((dhcpState == EthernetInterface::DHCPConf::v4) && |
| 171 | (protocol == IP::Protocol::IPv4)) |
| 172 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 173 | dhcpEnabled(EthernetInterface::DHCPConf::none); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 174 | } |
| 175 | else if ((dhcpState == EthernetInterface::DHCPConf::v6) && |
| 176 | (protocol == IP::Protocol::IPv6)) |
| 177 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 178 | dhcpEnabled(EthernetInterface::DHCPConf::none); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
William A. Kennington III | 24957b9 | 2021-12-03 13:59:19 -0800 | [diff] [blame] | 182 | bool EthernetInterface::dhcpIsEnabled(IP::Protocol family) |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 183 | { |
William A. Kennington III | 24957b9 | 2021-12-03 13:59:19 -0800 | [diff] [blame] | 184 | const auto cur = EthernetInterfaceIntf::dhcpEnabled(); |
| 185 | return cur == EthernetInterface::DHCPConf::both || |
| 186 | (family == IP::Protocol::IPv6 && |
| 187 | cur == EthernetInterface::DHCPConf::v6) || |
| 188 | (family == IP::Protocol::IPv4 && |
| 189 | cur == EthernetInterface::DHCPConf::v4); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 192 | bool EthernetInterface::originIsManuallyAssigned(IP::AddressOrigin origin) |
| 193 | { |
| 194 | return ( |
| 195 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
| 196 | (origin == IP::AddressOrigin::Static) |
| 197 | #else |
| 198 | (origin == IP::AddressOrigin::Static || |
| 199 | origin == IP::AddressOrigin::LinkLocal) |
| 200 | #endif |
| 201 | |
| 202 | ); |
| 203 | } |
| 204 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 205 | void EthernetInterface::createIPAddressObjects() |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 206 | { |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 207 | addrs.clear(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 208 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 209 | auto addrs = getInterfaceAddrs()[interfaceName()]; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 210 | |
Ratan Gupta | 6a387c1 | 2017-08-03 13:26:19 +0530 | [diff] [blame] | 211 | for (auto& addr : addrs) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 212 | { |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 213 | IP::Protocol addressType = convertFamily(addr.addrType); |
| 214 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 215 | if (dhcpIsEnabled(addressType)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 216 | { |
| 217 | origin = IP::AddressOrigin::DHCP; |
| 218 | } |
William A. Kennington III | 1689380 | 2019-01-30 16:01:01 -0800 | [diff] [blame] | 219 | if (isLinkLocalIP(addr.ipaddress)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 220 | { |
| 221 | origin = IP::AddressOrigin::LinkLocal; |
| 222 | } |
William A. Kennington III | f8c78f2 | 2019-04-20 20:32:59 -0700 | [diff] [blame] | 223 | // Obsolete parameter |
| 224 | std::string gateway = ""; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 225 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 226 | std::string ipAddressObjectPath = generateObjectPath( |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 227 | addressType, addr.ipaddress, addr.prefix, gateway, origin); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 228 | |
Lei YU | 7233c58 | 2021-04-08 14:39:43 +0800 | [diff] [blame] | 229 | this->addrs.insert_or_assign( |
| 230 | addr.ipaddress, |
| 231 | std::make_shared<phosphor::network::IPAddress>( |
| 232 | bus, ipAddressObjectPath.c_str(), *this, addressType, |
| 233 | addr.ipaddress, origin, addr.prefix, gateway)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 234 | } |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 235 | } |
| 236 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 237 | void EthernetInterface::createStaticNeighborObjects() |
| 238 | { |
| 239 | staticNeighbors.clear(); |
| 240 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 241 | NeighborFilter filter; |
| 242 | filter.interface = ifIndex(); |
| 243 | filter.state = NUD_PERMANENT; |
| 244 | auto neighbors = getCurrentNeighbors(filter); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 245 | for (const auto& neighbor : neighbors) |
| 246 | { |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 247 | if (!neighbor.mac) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 248 | { |
| 249 | continue; |
| 250 | } |
| 251 | std::string ip = toString(neighbor.address); |
| 252 | std::string mac = mac_address::toString(*neighbor.mac); |
| 253 | std::string objectPath = generateStaticNeighborObjectPath(ip, mac); |
| 254 | staticNeighbors.emplace(ip, |
| 255 | std::make_shared<phosphor::network::Neighbor>( |
| 256 | bus, objectPath.c_str(), *this, ip, mac, |
| 257 | Neighbor::State::Permanent)); |
| 258 | } |
| 259 | } |
| 260 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 261 | unsigned EthernetInterface::ifIndex() const |
| 262 | { |
| 263 | unsigned idx = if_nametoindex(interfaceName().c_str()); |
| 264 | if (idx == 0) |
| 265 | { |
| 266 | throw std::system_error(errno, std::generic_category(), |
| 267 | "if_nametoindex"); |
| 268 | } |
| 269 | return idx; |
| 270 | } |
| 271 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 272 | ObjectPath EthernetInterface::ip(IP::Protocol protType, std::string ipaddress, |
raviteja-b | ce37956 | 2019-03-28 05:59:36 -0500 | [diff] [blame] | 273 | uint8_t prefixLength, std::string gateway) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 274 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 275 | if (dhcpIsEnabled(protType)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 276 | { |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 277 | log<level::INFO>("DHCP enabled on the interface"), |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 278 | entry("INTERFACE=%s", interfaceName().c_str()); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 279 | disableDHCP(protType); |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 282 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
| 283 | |
| 284 | int addressFamily = (protType == IP::Protocol::IPv4) ? AF_INET : AF_INET6; |
| 285 | |
| 286 | if (!isValidIP(addressFamily, ipaddress)) |
| 287 | { |
| 288 | log<level::ERR>("Not a valid IP address"), |
| 289 | entry("ADDRESS=%s", ipaddress.c_str()); |
| 290 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"), |
| 291 | Argument::ARGUMENT_VALUE(ipaddress.c_str())); |
| 292 | } |
| 293 | |
William A. Kennington III | f8c78f2 | 2019-04-20 20:32:59 -0700 | [diff] [blame] | 294 | // Gateway is an obsolete parameter |
| 295 | gateway = ""; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 296 | |
| 297 | if (!isValidPrefix(addressFamily, prefixLength)) |
| 298 | { |
| 299 | log<level::ERR>("PrefixLength is not correct "), |
William A. Kennington III | f8c78f2 | 2019-04-20 20:32:59 -0700 | [diff] [blame] | 300 | entry("PREFIXLENGTH=%" PRIu8, prefixLength); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 301 | elog<InvalidArgument>( |
| 302 | Argument::ARGUMENT_NAME("prefixLength"), |
| 303 | Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str())); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 304 | } |
| 305 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 306 | std::string objectPath = |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 307 | generateObjectPath(protType, ipaddress, prefixLength, gateway, origin); |
Lei YU | 7233c58 | 2021-04-08 14:39:43 +0800 | [diff] [blame] | 308 | this->addrs.insert_or_assign(ipaddress, |
| 309 | std::make_shared<phosphor::network::IPAddress>( |
| 310 | bus, objectPath.c_str(), *this, protType, |
| 311 | ipaddress, origin, prefixLength, gateway)); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 312 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 313 | writeConfigurationFile(); |
| 314 | manager.reloadConfigs(); |
| 315 | |
raviteja-b | ce37956 | 2019-03-28 05:59:36 -0500 | [diff] [blame] | 316 | return objectPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 317 | } |
| 318 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 319 | ObjectPath EthernetInterface::neighbor(std::string ipAddress, |
| 320 | std::string macAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 321 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 322 | if (!isValidIP(AF_INET, ipAddress) && !isValidIP(AF_INET6, ipAddress)) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 323 | { |
| 324 | log<level::ERR>("Not a valid IP address", |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 325 | entry("ADDRESS=%s", ipAddress.c_str())); |
| 326 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipAddress"), |
| 327 | Argument::ARGUMENT_VALUE(ipAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 328 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 329 | if (!mac_address::isUnicast(mac_address::fromString(macAddress))) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 330 | { |
| 331 | log<level::ERR>("Not a valid MAC address", |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 332 | entry("MACADDRESS=%s", ipAddress.c_str())); |
| 333 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("macAddress"), |
| 334 | Argument::ARGUMENT_VALUE(macAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | std::string objectPath = |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 338 | generateStaticNeighborObjectPath(ipAddress, macAddress); |
| 339 | staticNeighbors.emplace(ipAddress, |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 340 | std::make_shared<phosphor::network::Neighbor>( |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 341 | bus, objectPath.c_str(), *this, ipAddress, |
| 342 | macAddress, Neighbor::State::Permanent)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 343 | |
| 344 | writeConfigurationFile(); |
| 345 | manager.reloadConfigs(); |
| 346 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 347 | return objectPath; |
| 348 | } |
| 349 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 350 | #ifdef NIC_SUPPORTS_ETHTOOL |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 351 | /* |
Johnathan Mantey | ad4bf5c | 2020-01-24 13:30:39 -0800 | [diff] [blame] | 352 | Enable this code if your NIC driver supports the ETHTOOL features. |
| 353 | Do this by adding the following to your phosphor-network*.bbappend file. |
| 354 | EXTRA_OECONF_append = " --enable-nic-ethtool=yes" |
| 355 | The default compile mode is to omit getInterfaceInfo() |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 356 | */ |
| 357 | InterfaceInfo EthernetInterface::getInterfaceInfo() const |
| 358 | { |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 359 | ifreq ifr = {}; |
| 360 | ethtool_cmd edata = {}; |
| 361 | LinkSpeed speed = {}; |
| 362 | Autoneg autoneg = {}; |
| 363 | DuplexMode duplex = {}; |
| 364 | LinkUp linkState = {}; |
| 365 | NICEnabled enabled = {}; |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 366 | MTU mtuSize = {}; |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 367 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 368 | |
| 369 | if (eifSocket.sock < 0) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 370 | { |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 371 | return std::make_tuple(speed, duplex, autoneg, linkState, enabled, |
| 372 | mtuSize); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 373 | } |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 374 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 375 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IFNAMSIZ - 1); |
| 376 | ifr.ifr_data = reinterpret_cast<char*>(&edata); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 377 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 378 | edata.cmd = ETHTOOL_GSET; |
| 379 | if (ioctl(eifSocket.sock, SIOCETHTOOL, &ifr) >= 0) |
| 380 | { |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 381 | speed = edata.speed; |
| 382 | duplex = edata.duplex; |
| 383 | autoneg = edata.autoneg; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 384 | } |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 385 | |
William A. Kennington III | 9620331 | 2021-05-07 12:50:41 -0700 | [diff] [blame] | 386 | enabled = nicEnabled(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 387 | linkState = linkUp(); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 388 | mtuSize = mtu(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 389 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 390 | return std::make_tuple(speed, duplex, autoneg, linkState, enabled, mtuSize); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 391 | } |
Johnathan Mantey | ad4bf5c | 2020-01-24 13:30:39 -0800 | [diff] [blame] | 392 | #endif |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 393 | |
| 394 | /** @brief get the mac address of the interface. |
| 395 | * @return macaddress on success |
| 396 | */ |
| 397 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 398 | std::string |
| 399 | EthernetInterface::getMACAddress(const std::string& interfaceName) const |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 400 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 401 | std::string activeMACAddr = MacAddressIntf::macAddress(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 402 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 403 | |
| 404 | if (eifSocket.sock < 0) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 405 | { |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 406 | return activeMACAddr; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 407 | } |
| 408 | |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 409 | ifreq ifr = {}; |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 410 | std::strncpy(ifr.ifr_name, interfaceName.c_str(), IFNAMSIZ - 1); |
| 411 | if (ioctl(eifSocket.sock, SIOCGIFHWADDR, &ifr) != 0) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 412 | { |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 413 | log<level::ERR>("ioctl failed for SIOCGIFHWADDR:", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 414 | entry("ERROR=%s", strerror(errno))); |
William A. Kennington III | 7ed1b28 | 2019-04-21 23:38:42 -0700 | [diff] [blame] | 415 | elog<InternalFailure>(); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 416 | } |
| 417 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 418 | static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= sizeof(ether_addr)); |
| 419 | std::string_view hwaddr(reinterpret_cast<char*>(ifr.ifr_hwaddr.sa_data), |
| 420 | sizeof(ifr.ifr_hwaddr.sa_data)); |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 421 | return mac_address::toString(stdplus::raw::copyFrom<ether_addr>(hwaddr)); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 422 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 423 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 424 | std::string EthernetInterface::generateId(const std::string& ipaddress, |
| 425 | uint8_t prefixLength, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 426 | const std::string& gateway, |
| 427 | const std::string& origin) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 428 | { |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 429 | std::stringstream hexId; |
| 430 | std::string hashString = ipaddress; |
| 431 | hashString += std::to_string(prefixLength); |
| 432 | hashString += gateway; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 433 | hashString += origin; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 434 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 435 | // Only want 8 hex digits. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 436 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 437 | return hexId.str(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 438 | } |
| 439 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 440 | std::string EthernetInterface::generateNeighborId(const std::string& ipAddress, |
| 441 | const std::string& macAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 442 | { |
| 443 | std::stringstream hexId; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 444 | std::string hashString = ipAddress + macAddress; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 445 | |
| 446 | // Only want 8 hex digits. |
| 447 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
| 448 | return hexId.str(); |
| 449 | } |
| 450 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 451 | void EthernetInterface::deleteObject(const std::string& ipaddress) |
| 452 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 453 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 454 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 455 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 456 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 457 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 458 | } |
| 459 | this->addrs.erase(it); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 460 | |
| 461 | writeConfigurationFile(); |
| 462 | manager.reloadConfigs(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 463 | } |
| 464 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 465 | void EthernetInterface::deleteStaticNeighborObject(const std::string& ipAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 466 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 467 | auto it = staticNeighbors.find(ipAddress); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 468 | if (it == staticNeighbors.end()) |
| 469 | { |
| 470 | log<level::ERR>( |
| 471 | "DeleteStaticNeighborObject:Unable to find the object."); |
| 472 | return; |
| 473 | } |
| 474 | staticNeighbors.erase(it); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 475 | |
| 476 | writeConfigurationFile(); |
| 477 | manager.reloadConfigs(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 480 | void EthernetInterface::deleteVLANFromSystem(const std::string& interface) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 481 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 482 | auto confDir = manager.getConfDir(); |
| 483 | fs::path networkFile = confDir; |
| 484 | networkFile /= systemd::config::networkFilePrefix + interface + |
| 485 | systemd::config::networkFileSuffix; |
| 486 | |
| 487 | fs::path deviceFile = confDir; |
| 488 | deviceFile /= interface + systemd::config::deviceFileSuffix; |
| 489 | |
| 490 | // delete the vlan network file |
| 491 | if (fs::is_regular_file(networkFile)) |
| 492 | { |
| 493 | fs::remove(networkFile); |
| 494 | } |
| 495 | |
| 496 | // delete the vlan device file |
| 497 | if (fs::is_regular_file(deviceFile)) |
| 498 | { |
| 499 | fs::remove(deviceFile); |
| 500 | } |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 501 | |
| 502 | // TODO systemd doesn't delete the virtual network interface |
| 503 | // even after deleting all the related configuartion. |
| 504 | // https://github.com/systemd/systemd/issues/6600 |
| 505 | try |
| 506 | { |
| 507 | deleteInterface(interface); |
| 508 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 509 | catch (const InternalFailure& e) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 510 | { |
| 511 | commit<InternalFailure>(); |
| 512 | } |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void EthernetInterface::deleteVLANObject(const std::string& interface) |
| 516 | { |
| 517 | auto it = vlanInterfaces.find(interface); |
| 518 | if (it == vlanInterfaces.end()) |
| 519 | { |
| 520 | log<level::ERR>("DeleteVLANObject:Unable to find the object", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 521 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | |
| 525 | deleteVLANFromSystem(interface); |
| 526 | // delete the interface |
| 527 | vlanInterfaces.erase(it); |
| 528 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 529 | writeConfigurationFile(); |
| 530 | manager.reloadConfigs(); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 531 | } |
| 532 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 533 | std::string EthernetInterface::generateObjectPath( |
| 534 | IP::Protocol addressType, const std::string& ipaddress, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 535 | uint8_t prefixLength, const std::string& gateway, |
| 536 | IP::AddressOrigin origin) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 537 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 538 | std::string type = convertForMessage(addressType); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 539 | type = type.substr(type.rfind('.') + 1); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 540 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); |
| 541 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 542 | std::filesystem::path objectPath; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 543 | objectPath /= objPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 544 | objectPath /= type; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 545 | objectPath /= |
| 546 | generateId(ipaddress, prefixLength, gateway, convertForMessage(origin)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 547 | return objectPath.string(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 548 | } |
| 549 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 550 | std::string EthernetInterface::generateStaticNeighborObjectPath( |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 551 | const std::string& ipAddress, const std::string& macAddress) const |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 552 | { |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 553 | std::filesystem::path objectPath; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 554 | objectPath /= objPath; |
| 555 | objectPath /= "static_neighbor"; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 556 | objectPath /= generateNeighborId(ipAddress, macAddress); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 557 | return objectPath.string(); |
| 558 | } |
| 559 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 560 | bool EthernetInterface::ipv6AcceptRA(bool value) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 561 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 562 | if (value == EthernetInterfaceIntf::ipv6AcceptRA()) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 563 | { |
| 564 | return value; |
| 565 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 566 | EthernetInterfaceIntf::ipv6AcceptRA(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 567 | |
| 568 | writeConfigurationFile(); |
| 569 | manager.reloadConfigs(); |
| 570 | |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 571 | return value; |
| 572 | } |
| 573 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 574 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled(DHCPConf value) |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 575 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 576 | if (value == EthernetInterfaceIntf::dhcpEnabled()) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 577 | { |
| 578 | return value; |
| 579 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 580 | EthernetInterfaceIntf::dhcpEnabled(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 581 | |
| 582 | writeConfigurationFile(); |
| 583 | manager.reloadConfigs(); |
| 584 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 585 | return value; |
| 586 | } |
| 587 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 588 | bool EthernetInterface::linkUp() const |
| 589 | { |
| 590 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 591 | bool value = EthernetInterfaceIntf::linkUp(); |
| 592 | |
| 593 | if (eifSocket.sock < 0) |
| 594 | { |
| 595 | return value; |
| 596 | } |
| 597 | |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 598 | ifreq ifr = {}; |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 599 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
| 600 | if (ioctl(eifSocket.sock, SIOCGIFFLAGS, &ifr) == 0) |
| 601 | { |
| 602 | value = static_cast<bool>(ifr.ifr_flags & IFF_RUNNING); |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | log<level::ERR>("ioctl failed for SIOCGIFFLAGS:", |
| 607 | entry("ERROR=%s", strerror(errno))); |
| 608 | } |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 609 | return value; |
| 610 | } |
| 611 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 612 | size_t EthernetInterface::mtu() const |
| 613 | { |
| 614 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 615 | size_t value = EthernetInterfaceIntf::mtu(); |
| 616 | |
| 617 | if (eifSocket.sock < 0) |
| 618 | { |
| 619 | return value; |
| 620 | } |
| 621 | |
| 622 | ifreq ifr = {}; |
| 623 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
| 624 | if (ioctl(eifSocket.sock, SIOCGIFMTU, &ifr) == 0) |
| 625 | { |
| 626 | value = ifr.ifr_mtu; |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | log<level::ERR>("ioctl failed for SIOCGIFMTU:", |
| 631 | entry("ERROR=%s", strerror(errno))); |
| 632 | } |
| 633 | return value; |
| 634 | } |
| 635 | |
| 636 | size_t EthernetInterface::mtu(size_t value) |
| 637 | { |
| 638 | if (value == EthernetInterfaceIntf::mtu()) |
| 639 | { |
| 640 | return value; |
| 641 | } |
| 642 | else if (value == 0) |
| 643 | { |
| 644 | return EthernetInterfaceIntf::mtu(); |
| 645 | } |
| 646 | |
| 647 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 648 | if (eifSocket.sock < 0) |
| 649 | { |
| 650 | return EthernetInterfaceIntf::mtu(); |
| 651 | } |
| 652 | |
| 653 | ifreq ifr = {}; |
| 654 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
| 655 | ifr.ifr_mtu = value; |
| 656 | |
| 657 | if (ioctl(eifSocket.sock, SIOCSIFMTU, &ifr) != 0) |
| 658 | { |
| 659 | log<level::ERR>("ioctl failed for SIOCSIFMTU:", |
| 660 | entry("ERROR=%s", strerror(errno))); |
| 661 | return EthernetInterfaceIntf::mtu(); |
| 662 | } |
| 663 | EthernetInterfaceIntf::mtu(value); |
| 664 | |
| 665 | return value; |
| 666 | } |
| 667 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 668 | bool EthernetInterface::queryNicEnabled() const |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 669 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 670 | constexpr auto svc = "org.freedesktop.network1"; |
| 671 | constexpr auto intf = "org.freedesktop.network1.Link"; |
| 672 | constexpr auto prop = "AdministrativeState"; |
| 673 | char* rpath; |
| 674 | sd_bus_path_encode("/org/freedesktop/network1/link", |
| 675 | std::to_string(ifIndex()).c_str(), &rpath); |
| 676 | std::string path(rpath); |
| 677 | free(rpath); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 678 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 679 | // Store / Parser for the AdministrativeState return value |
| 680 | std::optional<bool> ret; |
| 681 | auto cb = [&](const std::string& state) { |
| 682 | if (state != "initialized") |
| 683 | { |
| 684 | ret = state != "unmanaged"; |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | // Build a matcher before making the property call to ensure we |
| 689 | // can eventually get the value. |
| 690 | sdbusplus::bus::match::match match( |
| 691 | bus, |
| 692 | fmt::format("type='signal',sender='{}',path='{}',interface='{}',member=" |
| 693 | "'PropertiesChanged',arg0='{}',", |
| 694 | svc, path, PROPERTY_INTERFACE, intf) |
| 695 | .c_str(), |
| 696 | [&](sdbusplus::message::message& m) { |
| 697 | std::string intf; |
| 698 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 699 | try |
| 700 | { |
| 701 | m.read(intf, values); |
| 702 | auto it = values.find(prop); |
| 703 | // Ignore properties that aren't AdministrativeState |
| 704 | if (it != values.end()) |
| 705 | { |
| 706 | cb(std::get<std::string>(it->second)); |
| 707 | } |
| 708 | } |
| 709 | catch (const std::exception& e) |
| 710 | { |
| 711 | log<level::ERR>( |
| 712 | fmt::format( |
| 713 | "AdministrativeState match parsing failed on {}: {}", |
| 714 | interfaceName(), e.what()) |
| 715 | .c_str(), |
| 716 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 717 | entry("ERROR=%s", e.what())); |
| 718 | } |
| 719 | }); |
| 720 | |
| 721 | // Actively call for the value in case the interface is already configured |
| 722 | auto method = |
| 723 | bus.new_method_call(svc, path.c_str(), PROPERTY_INTERFACE, METHOD_GET); |
| 724 | method.append(intf, prop); |
| 725 | try |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 726 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 727 | auto reply = bus.call(method); |
| 728 | std::variant<std::string> state; |
| 729 | reply.read(state); |
| 730 | cb(std::get<std::string>(state)); |
| 731 | } |
| 732 | catch (const std::exception& e) |
| 733 | { |
| 734 | log<level::ERR>( |
| 735 | fmt::format("Failed to get AdministrativeState on {}: {}", |
| 736 | interfaceName(), e.what()) |
| 737 | .c_str(), |
| 738 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 739 | entry("ERROR=%s", e.what())); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 740 | } |
| 741 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 742 | // The interface is not yet configured by systemd-networkd, wait until it |
| 743 | // signals us a valid state. |
| 744 | while (!ret) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 745 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 746 | bus.wait(); |
| 747 | bus.process_discard(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 748 | } |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 749 | |
| 750 | return *ret; |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 751 | } |
| 752 | |
William A. Kennington III | 4209cee | 2021-10-23 18:14:21 -0700 | [diff] [blame] | 753 | static void setNICAdminState(int fd, const char* intf, bool up) |
| 754 | { |
| 755 | ifreq ifr = {}; |
| 756 | std::strncpy(ifr.ifr_name, intf, IF_NAMESIZE - 1); |
| 757 | if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) |
| 758 | { |
| 759 | log<level::ERR>("ioctl failed for SIOCGIFFLAGS:", |
| 760 | entry("ERROR=%s", strerror(errno))); |
| 761 | elog<InternalFailure>(); |
| 762 | } |
| 763 | |
| 764 | ifr.ifr_flags &= ~IFF_UP; |
| 765 | ifr.ifr_flags |= up ? IFF_UP : 0; |
| 766 | |
| 767 | if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) |
| 768 | { |
| 769 | log<level::ERR>("ioctl failed for SIOCSIFFLAGS:", |
| 770 | entry("ERROR=%s", strerror(errno))); |
| 771 | elog<InternalFailure>(); |
| 772 | } |
| 773 | } |
| 774 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 775 | bool EthernetInterface::nicEnabled(bool value) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 776 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 777 | if (value == EthernetInterfaceIntf::nicEnabled()) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 778 | { |
| 779 | return value; |
| 780 | } |
| 781 | |
| 782 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 783 | if (eifSocket.sock < 0) |
| 784 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 785 | return EthernetInterfaceIntf::nicEnabled(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 786 | } |
William A. Kennington III | 4209cee | 2021-10-23 18:14:21 -0700 | [diff] [blame] | 787 | auto ifname = interfaceName(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 788 | |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 789 | writeConfigurationFile(); |
William A. Kennington III | 329b5fb | 2021-11-09 17:19:30 -0800 | [diff] [blame] | 790 | if (!value) |
| 791 | { |
| 792 | // We only need to bring down the interface, networkd will always bring |
| 793 | // up managed interfaces |
| 794 | manager.addReloadPreHook( |
| 795 | [ifname = std::move(ifname), eifSocket = std::move(eifSocket)]() { |
| 796 | setNICAdminState(eifSocket.sock, ifname.c_str(), false); |
| 797 | }); |
| 798 | } |
| 799 | EthernetInterfaceIntf::nicEnabled(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 800 | manager.reloadConfigs(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 801 | |
| 802 | return value; |
| 803 | } |
| 804 | |
Manojkiran Eda | aa57fa5 | 2020-06-13 14:59:53 +0530 | [diff] [blame] | 805 | ServerList EthernetInterface::nameservers(ServerList /*value*/) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 806 | { |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 807 | elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property")); |
| 808 | return EthernetInterfaceIntf::nameservers(); |
| 809 | } |
| 810 | |
| 811 | ServerList EthernetInterface::staticNameServers(ServerList value) |
| 812 | { |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 813 | for (const auto& nameserverip : value) |
| 814 | { |
| 815 | if (!isValidIP(AF_INET, nameserverip) && |
| 816 | !isValidIP(AF_INET6, nameserverip)) |
| 817 | { |
| 818 | log<level::ERR>("Not a valid IP address"), |
| 819 | entry("ADDRESS=%s", nameserverip.c_str()); |
| 820 | elog<InvalidArgument>( |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 821 | Argument::ARGUMENT_NAME("StaticNameserver"), |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 822 | Argument::ARGUMENT_VALUE(nameserverip.c_str())); |
| 823 | } |
| 824 | } |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 825 | try |
| 826 | { |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 827 | EthernetInterfaceIntf::staticNameServers(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 828 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 829 | writeConfigurationFile(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 830 | manager.reloadConfigs(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 831 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 832 | catch (const InternalFailure& e) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 833 | { |
| 834 | log<level::ERR>("Exception processing DNS entries"); |
| 835 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 836 | return EthernetInterfaceIntf::staticNameServers(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 837 | } |
| 838 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 839 | void EthernetInterface::loadNameServers() |
| 840 | { |
| 841 | EthernetInterfaceIntf::nameservers(getNameServerFromResolvd()); |
| 842 | EthernetInterfaceIntf::staticNameServers(getstaticNameServerFromConf()); |
| 843 | } |
| 844 | |
| 845 | ServerList EthernetInterface::getstaticNameServerFromConf() |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 846 | { |
| 847 | fs::path confPath = manager.getConfDir(); |
| 848 | |
| 849 | std::string fileName = systemd::config::networkFilePrefix + |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 850 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 851 | confPath /= fileName; |
| 852 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 853 | config::Parser parser(confPath.string()); |
| 854 | auto rc = config::ReturnCode::SUCCESS; |
| 855 | |
| 856 | std::tie(rc, servers) = parser.getValues("Network", "DNS"); |
| 857 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 858 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 859 | log<level::DEBUG>("Unable to get the value for network[DNS]", |
| 860 | entry("RC=%d", rc)); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 861 | } |
| 862 | return servers; |
| 863 | } |
| 864 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 865 | ServerList EthernetInterface::getNameServerFromResolvd() |
| 866 | { |
| 867 | ServerList servers; |
| 868 | std::string OBJ_PATH = RESOLVED_SERVICE_PATH + std::to_string(ifIndex()); |
| 869 | |
| 870 | /* |
| 871 | The DNS property under org.freedesktop.resolve1.Link interface contains |
| 872 | an array containing all DNS servers currently used by resolved. It |
| 873 | contains similar information as the DNS server data written to |
| 874 | /run/systemd/resolve/resolv.conf. |
| 875 | |
| 876 | Each structure in the array consists of a numeric network interface index, |
| 877 | an address family, and a byte array containing the DNS server address |
| 878 | (either 4 bytes in length for IPv4 or 16 bytes in lengths for IPv6). |
| 879 | The array contains DNS servers configured system-wide, including those |
| 880 | possibly read from a foreign /etc/resolv.conf or the DNS= setting in |
| 881 | /etc/systemd/resolved.conf, as well as per-interface DNS server |
| 882 | information either retrieved from systemd-networkd or configured by |
| 883 | external software via SetLinkDNS(). |
| 884 | */ |
| 885 | |
| 886 | using type = std::vector<std::tuple<int32_t, std::vector<uint8_t>>>; |
| 887 | std::variant<type> name; // Variable to capture the DNS property |
| 888 | auto method = bus.new_method_call(RESOLVED_SERVICE, OBJ_PATH.c_str(), |
| 889 | PROPERTY_INTERFACE, METHOD_GET); |
| 890 | |
| 891 | method.append(RESOLVED_INTERFACE, "DNS"); |
| 892 | auto reply = bus.call(method); |
| 893 | |
| 894 | try |
| 895 | { |
| 896 | reply.read(name); |
| 897 | } |
Patrick Williams | b108fd7 | 2021-09-02 09:45:39 -0500 | [diff] [blame] | 898 | catch (const sdbusplus::exception::exception& e) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 899 | { |
| 900 | log<level::ERR>("Failed to get DNS information from Systemd-Resolved"); |
| 901 | } |
| 902 | auto tupleVector = std::get_if<type>(&name); |
| 903 | for (auto i = tupleVector->begin(); i != tupleVector->end(); ++i) |
| 904 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 905 | int addressFamily = std::get<0>(*i); |
| 906 | std::vector<uint8_t>& ipaddress = std::get<1>(*i); |
| 907 | |
| 908 | switch (addressFamily) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 909 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 910 | case AF_INET: |
| 911 | if (ipaddress.size() == sizeof(struct in_addr)) |
| 912 | { |
| 913 | servers.push_back(toString( |
| 914 | *reinterpret_cast<struct in_addr*>(ipaddress.data()))); |
| 915 | } |
| 916 | else |
| 917 | { |
| 918 | log<level::ERR>( |
| 919 | "Invalid data recived from Systemd-Resolved"); |
| 920 | } |
| 921 | break; |
| 922 | |
| 923 | case AF_INET6: |
| 924 | if (ipaddress.size() == sizeof(struct in6_addr)) |
| 925 | { |
| 926 | servers.push_back(toString( |
| 927 | *reinterpret_cast<struct in6_addr*>(ipaddress.data()))); |
| 928 | } |
| 929 | else |
| 930 | { |
| 931 | log<level::ERR>( |
| 932 | "Invalid data recived from Systemd-Resolved"); |
| 933 | } |
| 934 | break; |
| 935 | |
| 936 | default: |
| 937 | log<level::ERR>( |
| 938 | "Unsupported address family in DNS from Systemd-Resolved"); |
| 939 | break; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 940 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 941 | } |
| 942 | return servers; |
| 943 | } |
| 944 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 945 | void EthernetInterface::loadVLAN(VlanId id) |
| 946 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 947 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 948 | std::string path = objPath; |
| 949 | path += "_" + std::to_string(id); |
| 950 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 951 | DHCPConf dhcpEnabled = |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 952 | getDHCPValue(manager.getConfDir().string(), vlanInterfaceName); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 953 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 954 | bus, path.c_str(), dhcpEnabled, EthernetInterfaceIntf::nicEnabled(), id, |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 955 | *this, manager); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 956 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 957 | // Fetch the ip address from the system |
| 958 | // and create the dbus object. |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 959 | vlanIntf->createIPAddressObjects(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 960 | vlanIntf->createStaticNeighborObjects(); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 961 | |
| 962 | this->vlanInterfaces.emplace(std::move(vlanInterfaceName), |
| 963 | std::move(vlanIntf)); |
| 964 | } |
| 965 | |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 966 | ObjectPath EthernetInterface::createVLAN(VlanId id) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 967 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 968 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 969 | std::string path = objPath; |
| 970 | path += "_" + std::to_string(id); |
| 971 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 972 | // Pass the parents nicEnabled property, so that the child |
Manojkiran Eda | ca8b91b | 2020-05-28 09:28:42 +0530 | [diff] [blame] | 973 | // VLAN interface can inherit. |
| 974 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 975 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 976 | bus, path.c_str(), EthernetInterface::DHCPConf::none, |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 977 | EthernetInterfaceIntf::nicEnabled(), id, *this, manager); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 978 | |
| 979 | // write the device file for the vlan interface. |
| 980 | vlanIntf->writeDeviceFile(); |
| 981 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 982 | this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 983 | |
| 984 | writeConfigurationFile(); |
| 985 | manager.reloadConfigs(); |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 986 | |
| 987 | return path; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 988 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 989 | |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 990 | bool EthernetInterface::getIPv6AcceptRAFromConf() |
| 991 | { |
| 992 | fs::path confPath = manager.getConfDir(); |
| 993 | |
| 994 | std::string fileName = systemd::config::networkFilePrefix + |
| 995 | interfaceName() + systemd::config::networkFileSuffix; |
| 996 | confPath /= fileName; |
| 997 | config::ValueList values; |
| 998 | config::Parser parser(confPath.string()); |
| 999 | auto rc = config::ReturnCode::SUCCESS; |
| 1000 | std::tie(rc, values) = parser.getValues("Network", "IPv6AcceptRA"); |
| 1001 | if (rc != config::ReturnCode::SUCCESS) |
| 1002 | { |
| 1003 | log<level::DEBUG>("Unable to get the value for Network[IPv6AcceptRA]", |
| 1004 | entry("rc=%d", rc)); |
| 1005 | return false; |
| 1006 | } |
| 1007 | return (values[0] == "true"); |
| 1008 | } |
| 1009 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1010 | ServerList EthernetInterface::getNTPServersFromConf() |
| 1011 | { |
| 1012 | fs::path confPath = manager.getConfDir(); |
| 1013 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1014 | std::string fileName = systemd::config::networkFilePrefix + |
| 1015 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1016 | confPath /= fileName; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 1017 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1018 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 1019 | config::Parser parser(confPath.string()); |
| 1020 | auto rc = config::ReturnCode::SUCCESS; |
| 1021 | |
| 1022 | std::tie(rc, servers) = parser.getValues("Network", "NTP"); |
| 1023 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1024 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 1025 | log<level::DEBUG>("Unable to get the value for Network[NTP]", |
| 1026 | entry("rc=%d", rc)); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1027 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 1028 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1029 | return servers; |
| 1030 | } |
| 1031 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1032 | ServerList EthernetInterface::ntpServers(ServerList servers) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1033 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1034 | auto ntpServers = EthernetInterfaceIntf::ntpServers(servers); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1035 | |
| 1036 | writeConfigurationFile(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1037 | manager.reloadConfigs(); |
| 1038 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 1039 | return ntpServers; |
| 1040 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1041 | // Need to merge the below function with the code which writes the |
| 1042 | // config file during factory reset. |
| 1043 | // TODO openbmc/openbmc#1751 |
| 1044 | |
| 1045 | void EthernetInterface::writeConfigurationFile() |
| 1046 | { |
| 1047 | // write all the static ip address in the systemd-network conf file |
| 1048 | |
| 1049 | using namespace std::string_literals; |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 1050 | namespace fs = std::filesystem; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 1051 | |
| 1052 | // if there is vlan interafce then write the configuration file |
| 1053 | // for vlan also. |
| 1054 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1055 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 1056 | { |
| 1057 | intf.second->writeConfigurationFile(); |
| 1058 | } |
| 1059 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1060 | fs::path confPath = manager.getConfDir(); |
| 1061 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1062 | std::string fileName = systemd::config::networkFilePrefix + |
| 1063 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1064 | confPath /= fileName; |
| 1065 | std::fstream stream; |
| 1066 | |
| 1067 | stream.open(confPath.c_str(), std::fstream::out); |
| 1068 | if (!stream.is_open()) |
| 1069 | { |
| 1070 | log<level::ERR>("Unable to open the file", |
| 1071 | entry("FILE=%s", confPath.c_str())); |
| 1072 | elog<InternalFailure>(); |
| 1073 | } |
| 1074 | |
| 1075 | // Write the device |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 1076 | stream << "[Match]\n"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1077 | stream << "Name=" << interfaceName() << "\n"; |
| 1078 | |
| 1079 | auto addrs = getAddresses(); |
| 1080 | |
William A. Kennington III | 1578721 | 2019-04-23 19:18:01 -0700 | [diff] [blame] | 1081 | // Write the link section |
| 1082 | stream << "[Link]\n"; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1083 | auto mac = MacAddressIntf::macAddress(); |
William A. Kennington III | 1578721 | 2019-04-23 19:18:01 -0700 | [diff] [blame] | 1084 | if (!mac.empty()) |
| 1085 | { |
| 1086 | stream << "MACAddress=" << mac << "\n"; |
| 1087 | } |
| 1088 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1089 | if (!EthernetInterfaceIntf::nicEnabled()) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 1090 | { |
| 1091 | stream << "Unmanaged=yes\n"; |
| 1092 | } |
| 1093 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1094 | // write the network section |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 1095 | stream << "[Network]\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 1096 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
Nagaraju Goruganti | 24afe36 | 2017-09-21 07:40:26 -0500 | [diff] [blame] | 1097 | stream << "LinkLocalAddressing=yes\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 1098 | #else |
| 1099 | stream << "LinkLocalAddressing=no\n"; |
| 1100 | #endif |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 1101 | stream << std::boolalpha |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1102 | << "IPv6AcceptRA=" << EthernetInterfaceIntf::ipv6AcceptRA() << "\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 1103 | |
| 1104 | // Add the VLAN entry |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1105 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 1106 | { |
| 1107 | stream << "VLAN=" << intf.second->EthernetInterface::interfaceName() |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1108 | << "\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 1109 | } |
Ratan Gupta | 046b2a0 | 2019-09-20 15:49:51 +0530 | [diff] [blame] | 1110 | // Add the NTP server |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1111 | for (const auto& ntp : EthernetInterfaceIntf::ntpServers()) |
Ratan Gupta | 046b2a0 | 2019-09-20 15:49:51 +0530 | [diff] [blame] | 1112 | { |
| 1113 | stream << "NTP=" << ntp << "\n"; |
| 1114 | } |
| 1115 | |
| 1116 | // Add the DNS entry |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 1117 | for (const auto& dns : EthernetInterfaceIntf::staticNameServers()) |
Ratan Gupta | 046b2a0 | 2019-09-20 15:49:51 +0530 | [diff] [blame] | 1118 | { |
| 1119 | stream << "DNS=" << dns << "\n"; |
| 1120 | } |
| 1121 | |
Nagaraju Goruganti | e8b83ec | 2018-03-26 05:21:45 -0500 | [diff] [blame] | 1122 | // Add the DHCP entry |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1123 | stream << "DHCP="s + |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1124 | mapDHCPToSystemd[EthernetInterfaceIntf::dhcpEnabled()] + "\n"; |
Nagaraju Goruganti | e8b83ec | 2018-03-26 05:21:45 -0500 | [diff] [blame] | 1125 | |
William A. Kennington III | e6d1c0e | 2021-12-03 14:00:30 -0800 | [diff] [blame] | 1126 | stream << "[IPv6AcceptRA]\n"; |
| 1127 | stream << "DHCPv6Client="; |
| 1128 | stream << (dhcpIsEnabled(IP::Protocol::IPv6) ? "true" : "false"); |
| 1129 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1130 | // Static IP addresses |
| 1131 | for (const auto& addr : addrs) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1132 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1133 | if (originIsManuallyAssigned(addr.second->origin()) && |
| 1134 | !dhcpIsEnabled(addr.second->type())) |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1135 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1136 | // Process all static addresses |
| 1137 | std::string address = addr.second->address() + "/" + |
| 1138 | std::to_string(addr.second->prefixLength()); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1139 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1140 | // build the address entries. Do not use [Network] shortcuts to |
| 1141 | // insert address entries. |
| 1142 | stream << "[Address]\n"; |
| 1143 | stream << "Address=" << address << "\n"; |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1144 | } |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1145 | } |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1146 | |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1147 | if (!dhcpIsEnabled(IP::Protocol::IPv4)) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1148 | { |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1149 | auto gateway = EthernetInterfaceIntf::defaultGateway(); |
| 1150 | if (!gateway.empty()) |
| 1151 | { |
| 1152 | stream << "[Route]\n"; |
| 1153 | stream << "Gateway=" << gateway << "\n"; |
| 1154 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1155 | } |
| 1156 | |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1157 | if (!dhcpIsEnabled(IP::Protocol::IPv6)) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1158 | { |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1159 | auto gateway6 = EthernetInterfaceIntf::defaultGateway6(); |
| 1160 | if (!gateway6.empty()) |
| 1161 | { |
| 1162 | stream << "[Route]\n"; |
| 1163 | stream << "Gateway=" << gateway6 << "\n"; |
| 1164 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1165 | } |
| 1166 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 1167 | // Write the neighbor sections |
| 1168 | for (const auto& neighbor : staticNeighbors) |
| 1169 | { |
| 1170 | stream << "[Neighbor]" |
| 1171 | << "\n"; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1172 | stream << "Address=" << neighbor.second->ipAddress() << "\n"; |
| 1173 | stream << "MACAddress=" << neighbor.second->macAddress() << "\n"; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1176 | // Write the dhcp section irrespective of whether DHCP is enabled or not |
| 1177 | writeDHCPSection(stream); |
| 1178 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1179 | stream.close(); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | void EthernetInterface::writeDHCPSection(std::fstream& stream) |
| 1183 | { |
| 1184 | using namespace std::string_literals; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1185 | // write the dhcp section |
| 1186 | stream << "[DHCP]\n"; |
| 1187 | |
| 1188 | // Hardcoding the client identifier to mac, to address below issue |
| 1189 | // https://github.com/openbmc/openbmc/issues/1280 |
| 1190 | stream << "ClientIdentifier=mac\n"; |
| 1191 | if (manager.getDHCPConf()) |
| 1192 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1193 | auto value = manager.getDHCPConf()->dnsEnabled() ? "true"s : "false"s; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1194 | stream << "UseDNS="s + value + "\n"; |
| 1195 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1196 | value = manager.getDHCPConf()->ntpEnabled() ? "true"s : "false"s; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1197 | stream << "UseNTP="s + value + "\n"; |
| 1198 | |
| 1199 | value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s; |
| 1200 | stream << "UseHostname="s + value + "\n"; |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 1201 | |
| 1202 | value = |
| 1203 | manager.getDHCPConf()->sendHostNameEnabled() ? "true"s : "false"s; |
| 1204 | stream << "SendHostname="s + value + "\n"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1208 | std::string EthernetInterface::macAddress(std::string value) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1209 | { |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 1210 | ether_addr newMAC; |
| 1211 | try |
| 1212 | { |
| 1213 | newMAC = mac_address::fromString(value); |
| 1214 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 1215 | catch (const std::invalid_argument&) |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 1216 | { |
| 1217 | log<level::ERR>("MACAddress is not valid.", |
| 1218 | entry("MAC=%s", value.c_str())); |
| 1219 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 1220 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 1221 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1222 | if (!mac_address::isUnicast(newMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1223 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 1224 | log<level::ERR>("MACAddress is not valid.", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1225 | entry("MAC=%s", value.c_str())); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 1226 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 1227 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1228 | } |
| 1229 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1230 | auto interface = interfaceName(); |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 1231 | std::string validMAC = mac_address::toString(newMAC); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1232 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1233 | // We don't need to update the system if the address is unchanged |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1234 | ether_addr oldMAC = mac_address::fromString(MacAddressIntf::macAddress()); |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 1235 | if (!stdplus::raw::equal(newMAC, oldMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1236 | { |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1237 | // Update everything that depends on the MAC value |
| 1238 | for (const auto& [name, intf] : vlanInterfaces) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1239 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1240 | intf->MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1241 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1242 | MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1243 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1244 | writeConfigurationFile(); |
William A. Kennington III | 6bfdf3e | 2021-11-09 17:15:12 -0800 | [diff] [blame] | 1245 | manager.addReloadPreHook([interface]() { |
| 1246 | // The MAC and LLADDRs will only update if the NIC is already down |
| 1247 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 1248 | setNICAdminState(eifSocket.sock, interface.c_str(), false); |
| 1249 | }); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1250 | manager.reloadConfigs(); |
Ratan Gupta | 677ae12 | 2017-09-18 16:28:50 +0530 | [diff] [blame] | 1251 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1252 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1253 | #ifdef HAVE_UBOOT_ENV |
| 1254 | // Ensure that the valid address is stored in the u-boot-env |
| 1255 | auto envVar = interfaceToUbootEthAddr(interface.c_str()); |
| 1256 | if (envVar) |
| 1257 | { |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 1258 | // Trimming MAC addresses that are out of range. eg: AA:FF:FF:FF:FF:100; |
| 1259 | // and those having more than 6 bytes. eg: AA:AA:AA:AA:AA:AA:BB |
| 1260 | execute("/sbin/fw_setenv", "fw_setenv", envVar->c_str(), |
| 1261 | validMAC.c_str()); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1262 | } |
| 1263 | #endif // HAVE_UBOOT_ENV |
| 1264 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1265 | return value; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1266 | } |
| 1267 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1268 | void EthernetInterface::deleteAll() |
| 1269 | { |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1270 | // clear all the ip on the interface |
| 1271 | addrs.clear(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1272 | |
| 1273 | writeConfigurationFile(); |
| 1274 | manager.reloadConfigs(); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1275 | } |
| 1276 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1277 | std::string EthernetInterface::defaultGateway(std::string gateway) |
| 1278 | { |
| 1279 | auto gw = EthernetInterfaceIntf::defaultGateway(); |
| 1280 | if (gw == gateway) |
| 1281 | { |
| 1282 | return gw; |
| 1283 | } |
| 1284 | |
| 1285 | if (!isValidIP(AF_INET, gateway)) |
| 1286 | { |
| 1287 | log<level::ERR>("Not a valid v4 Gateway", |
| 1288 | entry("GATEWAY=%s", gateway.c_str())); |
| 1289 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1290 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1291 | } |
| 1292 | gw = EthernetInterfaceIntf::defaultGateway(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1293 | |
| 1294 | writeConfigurationFile(); |
| 1295 | manager.reloadConfigs(); |
| 1296 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1297 | return gw; |
| 1298 | } |
| 1299 | |
| 1300 | std::string EthernetInterface::defaultGateway6(std::string gateway) |
| 1301 | { |
| 1302 | auto gw = EthernetInterfaceIntf::defaultGateway6(); |
| 1303 | if (gw == gateway) |
| 1304 | { |
| 1305 | return gw; |
| 1306 | } |
| 1307 | |
| 1308 | if (!isValidIP(AF_INET6, gateway)) |
| 1309 | { |
| 1310 | log<level::ERR>("Not a valid v6 Gateway", |
| 1311 | entry("GATEWAY=%s", gateway.c_str())); |
| 1312 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1313 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1314 | } |
| 1315 | gw = EthernetInterfaceIntf::defaultGateway6(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1316 | |
| 1317 | writeConfigurationFile(); |
| 1318 | manager.reloadConfigs(); |
| 1319 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1320 | return gw; |
| 1321 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1322 | } // namespace network |
| 1323 | } // namespace phosphor |