diff --git a/source/DHCPv6_Server/DHCPv6_Server_service.c b/source/DHCPv6_Server/DHCPv6_Server_service.c index 26b7b3a7963d..1dbdf39189d3 100644 --- a/source/DHCPv6_Server/DHCPv6_Server_service.c +++ b/source/DHCPv6_Server/DHCPv6_Server_service.c @@ -34,7 +34,6 @@ #include "DHCPv6_Server/DHCPv6_server_service.h" #include "common_functions.h" #include "NWK_INTERFACE/Include/protocol.h" -#include "6LoWPAN/Thread/thread_bbr_api_internal.h" #include "Common_Protocols/icmpv6.h" #include "dhcp_service_api.h" @@ -57,9 +56,6 @@ static int8_t dhcpv6_service_tasklet = -1; static arm_event_storage_t *dhcp_timer_storage = NULL; -static void DHCPV6_server_service_remove_GUA_from_neighcache(protocol_interface_info_entry_t *cur, uint8_t *targetAddress); - - static bool DHCP_server_service_timer_start(void) { if(!dhcp_timer_storage) { @@ -101,19 +97,16 @@ int DHCPv6_server_respond_client(dhcpv6_gua_server_entry_s *serverBase, dhcpv6_r nonTemporalAddress.validLifeTime = dhcp_allocated_address->lifetime; nonTemporalAddress.preferredLifeTime = dhcp_allocated_address->preferredLifetime; - // If this is solicit from existing address, flush ND cache. - if (allocateNew) { - // coverity[returned_null] for ignoring protocol_stack_interface_info_get_by_id NULL return - DHCPV6_server_service_remove_GUA_from_neighcache(protocol_stack_interface_info_get_by_id(serverBase->interfaceId), nonTemporalAddress.requestedAddress); - } - if (thread_bbr_nd_entry_add(serverBase->interfaceId,dhcp_allocated_address->nonTemporalAddress, nonTemporalAddress.validLifeTime, serverBase->guaPrefix) == -1) { - // No nanostack BBR present we will put entry for application implemented BBR - ipv6_route_t *route = ipv6_route_add_with_info(dhcp_allocated_address->nonTemporalAddress, 128, serverBase->interfaceId, NULL, ROUTE_THREAD_PROXIED_HOST,serverBase->guaPrefix,0, nonTemporalAddress.validLifeTime, 0); - if (!route) { + if (serverBase->addCb) { + dhcp_address_cache_update_t update_info; + update_info.allocatedAddress = dhcp_allocated_address->nonTemporalAddress; + update_info.allocatedNewAddress = allocateNew; + update_info.validLifeTime = nonTemporalAddress.validLifeTime; + + if (!serverBase->addCb(serverBase->interfaceId, &update_info, serverBase->guaPrefix)) { address_allocated = false; libdhcpv6_address_rm_from_allocated_list(serverBase,dhcp_allocated_address->nonTemporalAddress); } - } } @@ -213,30 +206,6 @@ void DHCPv6_server_service_tasklet(arm_event_s *event) } } -static void DHCPV6_server_service_remove_GUA_from_neighcache(protocol_interface_info_entry_t *cur, uint8_t *targetAddress) -{ - ipv6_neighbour_t *neighbour_entry; - - neighbour_entry = ipv6_neighbour_lookup(&cur->ipv6_neighbour_cache, targetAddress); - if (neighbour_entry) { - tr_debug("Remove from neigh Cache: %s", tr_ipv6(targetAddress)); - ipv6_neighbour_entry_remove(&cur->ipv6_neighbour_cache, neighbour_entry); - } -} - -void DHCPv6_server_service_address_preferred_timeout_handler(int8_t interfaceId, uint8_t *targetAddress) -{ - tr_warn("Address Preferred Timeout"); - protocol_interface_info_entry_t *cur = 0; - //allocate Socket Service - - cur = protocol_stack_interface_info_get_by_id(interfaceId); - if (cur) { - ipv6_route_delete(targetAddress, 128, interfaceId, NULL, ROUTE_THREAD_PROXIED_HOST); - DHCPV6_server_service_remove_GUA_from_neighcache(cur, targetAddress); - } -} - static int8_t dhcpv6_server_service_tasklet_generated(void) { if (dhcpv6_service_tasklet == -1) { @@ -274,7 +243,7 @@ int DHCPv6_server_service_init(int8_t interface, uint8_t guaPrefix[static 16], u retVal = -2; } else { //allocate server - dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_gua_server_allocate(guaPrefix, interface, cur->mac, DHCPV6_DUID_HARDWARE_EUI64_TYPE, DHCPv6_server_service_address_preferred_timeout_handler); + dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_gua_server_allocate(guaPrefix, interface, cur->mac, DHCPV6_DUID_HARDWARE_EUI64_TYPE); if (serverInfo) { //if_address_entry_t *def_address = NULL; uint8_t temp[16]; @@ -303,25 +272,6 @@ void DHCPv6_server_service_timeout_cb(uint32_t timeUpdateInSeconds) libdhcpv6_gua_servers_time_update(timeUpdateInSeconds); } -void DHCPv6_GUA64_ML64_route_control(int8_t interfaceId, uint8_t *allocatedGuaAddress, uint8_t *clientEUID64, uint8_t *meshLocalPrefix, bool deleteMapping) -{ - uint8_t ml64[16]; - uint8_t *ptr = ml64; - memcpy(ptr, meshLocalPrefix, 8); - ptr += 8; - memcpy(ptr, clientEUID64, 8); - *ptr ^= 2; - //Generate Route Info - if (deleteMapping) { - ipv6_route_delete(allocatedGuaAddress, 128, interfaceId, ml64, ROUTE_STATIC); - } else if (ipv6_route_add(allocatedGuaAddress, 128, interfaceId, ml64, ROUTE_STATIC, 0xffffffff, 0) == 0) { - tr_debug("Route ADD OK"); - } else { - tr_warn("Route Add fail"); - } - -} - /* Delete dhcp thread dhcp router ID server. * * When this is called it close selected service and free all allocated memory. @@ -332,30 +282,32 @@ void DHCPv6_GUA64_ML64_route_control(int8_t interfaceId, uint8_t *allocatedGuaAd */ void DHCPv6_server_service_delete(int8_t interface, uint8_t guaPrefix[static 16], bool delete_gua_addresses) { - protocol_interface_info_entry_t *curPtr = 0; - //allocate Socket Service - - curPtr = protocol_stack_interface_info_get_by_id(interface); - if (curPtr) { - dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix); - if (serverInfo) { - ns_list_foreach_safe(dhcpv6_alloacted_address_entry_t, cur, &serverInfo->allocatedAddressList) { - //Delete Server data base - DHCPV6_server_service_remove_GUA_from_neighcache(curPtr, cur->nonTemporalAddress); + dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix); + if (serverInfo) { + ns_list_foreach_safe(dhcpv6_alloacted_address_entry_t, cur, &serverInfo->allocatedAddressList) { + //Delete Server data base + if (serverInfo->removeCb) { + serverInfo->removeCb(interface, cur->nonTemporalAddress, NULL); } + } + if (serverInfo->removeCb) { // Clean all /128 'Thread Proxy' routes to self and others added when acting as a DHCP server - ipv6_route_table_remove_info(curPtr->id, ROUTE_THREAD_PROXIED_HOST,serverInfo->guaPrefix); - dhcp_service_delete(serverInfo->socketInstance_id); + serverInfo->removeCb(interface, NULL, serverInfo->guaPrefix); } + dhcp_service_delete(serverInfo->socketInstance_id); + } - if (delete_gua_addresses) { + if (delete_gua_addresses) { + protocol_interface_info_entry_t *curPtr = protocol_stack_interface_info_get_by_id(interface); + if (curPtr) { protocol_core_dhcpv6_allocated_address_remove(curPtr, guaPrefix); } + } - libdhcpv6_gua_server_free_by_prefix_and_interfaceid(guaPrefix, interface); + libdhcpv6_gua_server_free_by_prefix_and_interfaceid(guaPrefix, interface); + + DHCP_server_service_timer_stop(); - DHCP_server_service_timer_stop(); - } } /* Control GUA address for client by DUI.Default value is true @@ -379,33 +331,15 @@ int DHCPv6_server_service_set_address_autonous_flag(int8_t interface, uint8_t gu return retVal; } -/* Enable or disable GUA64 Address mapping to ML64 - * - * - * /param interface interface id of this thread instance. - * /param guaPrefix Prefix which will be removed - * /param mode - * /param meshLocalPrefix mesh local prefix for generate ML6 from client EUID64 - */ -int DHCPv6_server_service_set_gua_address_mapping(int8_t interface, uint8_t guaPrefix[static 16], bool mode, uint8_t meshLocalPrefix[8]) +void DHCPv6_server_service_callback_set(int8_t interface, uint8_t guaPrefix[static 16], dhcp_address_prefer_remove_cb *remove_cb, dhcp_address_add_notify_cb *add_cb) { - int retVal = -1; - dhcpv6_gua_server_entry_s *serverInfo; - - serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix); - if (serverInfo) { - if (mode == true && meshLocalPrefix == NULL) { - retVal = -2; - } else { - serverInfo->enableAddressMapping = mode; - if (meshLocalPrefix) { - memcpy(serverInfo->meshLocalPrefix, meshLocalPrefix, 8); - } - retVal = 0; - } + dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix); + if (!serverInfo) { + return; } - return retVal; + serverInfo->addCb = add_cb; + serverInfo->removeCb = remove_cb; } /* SET max accepted clients to server, Default is 200 @@ -453,21 +387,6 @@ int DHCPv6_server_service_set_address_validlifetime(int8_t interface, uint8_t gu } return retVal; } - -int DHCPv6_server_service_gua_target_mac_check(int8_t interfaceId, const uint8_t *targetGUA, uint8_t *targetEUI64) -{ - dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interfaceId, targetGUA); - if (serverInfo) { - dhcpv6_alloacted_address_entry_t *entry = libdhcpv6_address_get_from_allocated_list(serverInfo, targetGUA); - if (entry) { - if (entry->preferredLifetime && (entry->linkType == DHCPV6_DUID_HARDWARE_EUI64_TYPE)) { - memcpy(targetEUI64, entry->linkId, 8); - return 0; - } - } - } - return -1; -} #else int DHCPv6_server_service_init(int8_t interface, uint8_t guaPrefix[static 16], uint8_t serverDUID[static 8], uint16_t serverDUIDType) @@ -497,15 +416,6 @@ int DHCPv6_server_service_set_address_autonous_flag(int8_t interface, uint8_t gu return -1; } -int DHCPv6_server_service_set_gua_address_mapping(int8_t interface, uint8_t guaPrefix[static 16], bool mode, uint8_t meshLocalPrefix[8]) -{ - (void) interface; - (void) guaPrefix; - (void) mode; - (void) meshLocalPrefix; - - return -1; -} int DHCPv6_server_service_set_max_clients_accepts_count(int8_t interface, uint8_t guaPrefix[static 16], uint32_t maxClientCount) { (void) interface; @@ -522,13 +432,5 @@ int DHCPv6_server_service_set_address_validlifetime(int8_t interface, uint8_t gu return -1; } -int DHCPv6_server_service_gua_target_mac_check(int8_t interfaceId, const uint8_t *targetGUA, uint8_t *targetEUI64) -{ - (void) interfaceId; - (void) targetGUA; - (void) targetEUI64; - - return -1; -} #endif diff --git a/source/DHCPv6_Server/DHCPv6_server_service.h b/source/DHCPv6_Server/DHCPv6_server_service.h index 50f535f7a74f..25d18ff8a0c9 100644 --- a/source/DHCPv6_Server/DHCPv6_server_service.h +++ b/source/DHCPv6_Server/DHCPv6_server_service.h @@ -23,6 +23,10 @@ #ifndef DHCPV6_SERVER_SERVICE_H_ #define DHCPV6_SERVER_SERVICE_H_ + +#include "libDHCPv6/libDHCPv6.h" +#include "libDHCPv6/libDHCPv6_server.h" + /* Initialize dhcp Global address server. * * This instance needs to bee initialized once for each thread network interface. @@ -38,6 +42,10 @@ */ int DHCPv6_server_service_init(int8_t interface, uint8_t guaPrefix[static 16], uint8_t serverDUID[static 8], uint16_t serverDUIDType); + +void DHCPv6_server_service_callback_set(int8_t interface, uint8_t guaPrefix[static 16], dhcp_address_prefer_remove_cb *remove_cb, dhcp_address_add_notify_cb *add_cb); + + /* Delete dhcp thread dhcp router ID server. * * When this is called it close selected service and free all allocated memory. @@ -51,8 +59,6 @@ void DHCPv6_server_service_delete(int8_t interface, uint8_t guaPrefix[static 16] void DHCPv6_server_service_timeout_cb(uint32_t timeUpdateInSeconds); -//void DHCPv6_GUA64_ML64_route_control(int8_t interfaceId, uint8_t *allocatedGuaAddress, uint8_t *clientEUID64, uint8_t *meshLocalPrefix, bool deleteMapping); - /* Control GUA address for client by DUI.Default value is true * * @@ -63,16 +69,6 @@ void DHCPv6_server_service_timeout_cb(uint32_t timeUpdateInSeconds); int DHCPv6_server_service_set_address_autonous_flag(int8_t interface, uint8_t guaPrefix[static 16], bool mode); -/* Enable or disable GUA64 Address mapping to ML64 - * - * - * /param interface interface id of this thread instance. - * /param guaPrefix Prefix which will be removed - * /param mode - * /param meshLocalPrefix mesh local prefix for generate ML6 from client EUID64 - */ -int DHCPv6_server_service_set_gua_address_mapping(int8_t interface, uint8_t guaPrefix[static 16], bool mode, uint8_t meshLocalPrefix[8]); - /* SET max accepted clients to server, Default is 200 * * @@ -92,6 +88,4 @@ int DHCPv6_server_service_set_max_clients_accepts_count(int8_t interface, uint8_ */ int DHCPv6_server_service_set_address_validlifetime(int8_t interface, uint8_t guaPrefix[static 16], uint32_t validLifeTimne); - -int DHCPv6_server_service_gua_target_mac_check(int8_t interfaceId, const uint8_t *targetGUA, uint8_t *targetEUI64); #endif /* DHCPV6_SERVER_SERVICE_H_ */ diff --git a/source/libDHCPv6/libDHCPv6_server.c b/source/libDHCPv6/libDHCPv6_server.c index a70fd001da18..f8faa890e19c 100644 --- a/source/libDHCPv6/libDHCPv6_server.c +++ b/source/libDHCPv6/libDHCPv6_server.c @@ -43,11 +43,12 @@ static dhcpv6_gua_server_entry_s *libdhcpv6_server_entry_allocate(void) dhcpv6_gua_server_entry_s *entry = ns_dyn_mem_alloc(sizeof(dhcpv6_gua_server_entry_s)); if (entry) { entry->clientIdSequence = 0; - entry->enableAddressMapping = false; entry->enableAddressAutonous = true; entry->clientIdDefaultSuffics = 0x0000000; entry->maxSuppertedClients = 200; entry->validLifetime = 7200; + entry->removeCb = NULL; + entry->addCb = NULL; ns_list_init(&entry->allocatedAddressList); } return entry; @@ -96,7 +97,9 @@ void libdhcpv6_gua_servers_time_update(uint32_t timeUpdateInSeconds) //Stop use this address for leasequery and delete Route or address map address->preferredLifetime = 0; - cur->timeoutCb(cur->interfaceId, address->nonTemporalAddress); + if (cur->removeCb) { + cur->removeCb(cur->interfaceId, address->nonTemporalAddress, cur->guaPrefix); + } } else { address->preferredLifetime -= timeUpdateInSeconds; } @@ -137,7 +140,7 @@ dhcpv6_gua_server_entry_s *libdhcpv6_server_data_get_by_prefix_and_socketinstanc } -dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t interfaceId, uint8_t *serverDUID, uint16_t serverDUIDType, dhcp_address_prefer_timeout_cb *prefered_timeout_cb) +dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t interfaceId, uint8_t *serverDUID, uint16_t serverDUIDType) { dhcpv6_gua_server_entry_s *entry = NULL; if (libdhcpv6_server_data_get_by_prefix_and_interfaceid(interfaceId, prefix) == NULL) { @@ -147,7 +150,6 @@ dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t memcpy(entry->serverDUID, serverDUID, 8); entry->serverLinkType = serverDUIDType; entry->interfaceId = interfaceId; - entry->timeoutCb = prefered_timeout_cb; ns_list_add_to_end(&dhcpv6_gua_server_list, entry); } } diff --git a/source/libDHCPv6/libDHCPv6_server.h b/source/libDHCPv6/libDHCPv6_server.h index 9f47aff2437b..6e9412e4789b 100644 --- a/source/libDHCPv6/libDHCPv6_server.h +++ b/source/libDHCPv6/libDHCPv6_server.h @@ -26,7 +26,7 @@ #ifdef HAVE_DHCPV6_SERVER #include "ns_list.h" -typedef void (dhcp_address_prefer_timeout_cb)(int8_t interfaceId, uint8_t *targetAddress); +typedef void (dhcp_address_prefer_remove_cb)(int8_t interfaceId, uint8_t *targetAddress, void *prefix_info); typedef struct dhcpv6_alloacted_address_entry_s { uint8_t nonTemporalAddress[16]; @@ -50,10 +50,16 @@ typedef struct thread_dhcpv6_server_data_s { ns_list_link_t link; /*!< List link entry */ } dhcpv6_server_data_entry_t; +typedef struct dhcp_address_cache_update{ + uint8_t *allocatedAddress; + bool allocatedNewAddress; + uint32_t validLifeTime; +} dhcp_address_cache_update_t; + +typedef bool (dhcp_address_add_notify_cb)(int8_t interfaceId, dhcp_address_cache_update_t *address_info, void *route_src); + typedef struct dhcpv6_gua_server_entry_s { int8_t interfaceId; - bool enableAddressMapping; - uint8_t meshLocalPrefix[8]; bool enableAddressAutonous; uint16_t socketInstance_id; uint8_t guaPrefix[8]; @@ -63,13 +69,14 @@ typedef struct dhcpv6_gua_server_entry_s { uint32_t clientIdDefaultSuffics; uint32_t clientIdSequence; /*!< Define */ uint32_t validLifetime; - dhcp_address_prefer_timeout_cb *timeoutCb; + dhcp_address_prefer_remove_cb *removeCb; + dhcp_address_add_notify_cb *addCb; dhcpv6_alloacted_address_list_t allocatedAddressList; ns_list_link_t link; /*!< List link entry */ } dhcpv6_gua_server_entry_s; bool libdhcpv6_gua_server_list_empty(void); -dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t interfaceId, uint8_t *serverDUID, uint16_t serverDUIDType, dhcp_address_prefer_timeout_cb *prefered_timeout_cb); +dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t interfaceId, uint8_t *serverDUID, uint16_t serverDUIDType); void libdhcpv6_gua_server_free_by_prefix_and_interfaceid(uint8_t *prefix, int8_t interfaceId); void libdhcpv6_gua_servers_time_update(uint32_t timeUpdateInSeconds); void libdhcpv6_address_rm_from_allocated_list(dhcpv6_gua_server_entry_s *serverInfo, const uint8_t *address); @@ -81,7 +88,7 @@ dhcpv6_alloacted_address_entry_t *libdhcpv6_address_allocated_list_scan(dhcpv6_g #define libdhcpv6_gua_server_list_empty() true #define libdhcpv6_server_data_get_by_prefix_and_interfaceid(interfaceId, prefixPtr) NULL #define libdhcpv6_server_data_get_by_prefix_and_socketinstance(socketInstance, prefixPtr) NULL -#define libdhcpv6_gua_server_allocate(prefix, interfaceId, serverDUID, serverDUIDType, prefered_timeout_cb) NULL +#define libdhcpv6_gua_server_allocate(prefix, interfaceId, serverDUID, serverDUIDType) NULL #define libdhcpv6_gua_server_free_by_prefix_and_interfaceid(prefix, interfaceId) ((void)0) #define libdhcpv6_gua_servers_time_update(timeUpdateInSeconds) ((void)0) #define libdhcpv6_gua_server_free_by_interfaceid(interfaceId) ((void)0) diff --git a/test/nanostack/unittest/stub/libDHCPv6_server_stub.c b/test/nanostack/unittest/stub/libDHCPv6_server_stub.c index b5b4583ce9e8..006792452bab 100644 --- a/test/nanostack/unittest/stub/libDHCPv6_server_stub.c +++ b/test/nanostack/unittest/stub/libDHCPv6_server_stub.c @@ -38,7 +38,7 @@ dhcpv6_gua_server_entry_s *libdhcpv6_server_data_get_by_prefix_and_socketinstanc } -dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t interfaceId, uint8_t *serverDUID, uint16_t serverDUIDType, dhcp_address_prefer_timeout_cb *prefered_timeout_cb) +dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t interfaceId, uint8_t *serverDUID, uint16_t serverDUIDType) { return NULL; }