23 #include "networkgateway.h" 24 #include "networkgatewayparser.h" 25 #include "functionjob.h" 30 const std::string bridgeDevice,
31 const std::string gateway,
32 const uint8_t maskBits,
33 std::shared_ptr<ContainerAbstractInterface> container) :
37 m_bridgeDevice(bridgeDevice),
38 m_interfaceInitialized(false),
43 NetworkGateway::~NetworkGateway() { }
51 m_entries.push_back(e);
60 if (m_gateway.size() != 0) {
61 log_debug() <<
"Default gateway set to " << m_gateway;
63 log_warning() <<
"No gateway. Network access will be disabled";
67 if (!isBridgeAvailable()) {
68 log_error() <<
"Bridge not available, expected gateway to be " << m_gateway;
73 m_ip.s_addr = m_functions.
generateIP(m_netmask, m_gateway, m_containerID);
75 log_error() << error.what();
79 bool returnValue = up();
81 log_error() <<
"Couldn't bring the network up";
85 log_debug() <<
"Adding iptables entries";
86 for (
auto entry : m_entries) {
88 return entry.applyRules() ? SUCCESS : FAILURE;
94 log_error() <<
"Failed to apply rules for entry: " << entry.toString();
107 bool NetworkGateway::setDefaultGateway()
116 return job.isSuccess();
119 bool NetworkGateway::up()
121 static const constexpr
int BAD_SETIP = 3;
123 if (m_interfaceInitialized) {
124 log_debug() <<
"Interface already configured";
128 log_debug() <<
"Attempting to bring up eth0";
132 Netlink::LinkInfo iface;
137 int ifaceIndex = iface.first.ifi_index;
138 if (!n.
linkUp(ifaceIndex)) {
142 if (!n.
setIP(ifaceIndex, m_ip, m_netmask)) {
149 jobBringUpEthernet.start();
151 int returnCode = jobBringUpEthernet.wait();
154 log_error() <<
"Could not find interface eth0 in container";
157 log_error() <<
"Could not bring interface eth0 up in container";
160 log_error() <<
"Could not set IP-address";
163 log_debug() <<
"Interface brought up, proceeding to set default gateway";
164 m_interfaceInitialized =
true;
165 return setDefaultGateway();
167 log_error() <<
"Unhandled case in NetworkGateway::up(), this is an error!";
172 bool NetworkGateway::down()
174 log_debug() <<
"Attempting to configure eth0 to 'down state'";
177 Netlink::LinkInfo iface;
182 if (!n.
linkDown(iface.first.ifi_index)) {
189 int returnCode = job.wait();
193 log_error() <<
"Could not find interface eth0 in container";
196 log_error() <<
"Could not bring interface eth0 down in container";
201 log_error() <<
"Unhandled case in NetworkGateway::down(), this is an error!";
206 bool NetworkGateway::isBridgeAvailable()
208 log_debug() <<
"Is bridge available?";
209 Netlink::LinkInfo iface;
210 if (!m_netlinkHost.
findLink(m_bridgeDevice.c_str(), iface)) {
211 log_error() <<
"Could not find " << m_bridgeDevice <<
" in the host";
214 std::vector<Netlink::AddressInfo> addresses;
215 if (!m_netlinkHost.
findAddresses(iface.first.ifi_index, addresses)) {
216 log_error() <<
"Could not fetch addresses for " << m_bridgeDevice <<
" in the host";
219 log_debug() <<
"Could find bridge and could fetch address, running hasAddress";
220 bool retval = m_netlinkHost.
hasAddress(addresses, AF_INET, m_gateway.c_str());
bool linkDown(const int ifaceIndex)
Bring a given interface down.
bool hasAddress(const std::vector< AddressInfo > &haystack, const int addressFamily, const char *needle)
checks if an address is present in the given list
Run a C++ function lambda inside a SoftwareContainer.
bool linkUp(const int ifaceIndex)
Bring the given interface up.
bool activateGateway() override
Implements Gateway::activateGateway.
uint32_t generateIP(const uint32_t netmask, const std::string gatewayIP, const int32_t containerID)
Generate IP address for the container.
bool parseNetworkGatewayConfiguration(const json_t *element, IPTableEntry &e)
Parses NetworkGateway configuration into IPTableEntry.
Gateway base class for SoftwareContainer.
bool teardownGateway() override
Implements Gateway::teardownGateway.
bool findAddresses(const unsigned int interfaceIndex, std::vector< AddressInfo > &result)
Get all addresses associated with the given interface index.
bool setDefaultGateway(const char *gatewayAddress)
Sets an ip address as the default gateway.
bool findLink(const char *ifaceName, LinkInfo &linkInfo)
Check that the device given is a network bridge.
std::shared_ptr< ContainerAbstractInterface > getContainer()
Get a handle to the associated container.
bool setIP(const int ifaceIndex, const in_addr ip, const unsigned char netmask)
Sets an IP address for a network link.
NetworkGateway(const int32_t id, const std::string bridgeDevice, const std::string gateway, const uint8_t maskBits, std::shared_ptr< ContainerAbstractInterface > container)
Creates a network gateway.
Developers guide to adding a config item:
A rules entry for the treatment of packets.
bool readConfigElement(const json_t *element) override
Gateway specific parsing of config elements.
Handles various network operations over netlink.