Skip to content

Commit

Permalink
interfaces-plugin: load IPv6 address and neighbor list
Browse files Browse the repository at this point in the history
  • Loading branch information
zinccyy committed Oct 31, 2022
1 parent 43f3ce7 commit 290bde5
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "load.h"
#include "netlink/route/link.h"
#include "plugin/api/interfaces/interface/ipv6/address/load.h"
#include "plugin/api/interfaces/interface/ipv6/neighbor/load.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/data/interfaces/interface/ipv6.h"

Expand Down Expand Up @@ -46,3 +48,113 @@ int interfaces_interface_ipv6_load_mtu(interfaces_ctx_t* ctx, interfaces_interfa

return error;
}

int interfaces_interface_ipv6_load_address(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link)
{
int error = 0;
interfaces_nl_ctx_t* nl_ctx = &ctx->startup_ctx.nl_ctx;
struct rtnl_addr* addr_iter = NULL;

// created element
interfaces_interface_ipv6_address_element_t* new_element = NULL;
uint8_t element_added = 0;

// allocate address list
ipv6->address = interfaces_interface_ipv6_address_new();

// allocate cache
SRPC_SAFE_CALL_ERR(error, rtnl_addr_alloc_cache(nl_ctx->socket, &nl_ctx->addr_cache), error_out);

// get link iterator
SRPC_SAFE_CALL_PTR(addr_iter, (struct rtnl_addr*)nl_cache_get_first(nl_ctx->addr_cache), error_out);

// iterate links
while (addr_iter) {
// get all addresses from the link and extract info
if (rtnl_addr_get_ifindex(addr_iter) == rtnl_link_get_ifindex(link) && rtnl_addr_get_family(addr_iter) == AF_INET6) {
// create new element
new_element = interfaces_interface_ipv6_address_element_new();
element_added = 0;

// load IP and prefix
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_address_load_ip(ctx, &new_element, addr_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_address_load_prefix_length(ctx, &new_element, addr_iter), error_out);

// add element to the list
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_address_add_element(&ipv6->address, new_element), error_out);
element_added = 1;
}

// iterate
SRPC_SAFE_CALL_PTR(addr_iter, (struct rtnl_addr*)nl_cache_get_next((struct nl_object*)addr_iter), error_out);
}

goto out;

error_out:
error = -1;

// if interrupted see if any memory is left over from the allocated element - free the element
if (!element_added) {
interfaces_interface_ipv6_address_element_free(&new_element);
}

out:

return error;
}

int interfaces_interface_ipv6_load_neighbor(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link)
{
int error = 0;
interfaces_nl_ctx_t* nl_ctx = &ctx->startup_ctx.nl_ctx;
struct rtnl_neigh* neigh_iter = NULL;

// created element
interfaces_interface_ipv6_neighbor_element_t* new_element = NULL;
uint8_t element_added = 0;

// allocate address list
ipv6->neighbor = interfaces_interface_ipv6_neighbor_new();

// allocate cache
SRPC_SAFE_CALL_ERR(error, rtnl_neigh_alloc_cache(nl_ctx->socket, &nl_ctx->neigh_cache), error_out);

// get link iterator
SRPC_SAFE_CALL_PTR(neigh_iter, (struct rtnl_neigh*)nl_cache_get_first(nl_ctx->addr_cache), error_out);

// iterate links
while (neigh_iter) {
// get all neighbors from the link and extract info
if (rtnl_neigh_get_ifindex(neigh_iter) == rtnl_link_get_ifindex(link) && rtnl_neigh_get_family(neigh_iter) == AF_INET6) {
// create new element
new_element = interfaces_interface_ipv6_neighbor_element_new();
element_added = 0;

// load IP and prefix
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_neighbor_load_ip(ctx, &new_element, neigh_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_neighbor_load_link_layer_address(ctx, &new_element, neigh_iter), error_out);

// add element to the list
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_neighbor_add_element(&ipv6->neighbor, new_element), error_out);
element_added = 1;
}

// iterate
SRPC_SAFE_CALL_PTR(neigh_iter, (struct rtnl_neigh*)nl_cache_get_next((struct nl_object*)neigh_iter), error_out);
}

goto out;

error_out:
error = -1;

// if interrupted see if any memory is left over from the allocated element - free the element
if (!element_added) {
interfaces_interface_ipv6_neighbor_element_free(&new_element);
}

out:

return error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
int interfaces_interface_ipv6_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);
int interfaces_interface_ipv6_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);
int interfaces_interface_ipv6_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);
int interfaces_interface_ipv6_load_address(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);
int interfaces_interface_ipv6_load_neighbor(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);

#endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV6_LOAD_H

0 comments on commit 290bde5

Please sign in to comment.