diff --git a/src/interfaces/CMakeLists.txt b/src/interfaces/CMakeLists.txt index c24be20b..41f1a22b 100644 --- a/src/interfaces/CMakeLists.txt +++ b/src/interfaces/CMakeLists.txt @@ -51,6 +51,8 @@ set( # data src/plugin/data/interfaces/interface.c src/plugin/data/interfaces/interface_state.c + src/plugin/data/interfaces/interface/ipv4.c + src/plugin/data/interfaces/interface/ipv6.c src/plugin/data/interfaces/interface/ipv4/address.c src/plugin/data/interfaces/interface/ipv4/neighbor.c src/plugin/data/interfaces/interface/ipv6/address.c diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c index e9b519c9..ed6c5ba8 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c @@ -1,13 +1,14 @@ #include "load.h" #include "netlink/route/link.h" #include "plugin/data/interfaces/interface.h" +#include "plugin/data/interfaces/interface/ipv4.h" -int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link) +int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link) { int error = 0; // enabled by default - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_enabled(element, 1), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_enabled(ipv4, 1), error_out); goto out; @@ -19,7 +20,7 @@ int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_int return error; } -int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link) +int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link) { int error = 0; @@ -28,13 +29,13 @@ int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_ return error; } -int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link) +int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link) { int error = 0; const unsigned int mtu = rtnl_link_get_mtu(link); - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_mtu(element, (uint16_t)mtu), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_mtu(ipv4, (uint16_t)mtu), error_out); goto out; diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h index 378b33af..6adeff9f 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h @@ -6,8 +6,8 @@ #include "plugin/data/interfaces/interface/linked_list.h" #include "plugin/types.h" -int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link); -int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link); -int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link); +int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); +int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); +int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); #endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV4_LOAD_H diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c b/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c index edc995ee..8e0e2a30 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c @@ -1,75 +1,48 @@ #include "load.h" +#include "netlink/route/link.h" +#include "plugin/data/interfaces/interface.h" +#include "plugin/data/interfaces/interface/ipv6.h" -int interfaces_add_address_ipv6(interfaces_interface_ipv6_address_element_t **address, char *ip, char *netmask) +int interfaces_interface_ipv6_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link) { int error = 0; - uint8_t prefix_length = 0; - interfaces_interface_ipv6_address_element_t* new_element = NULL; + // enabled by default + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv6_set_enabled(ipv6, 1), error_out); - new_element = interfaces_interface_ipv6_address_element_new(); + goto out; - interfaces_interface_ipv6_address_element_set_ip(&new_element, ip); - SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_address_netmask2prefix(netmask, prefix_length), out); - interfaces_interface_ipv6_address_element_set_prefix_length(&new_element, prefix_length); - interfaces_interface_ipv6_address_add_element(address, new_element); +error_out: + error = -1; out: - return error; -} - -int interfaces_add_neighbor_ipv6(interfaces_interface_ipv6_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr) -{ - interfaces_interface_ipv6_neighbor_element_t* new_element = NULL; - - new_element = interfaces_interface_ipv6_neighbor_element_new(); - - interfaces_interface_ipv6_neighbor_element_set_ip(&new_element, dst_addr); - interfaces_interface_ipv6_neighbor_element_set_link_layer_address(&new_element, ll_addr); - interfaces_interface_ipv6_neighbor_add_element(neighbor, new_element); - - return 0; -} - -unsigned int interfaces_get_ipv6_mtu(struct rtnl_link* link, interfaces_interface_t* interface) -{ - unsigned int mtu = 0; - mtu = rtnl_link_get_mtu(link); - - interface->ipv6.mtu = mtu; - - return 0; - + return error; } -unsigned int interfaces_get_ipv6_enabled(interfaces_interface_t* interface) +int interfaces_interface_ipv6_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link) { int error = 0; - int enabled = 0; - const char *ipv6_base = "/proc/sys/net/ipv6/conf"; - - SRPC_SAFE_CALL_ERR(error, read_from_proc_file(ipv6_base, interface->name, "disable_ipv6", &enabled), out); + // TODO: implement - interface->ipv6.enabled = enabled; - -out: return error; } -unsigned int interfaces_get_ipv6_forwarding(interfaces_interface_t* interface) +int interfaces_interface_ipv6_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link) { int error = 0; - int forwarding = 0; - const char *ipv6_base = "/proc/sys/net/ipv6/conf"; - - SRPC_SAFE_CALL_ERR(error, read_from_proc_file(ipv6_base, interface->name, "forwarding", &forwarding), out); + const unsigned int mtu = rtnl_link_get_mtu(link); - interface->ipv6.forwarding = forwarding; + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv6_set_mtu(ipv6, (uint16_t)mtu), error_out); + + goto out; + +error_out: + error = -1; out: + return error; } - diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.h b/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.h index 80a9daa4..342f4e71 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.h +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.h @@ -1,20 +1,13 @@ -#ifndef INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H -#define INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H +#ifndef INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV6_LOAD_H +#define INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV6_LOAD_H #include "plugin/context.h" #include "plugin/data/interfaces/interface.h" #include "plugin/data/interfaces/interface/linked_list.h" #include "plugin/types.h" -int interfaces_add_address_ipv6(interfaces_interface_ipv6_address_element_t **address, char *ip, char *netmask); - -int interfaces_add_neighbor_ipv6(interfaces_interface_ipv6_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr); - -unsigned int interfaces_get_ipv6_mtu(struct rtnl_link* link, interfaces_interface_t* interface); - -unsigned int interfaces_get_ipv6_enabled(interfaces_interface_t* interface); - -unsigned int interfaces_get_ipv6_forwarding(interfaces_interface_t* interface); - -#endif /* INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H */ +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); +#endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV6_LOAD_H diff --git a/src/interfaces/src/plugin/data/interfaces/interface.c b/src/interfaces/src/plugin/data/interfaces/interface.c index e26432cd..086376c4 100644 --- a/src/interfaces/src/plugin/data/interfaces/interface.c +++ b/src/interfaces/src/plugin/data/interfaces/interface.c @@ -1,4 +1,5 @@ #include "interface.h" +#include "interface/ipv4.h" #include "libyang/tree_data.h" #include "plugin/common.h" #include "srpc/ly_tree.h" @@ -176,18 +177,18 @@ int interfaces_interface_hash_from_ly(interfaces_interface_hash_element_t** if_h } if (ipv4_enabled_node) { - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_enabled(&new_element, strcmp(lyd_get_value(ipv4_enabled_node), "true") == 0 ? 1 : 0), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_enabled(&new_element->interface.ipv4, strcmp(lyd_get_value(ipv4_enabled_node), "true") == 0 ? 1 : 0), error_out); } if (ipv4_forwarding_node) { - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_forwarding(&new_element, strcmp(lyd_get_value(ipv4_forwarding_node), "true") == 0 ? 1 : 0), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_forwarding(&new_element->interface.ipv4, strcmp(lyd_get_value(ipv4_forwarding_node), "true") == 0 ? 1 : 0), error_out); } if (ipv4_mtu_node) { const char* mtu_str = lyd_get_value(ipv4_mtu_node); uint16_t mtu = (uint16_t)atoi(mtu_str); - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_mtu(&new_element, mtu), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_mtu(&new_element->interface.ipv4, mtu), error_out); } // init every list @@ -432,6 +433,7 @@ int interfaces_interface_hash_element_set_encapsulation(interfaces_interface_has (*el)->interface.encapsulation = encapsulation; (*el)->interface.encapsulation.dot1q_vlan.outer_tag.tag_type = xstrdup(encapsulation.dot1q_vlan.outer_tag.tag_type); (*el)->interface.encapsulation.dot1q_vlan.second_tag.tag_type = xstrdup(encapsulation.dot1q_vlan.second_tag.tag_type); + return 0; } @@ -469,62 +471,6 @@ int interfaces_interface_hash_element_set_parent_interface(interfaces_interface_ return 0; } -int interfaces_interface_hash_element_set_ipv4(interfaces_interface_hash_element_t** el, interfaces_interface_ipv4_t ipv4) -{ - (*el)->interface.ipv4 = ipv4; - - return 0; -} - -int interfaces_interface_hash_element_set_ipv4_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled) -{ - (*el)->interface.ipv4.enabled = enabled; - - return 0; -} - -int interfaces_interface_hash_element_set_ipv4_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding) -{ - (*el)->interface.ipv4.forwarding = forwarding; - - return 0; -} - -int interfaces_interface_hash_element_set_ipv4_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu) -{ - (*el)->interface.ipv4.mtu = mtu; - - return 0; -} - -int interfaces_interface_hash_element_set_ipv6(interfaces_interface_hash_element_t** el, interfaces_interface_ipv6_t ipv6) -{ - (*el)->interface.ipv6 = ipv6; - - return 0; -} - -int interfaces_interface_hash_element_set_ipv6_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled) -{ - (*el)->interface.ipv6.enabled = enabled; - - return 0; -} - -int interfaces_interface_hash_element_set_ipv6_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding) -{ - (*el)->interface.ipv6.forwarding = forwarding; - - return 0; -} - -int interfaces_interface_hash_element_set_ipv6_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu) -{ - (*el)->interface.ipv6.mtu = mtu; - - return 0; -} - int interfaces_interface_type_nl2ly(const char* nl_type, const char** ly_type) { int error = 0; diff --git a/src/interfaces/src/plugin/data/interfaces/interface.h b/src/interfaces/src/plugin/data/interfaces/interface.h index e65fd969..2c62cff7 100644 --- a/src/interfaces/src/plugin/data/interfaces/interface.h +++ b/src/interfaces/src/plugin/data/interfaces/interface.h @@ -33,24 +33,6 @@ int interfaces_interface_hash_element_set_loopback(interfaces_interface_hash_ele int interfaces_interface_hash_element_set_max_frame_size(interfaces_interface_hash_element_t** el, uint32_t max_frame_size); int interfaces_interface_hash_element_set_parent_interface(interfaces_interface_hash_element_t** el, const char* parent_interface); -/* - IPv4 -*/ - -int interfaces_interface_hash_element_set_ipv4(interfaces_interface_hash_element_t** el, interfaces_interface_ipv4_t ipv4); -int interfaces_interface_hash_element_set_ipv4_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled); -int interfaces_interface_hash_element_set_ipv4_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding); -int interfaces_interface_hash_element_set_ipv4_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu); - -/* - IPv6 -*/ - -int interfaces_interface_hash_element_set_ipv6(interfaces_interface_hash_element_t** el, interfaces_interface_ipv6_t ipv6); -int interfaces_interface_hash_element_set_ipv6_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled); -int interfaces_interface_hash_element_set_ipv6_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding); -int interfaces_interface_hash_element_set_ipv6_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu); - /* Helper functionality. */ diff --git a/src/interfaces/src/plugin/data/interfaces/interface/ipv4.c b/src/interfaces/src/plugin/data/interfaces/interface/ipv4.c new file mode 100644 index 00000000..cbb7e4dd --- /dev/null +++ b/src/interfaces/src/plugin/data/interfaces/interface/ipv4.c @@ -0,0 +1,22 @@ +#include "ipv4.h" + +int interfaces_interface_hash_element_ipv4_set_enabled(interfaces_interface_ipv4_t* ipv4, uint8_t enabled) +{ + ipv4->enabled = enabled; + + return 0; +} + +int interfaces_interface_hash_element_ipv4_set_forwarding(interfaces_interface_ipv4_t* ipv4, uint8_t forwarding) +{ + ipv4->forwarding = forwarding; + + return 0; +} + +int interfaces_interface_hash_element_ipv4_set_mtu(interfaces_interface_ipv4_t* ipv4, uint16_t mtu) +{ + ipv4->mtu = mtu; + + return 0; +} diff --git a/src/interfaces/src/plugin/data/interfaces/interface/ipv4.h b/src/interfaces/src/plugin/data/interfaces/interface/ipv4.h new file mode 100644 index 00000000..61fb3f4a --- /dev/null +++ b/src/interfaces/src/plugin/data/interfaces/interface/ipv4.h @@ -0,0 +1,10 @@ +#ifndef INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV4_H +#define INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV4_H + +#include "plugin/types.h" + +int interfaces_interface_hash_element_ipv4_set_enabled(interfaces_interface_ipv4_t* ipv4, uint8_t enabled); +int interfaces_interface_hash_element_ipv4_set_forwarding(interfaces_interface_ipv4_t* ipv4, uint8_t forwarding); +int interfaces_interface_hash_element_ipv4_set_mtu(interfaces_interface_ipv4_t* ipv4, uint16_t mtu); + +#endif // INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV4_H \ No newline at end of file diff --git a/src/interfaces/src/plugin/data/interfaces/interface/ipv6.c b/src/interfaces/src/plugin/data/interfaces/interface/ipv6.c new file mode 100644 index 00000000..36dbbbe1 --- /dev/null +++ b/src/interfaces/src/plugin/data/interfaces/interface/ipv6.c @@ -0,0 +1,22 @@ +#include "ipv6.h" + +int interfaces_interface_hash_element_ipv6_set_enabled(interfaces_interface_ipv6_t* ipv6, uint8_t enabled) +{ + ipv6->enabled = enabled; + + return 0; +} + +int interfaces_interface_hash_element_ipv6_set_forwarding(interfaces_interface_ipv6_t* ipv6, uint8_t forwarding) +{ + ipv6->forwarding = forwarding; + + return 0; +} + +int interfaces_interface_hash_element_ipv6_set_mtu(interfaces_interface_ipv6_t* ipv6, uint16_t mtu) +{ + ipv6->mtu = mtu; + + return 0; +} diff --git a/src/interfaces/src/plugin/data/interfaces/interface/ipv6.h b/src/interfaces/src/plugin/data/interfaces/interface/ipv6.h new file mode 100644 index 00000000..302671af --- /dev/null +++ b/src/interfaces/src/plugin/data/interfaces/interface/ipv6.h @@ -0,0 +1,10 @@ +#ifndef INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV6_H +#define INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV6_H + +#include "plugin/types.h" + +int interfaces_interface_hash_element_ipv6_set_enabled(interfaces_interface_ipv6_t* ipv6, uint8_t enabled); +int interfaces_interface_hash_element_ipv6_set_forwarding(interfaces_interface_ipv6_t* ipv6, uint8_t forwarding); +int interfaces_interface_hash_element_ipv6_set_mtu(interfaces_interface_ipv6_t* ipv6, uint16_t mtu); + +#endif // INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV6_H \ No newline at end of file