Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Netlink Class Reference

Basic wrapper around the libnl netlink library. More...

#include <Netlink.h>

Classes

struct  BridgePortDetails
 

Public Member Functions

bool isValid () const
 
bool ifaceUp (const std::string &ifaceName)
 Brings an interface up.
 
bool ifaceDown (const std::string &ifaceName)
 Takes an interface down.
 
bool ifaceIsUp (const std::string &ifaceName) const
 Queries the interface to determine if it's up or not.
 
bool ifaceExists (const std::string &ifaceName) const
 Checks if an interface with a given name exists (interface could be either up or down)
 
bool setIfaceAddress (const std::string &ifaceName, const in_addr_t address, const in_addr_t netmask)
 Sets the ip address and netmask of an interface (IPv4)
 
bool setIfaceAddress (const std::string &ifaceName, const struct in6_addr address, const int netmask)
 Sets the ip address and netmask of an interface (IPv6)
 
bool setIfaceForwarding (const std::string &ifaceName, bool enable)
 Enables or disables IPv4 forwarding on the given interface.
 
bool setIfaceForwarding6 (const std::shared_ptr< DobbyRdkPluginUtils > &utils, const std::string &ifaceName, bool enable)
 Enables or disables IPv6 forwarding on the given interface.
 
bool setIfaceRouteLocalNet (const std::string &ifaceName, bool enable)
 Sets the route_localnet flag on the interface.
 
bool setIfaceAcceptRa (const std::shared_ptr< DobbyRdkPluginUtils > &utils, const std::string &ifaceName, int value)
 Sets the accept_ra flag on the interface.
 
bool setIfaceMAC (const std::string &ifaceName, const std::array< uint8_t, 6 > &address)
 Sets the MAC address of the given interface.
 
std::array< uint8_t, 6 > getIfaceMAC (const std::string &ifaceName)
 Gets the MAC address of the given interface.
 
std::list< BridgePortDetailsgetAttachedIfaces (const std::string &bridgeName)
 Gets the set of interfaces currently enslaved to a given bridge device.
 
bool createBridge (const std::string &bridgeName)
 Creates a new bridge device.
 
bool destroyBridge (const std::string &bridgeName)
 Deletes an existing bridge device.
 
bool addIfaceToBridge (const std::string &bridgeName, const std::string &ifaceName)
 Adds an interface to the bridge device.
 
bool delIfaceFromBridge (const std::string &bridgeName, const std::string &ifaceName)
 Removes an interface from a bridge.
 
std::string createVeth (const std::string &peerVethName, pid_t peerPid, std::vector< std::string > &takenVeths)
 Creates a veth pair for the netns attached to the given pid.
 
bool checkVeth (const std::string &vethName)
 
bool addRoute (const std::string &iface, const in_addr_t destination, const in_addr_t netmask, const in_addr_t gateway)
 Adds a new route to the routing table (IPv4)
 
bool addRoute (const std::string &iface, const struct in6_addr destination, const int netmask, const struct in6_addr gateway)
 Adds a new route to the routing table (IPv6)
 
bool addArpEntry (const std::string &iface, in_addr_t address, const std::array< uint8_t, 6 > &mac)
 Sets an entry in the ARP table.
 
bool delArpEntry (const std::string &iface, in_addr_t address)
 Removes (invalidates) an entry in the ARP table.
 

Private Member Functions

bool applyChangesToLink (const std::string &ifaceName, const NlLink &changes)
 
bool setLinkAddress (const NlLink &link, const in_addr_t address, const in_addr_t netmask)
 Sets the ip address and netmask of an interface (IPv4)
 
bool setLinkAddress (const NlLink &link, const struct in6_addr address, const int netmask)
 Sets the ip address and netmask of an interface (IPv6)
 
bool setIfaceConfig (const std::string &ifaceName, const unsigned int configId, const uint32_t value)
 Sets the config on a given interface.
 
std::string getAvailableVethName (const int startIndex) const
 Returns the number of the next free veth device.
 

Private Attributes

struct nl_sock * mSocket
 
int mSysClassNetDirFd
 
std::mutex mLock
 

Detailed Description

Basic wrapper around the libnl netlink library.

There is only expected to be one of these objects (i.e. a shared_ptr is passed around). The object represents a single netlink socket.

At construction time a new netlink socket is opened, on destruction it is closed.

Member Function Documentation

◆ addArpEntry()

bool Netlink::addArpEntry ( const std::string &  iface,
in_addr_t  address,
const std::array< uint8_t, 6 > &  mac 
)

Sets an entry in the ARP table.

This is equivalent of the performing the following on the command line

arp -i <iface> -s <address> <mac>
Parameters
[in]ifaceThe name of the iface containing the ARP cache
[in]addressThe destination ip address
[in]macThe hardware address of the destination
Returns
true on success, false on failure.

◆ addIfaceToBridge()

bool Netlink::addIfaceToBridge ( const std::string &  bridgeName,
const std::string &  ifaceName 
)

Adds an interface to the bridge device.

This is equivalent of the performing the following on the command line

brctl addif <bridgeName> <ifaceName>
Returns
true on success, false on failure.

◆ addRoute() [1/2]

bool Netlink::addRoute ( const std::string &  iface,
const in_addr_t  destination,
const in_addr_t  netmask,
const in_addr_t  gateway 
)

Adds a new route to the routing table (IPv4)

This is equivalent of the performing the following on the command line

ip route add <destination>/<netmask> via <gateway> dev <ifname>
Parameters
[in]ifaceThe name of the iface to route to
[in]destinationThe destination ip address
[in]netmaskThe netmask for the destination ip address
[in]gatewayThe ip address of the gateway
Returns
true on success, false on failure.

◆ addRoute() [2/2]

bool Netlink::addRoute ( const std::string &  iface,
const struct in6_addr  destination,
const int  netmask,
const struct in6_addr  gateway 
)

Adds a new route to the routing table (IPv6)

This is equivalent of the performing the following on the command line

ip -6 route add <destination>/<netmask> via <gateway> dev <ifname>
Parameters
[in]ifaceThe name of the iface to route to
[in]destinationThe destination ip address
[in]netmaskThe netmask for the destination ip address
[in]gatewayThe ip address of the gateway, if null, we don't set a gateway for the route.
Returns
true on success, false on failure.

◆ createBridge()

bool Netlink::createBridge ( const std::string &  bridgeName)

Creates a new bridge device.

Note that it is not an error if a bridge device already exists with the same name, this method will return true in that case.

This is equivalent of the performing the following on the command line

brctl addbr <bridgeName>
Parameters
[in]bridgeNameThe name of the new bridge device.
Returns
true on success, false on failure.

◆ createVeth()

std::string Netlink::createVeth ( const std::string &  peerVethName,
pid_t  peerPid,
std::vector< std::string > &  takenVeths 
)

Creates a veth pair for the netns attached to the given pid.

Parameters
[in]peerVethNameThe name of the veth interface inside the net namespace (container), typically this will be "eth0".
[in]peerPidThe pid of the process which has the netns we want to create the veth in (i.e. the pid of init process within the container).
[in]takenVethsVeth devices reserved by other containers. We want to check that in case of races.
Returns
on success the interface name of the veth pair, this is the name outside the container and will be of the form vethd, ie veth0, veth1, etc. On failure an empty string is returned.

◆ delArpEntry()

bool Netlink::delArpEntry ( const std::string &  iface,
in_addr_t  address 
)

Removes (invalidates) an entry in the ARP table.

This is equivalent of the performing the following on the command line

arp -i <iface> -d <address>
Parameters
[in]ifaceThe name of the iface containing the ARP cache
[in]addressThe destination ip address
Returns
true on success, false on failure.

◆ delIfaceFromBridge()

bool Netlink::delIfaceFromBridge ( const std::string &  bridgeName,
const std::string &  ifaceName 
)

Removes an interface from a bridge.

This is equivalent of the performing the following on the command line

brctl delif <bridgeName> <ifaceName>
Returns
true on success, false on failure.

◆ destroyBridge()

bool Netlink::destroyBridge ( const std::string &  bridgeName)

Deletes an existing bridge device.

This is equivalent of the performing the following on the command line

brctl delbr <bridgeName>
Parameters
[in]bridgeNameThe name of the new bridge to delete.
Returns
true on success, false on failure.

◆ getAttachedIfaces()

std::list< Netlink::BridgePortDetails > Netlink::getAttachedIfaces ( const std::string &  bridgeName)

Gets the set of interfaces currently enslaved to a given bridge device.

Parameters
[in]bridgeNameThe name of the bridge device
Returns
a list of interfaces attached to the bridge, or empty list on failure.

◆ getAvailableVethName()

std::string Netlink::getAvailableVethName ( const int  startIndex) const
private

Returns the number of the next free veth device.

This works by scanning /sys/class/net/ for devices with names "veth%d", the first one not found is returned.

Parameters
[in]startIndexStarting index for veth names
Returns
the name of a free veth device, if all names are used (possible?) we return an empty string.

◆ getIfaceMAC()

std::array< uint8_t, 6 > Netlink::getIfaceMAC ( const std::string &  ifaceName)

Gets the MAC address of the given interface.

Parameters
[in]ifaceNameThe name of the interface to get.
Returns
the mac address.

◆ ifaceDown()

bool Netlink::ifaceDown ( const std::string &  ifaceName)

Takes an interface down.

Parameters
[in]ifaceNameThe name of the interface to take down.
Returns
true on success, false on failure.

◆ ifaceExists()

bool Netlink::ifaceExists ( const std::string &  ifaceName) const

Checks if an interface with a given name exists (interface could be either up or down)

Parameters
[in]ifaceNameThe name of the interface to check
Returns
true if the interface exists, otherwise false.

◆ ifaceIsUp()

bool Netlink::ifaceIsUp ( const std::string &  ifaceName) const

Queries the interface to determine if it's up or not.

Parameters
[in]ifaceNameThe name of the interface to check
Returns
true if the interface is up, otherwise false.

◆ ifaceUp()

bool Netlink::ifaceUp ( const std::string &  ifaceName)

Brings an interface up.

Parameters
[in]ifaceNameThe name of the interface to bring up.
Returns
true on success, false on failure.

◆ setIfaceAcceptRa()

bool Netlink::setIfaceAcceptRa ( const std::shared_ptr< DobbyRdkPluginUtils > &  utils,
const std::string &  ifaceName,
int  value 
)

Sets the accept_ra flag on the interface.

This is the equivalent of the following on the command line

echo "2" > /proc/sys/net/ipv6/conf/<ifaceName>/accept_ra

Or echo "1" > /proc/sys/net/ipv6/conf/<ifaceName>/accept_ra Or echo "0" > /proc/sys/net/ipv6/conf/<ifaceName>/accept_ra

This is used to set accept_ra to "2" so that router advertisements are accepted on the interface even with forwarding enabled.

Parameters
[in]utilsInstance of the DobbyRdkPluginUtils class.
[in]ifaceNameThe name of the interface to set the config on.
[in]enabletrue to enable, false to disable.
Returns
true on success, false on failure.

◆ setIfaceAddress() [1/2]

bool Netlink::setIfaceAddress ( const std::string &  ifaceName,
const in_addr_t  address,
const in_addr_t  netmask 
)

Sets the ip address and netmask of an interface (IPv4)

This is the equivalent of the following on the command line

ifconfig <ifaceName> <address> netmask <netmask>
Parameters
[in]ifaceNameThe name of the interface to set the address on
[in]addressThe address to set, the netmask will be applied to this before setting on the iface
[in]netmaskThe netmask to apply.
Returns
true on success, false on failure.

◆ setIfaceAddress() [2/2]

bool Netlink::setIfaceAddress ( const std::string &  ifaceName,
const struct in6_addr  address,
const int  netmask 
)

Sets the ip address and netmask of an interface (IPv6)

This is the equivalent of the following on the command line

ifconfig <ifaceName> inet6 add <address>/<netmask>
Parameters
[in]ifaceNameThe name of the interface to set the address on
[in]addressThe IPv6 address to set, the netmask will be applied to this before setting on the iface
[in]netmaskThe netmask to apply.
Returns
true on success, false on failure.

◆ setIfaceConfig()

bool Netlink::setIfaceConfig ( const std::string &  ifaceName,
const unsigned int  configId,
const uint32_t  value 
)
private

Sets the config on a given interface.

Used by setIfaceForwarding() and setIfaceRouteLocalNet().

Parameters
[in]ifaceNameThe name of the interface to set the address on
[in]configIdThe config to set.
[in]valueThe value to set.
Returns
true on success, false on failure.

◆ setIfaceForwarding()

bool Netlink::setIfaceForwarding ( const std::string &  ifaceName,
bool  enable 
)

Enables or disables IPv4 forwarding on the given interface.

This is the equivalent of the following on the command line

echo "1" > /proc/sys/net/ipv4/conf/<ifaceName>/forwarding

Or echo "0" > /proc/sys/net/ipv4/conf/<ifaceName>/forwarding

Parameters
[in]ifaceNameThe name of the interface to set the config on
[in]enabletrue to enable, false to disable.
Returns
true on success, false on failure.

◆ setIfaceForwarding6()

bool Netlink::setIfaceForwarding6 ( const std::shared_ptr< DobbyRdkPluginUtils > &  utils,
const std::string &  ifaceName,
bool  enable 
)

Enables or disables IPv6 forwarding on the given interface.

This is the equivalent of the following on the command line

echo "1" > /proc/sys/net/ipv6/conf/<ifaceName>/forwarding

Or echo "0" > /proc/sys/net/ipv6/conf/<ifaceName>/forwarding

Parameters
[in]utilsInstance of the DobbyRdkPluginUtils class.
[in]ifaceNameThe name of the interface to set the config on.
[in]enabletrue to enable, false to disable.
Returns
true on success, false on failure.

◆ setIfaceMAC()

bool Netlink::setIfaceMAC ( const std::string &  ifaceName,
const std::array< uint8_t, 6 > &  address 
)

Sets the MAC address of the given interface.

This is primarily used to set a fixed MAC address for the bridge device.

Parameters
[in]ifaceNameThe name of the interface to set.
[in]addressThe MAC address to set.
Returns
true if successfully set.

◆ setIfaceRouteLocalNet()

bool Netlink::setIfaceRouteLocalNet ( const std::string &  ifaceName,
bool  enable 
)

Sets the route_localnet flag on the interface.

Which means: "Do not consider loopback addresses as martian source or destination while routing. This enables the use of 127/8 for local routing purposes. default FALSE"

This is the equivalent of the following on the command line

echo "1" > /proc/sys/net/ipv4/conf/<ifaceName>/route_localnet

Or echo "0" > /proc/sys/net/ipv4/conf/<ifaceName>/route_localnet

This is used so we can use iptables to route packets on the bridge interface to local host. The main usage is for connecting specific ports like dns, as to the localhost interface.

Parameters
[in]ifaceNameThe name of the interface to set the config on.
[in]enabletrue to enable, false to disable.
Returns
true on success, false on failure.

◆ setLinkAddress() [1/2]

bool Netlink::setLinkAddress ( const NlLink link,
const in_addr_t  address,
const in_addr_t  netmask 
)
private

Sets the ip address and netmask of an interface (IPv4)

This is the equivalent of the following on the command line

ifconfig <ifaceName> <address> netmask <netmask>
Parameters
[in]linkInstance of NlLink (rtnl_link wrapper)
[in]addressThe address to set, the netmask will be applied to this before setting on the iface
[in]netmaskThe netmask to apply.
Returns
true on success, false on failure.

◆ setLinkAddress() [2/2]

bool Netlink::setLinkAddress ( const NlLink link,
const struct in6_addr  address,
const int  netmask 
)
private

Sets the ip address and netmask of an interface (IPv6)

This is the equivalent of the following on the command line

ifconfig <ifaceName> inet6 add <address>/<netmask>
Parameters
[in]linkInstance of NlLink (rtnl_link wrapper)
[in]addressThe address to set, the netmask will be applied to this before setting on the iface
[in]netmaskThe netmask to apply.
Returns
true on success, false on failure.

The documentation for this class was generated from the following files: