Skip to content

Commit 6d2af0b

Browse files
author
Juha Heiskanen
committed
Dhcpv6 server update
Removed thread spesific code away from dhcpv6 service and added address remove and add callback which do protocol spesific thing.
1 parent ab1260a commit 6d2af0b

File tree

5 files changed

+60
-155
lines changed

5 files changed

+60
-155
lines changed

source/DHCPv6_Server/DHCPv6_Server_service.c

Lines changed: 32 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "DHCPv6_Server/DHCPv6_server_service.h"
3535
#include "common_functions.h"
3636
#include "NWK_INTERFACE/Include/protocol.h"
37-
#include "6LoWPAN/Thread/thread_bbr_api_internal.h"
3837
#include "Common_Protocols/icmpv6.h"
3938
#include "dhcp_service_api.h"
4039

@@ -57,9 +56,6 @@ static int8_t dhcpv6_service_tasklet = -1;
5756

5857
static arm_event_storage_t *dhcp_timer_storage = NULL;
5958

60-
static void DHCPV6_server_service_remove_GUA_from_neighcache(protocol_interface_info_entry_t *cur, uint8_t *targetAddress);
61-
62-
6359
static bool DHCP_server_service_timer_start(void)
6460
{
6561
if(!dhcp_timer_storage) {
@@ -101,19 +97,16 @@ int DHCPv6_server_respond_client(dhcpv6_gua_server_entry_s *serverBase, dhcpv6_r
10197
nonTemporalAddress.validLifeTime = dhcp_allocated_address->lifetime;
10298
nonTemporalAddress.preferredLifeTime = dhcp_allocated_address->preferredLifetime;
10399

104-
// If this is solicit from existing address, flush ND cache.
105-
if (allocateNew) {
106-
// coverity[returned_null] for ignoring protocol_stack_interface_info_get_by_id NULL return
107-
DHCPV6_server_service_remove_GUA_from_neighcache(protocol_stack_interface_info_get_by_id(serverBase->interfaceId), nonTemporalAddress.requestedAddress);
108-
}
109-
if (thread_bbr_nd_entry_add(serverBase->interfaceId,dhcp_allocated_address->nonTemporalAddress, nonTemporalAddress.validLifeTime, serverBase->guaPrefix) == -1) {
110-
// No nanostack BBR present we will put entry for application implemented BBR
111-
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);
112-
if (!route) {
100+
if (serverBase->addCb) {
101+
dhcp_address_cache_update_t update_info;
102+
update_info.allocatedAddress = dhcp_allocated_address->nonTemporalAddress;
103+
update_info.allocatedNewAddress = allocateNew;
104+
update_info.validLifeTime = nonTemporalAddress.validLifeTime;
105+
106+
if (!serverBase->addCb(serverBase->interfaceId, &update_info, serverBase->guaPrefix)) {
113107
address_allocated = false;
114108
libdhcpv6_address_rm_from_allocated_list(serverBase,dhcp_allocated_address->nonTemporalAddress);
115109
}
116-
117110
}
118111
}
119112

@@ -213,30 +206,6 @@ void DHCPv6_server_service_tasklet(arm_event_s *event)
213206
}
214207
}
215208

216-
static void DHCPV6_server_service_remove_GUA_from_neighcache(protocol_interface_info_entry_t *cur, uint8_t *targetAddress)
217-
{
218-
ipv6_neighbour_t *neighbour_entry;
219-
220-
neighbour_entry = ipv6_neighbour_lookup(&cur->ipv6_neighbour_cache, targetAddress);
221-
if (neighbour_entry) {
222-
tr_debug("Remove from neigh Cache: %s", tr_ipv6(targetAddress));
223-
ipv6_neighbour_entry_remove(&cur->ipv6_neighbour_cache, neighbour_entry);
224-
}
225-
}
226-
227-
void DHCPv6_server_service_address_preferred_timeout_handler(int8_t interfaceId, uint8_t *targetAddress)
228-
{
229-
tr_warn("Address Preferred Timeout");
230-
protocol_interface_info_entry_t *cur = 0;
231-
//allocate Socket Service
232-
233-
cur = protocol_stack_interface_info_get_by_id(interfaceId);
234-
if (cur) {
235-
ipv6_route_delete(targetAddress, 128, interfaceId, NULL, ROUTE_THREAD_PROXIED_HOST);
236-
DHCPV6_server_service_remove_GUA_from_neighcache(cur, targetAddress);
237-
}
238-
}
239-
240209
static int8_t dhcpv6_server_service_tasklet_generated(void)
241210
{
242211
if (dhcpv6_service_tasklet == -1) {
@@ -274,7 +243,7 @@ int DHCPv6_server_service_init(int8_t interface, uint8_t guaPrefix[static 16], u
274243
retVal = -2;
275244
} else {
276245
//allocate server
277-
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);
246+
dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_gua_server_allocate(guaPrefix, interface, cur->mac, DHCPV6_DUID_HARDWARE_EUI64_TYPE);
278247
if (serverInfo) {
279248
//if_address_entry_t *def_address = NULL;
280249
uint8_t temp[16];
@@ -303,25 +272,6 @@ void DHCPv6_server_service_timeout_cb(uint32_t timeUpdateInSeconds)
303272
libdhcpv6_gua_servers_time_update(timeUpdateInSeconds);
304273
}
305274

306-
void DHCPv6_GUA64_ML64_route_control(int8_t interfaceId, uint8_t *allocatedGuaAddress, uint8_t *clientEUID64, uint8_t *meshLocalPrefix, bool deleteMapping)
307-
{
308-
uint8_t ml64[16];
309-
uint8_t *ptr = ml64;
310-
memcpy(ptr, meshLocalPrefix, 8);
311-
ptr += 8;
312-
memcpy(ptr, clientEUID64, 8);
313-
*ptr ^= 2;
314-
//Generate Route Info
315-
if (deleteMapping) {
316-
ipv6_route_delete(allocatedGuaAddress, 128, interfaceId, ml64, ROUTE_STATIC);
317-
} else if (ipv6_route_add(allocatedGuaAddress, 128, interfaceId, ml64, ROUTE_STATIC, 0xffffffff, 0) == 0) {
318-
tr_debug("Route ADD OK");
319-
} else {
320-
tr_warn("Route Add fail");
321-
}
322-
323-
}
324-
325275
/* Delete dhcp thread dhcp router ID server.
326276
*
327277
* 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
332282
*/
333283
void DHCPv6_server_service_delete(int8_t interface, uint8_t guaPrefix[static 16], bool delete_gua_addresses)
334284
{
335-
protocol_interface_info_entry_t *curPtr = 0;
336-
//allocate Socket Service
337-
338-
curPtr = protocol_stack_interface_info_get_by_id(interface);
339-
if (curPtr) {
340-
dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix);
341-
if (serverInfo) {
342-
ns_list_foreach_safe(dhcpv6_alloacted_address_entry_t, cur, &serverInfo->allocatedAddressList) {
343-
//Delete Server data base
344-
DHCPV6_server_service_remove_GUA_from_neighcache(curPtr, cur->nonTemporalAddress);
285+
dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix);
286+
if (serverInfo) {
287+
ns_list_foreach_safe(dhcpv6_alloacted_address_entry_t, cur, &serverInfo->allocatedAddressList) {
288+
//Delete Server data base
289+
if (serverInfo->removeCb) {
290+
serverInfo->removeCb(interface, cur->nonTemporalAddress, NULL);
345291
}
292+
}
293+
if (serverInfo->removeCb) {
346294
// Clean all /128 'Thread Proxy' routes to self and others added when acting as a DHCP server
347-
ipv6_route_table_remove_info(curPtr->id, ROUTE_THREAD_PROXIED_HOST,serverInfo->guaPrefix);
348-
dhcp_service_delete(serverInfo->socketInstance_id);
295+
serverInfo->removeCb(interface, NULL, serverInfo->guaPrefix);
349296
}
297+
dhcp_service_delete(serverInfo->socketInstance_id);
298+
}
350299

351-
if (delete_gua_addresses) {
300+
if (delete_gua_addresses) {
301+
protocol_interface_info_entry_t *curPtr = protocol_stack_interface_info_get_by_id(interface);
302+
if (curPtr) {
352303
protocol_core_dhcpv6_allocated_address_remove(curPtr, guaPrefix);
353304
}
305+
}
354306

355-
libdhcpv6_gua_server_free_by_prefix_and_interfaceid(guaPrefix, interface);
307+
libdhcpv6_gua_server_free_by_prefix_and_interfaceid(guaPrefix, interface);
308+
309+
DHCP_server_service_timer_stop();
356310

357-
DHCP_server_service_timer_stop();
358-
}
359311
}
360312

361313
/* 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
379331
return retVal;
380332
}
381333

382-
/* Enable or disable GUA64 Address mapping to ML64
383-
*
384-
*
385-
* /param interface interface id of this thread instance.
386-
* /param guaPrefix Prefix which will be removed
387-
* /param mode
388-
* /param meshLocalPrefix mesh local prefix for generate ML6 from client EUID64
389-
*/
390-
int DHCPv6_server_service_set_gua_address_mapping(int8_t interface, uint8_t guaPrefix[static 16], bool mode, uint8_t meshLocalPrefix[8])
334+
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)
391335
{
392-
int retVal = -1;
393-
dhcpv6_gua_server_entry_s *serverInfo;
394-
395-
serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix);
396-
if (serverInfo) {
397-
if (mode == true && meshLocalPrefix == NULL) {
398-
retVal = -2;
399-
} else {
400-
serverInfo->enableAddressMapping = mode;
401-
if (meshLocalPrefix) {
402-
memcpy(serverInfo->meshLocalPrefix, meshLocalPrefix, 8);
403-
}
404-
retVal = 0;
405-
}
336+
dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interface, guaPrefix);
337+
if (!serverInfo) {
338+
return;
406339
}
407340

408-
return retVal;
341+
serverInfo->addCb = add_cb;
342+
serverInfo->removeCb = remove_cb;
409343
}
410344

411345
/* 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
453387
}
454388
return retVal;
455389
}
456-
457-
int DHCPv6_server_service_gua_target_mac_check(int8_t interfaceId, const uint8_t *targetGUA, uint8_t *targetEUI64)
458-
{
459-
dhcpv6_gua_server_entry_s *serverInfo = libdhcpv6_server_data_get_by_prefix_and_interfaceid(interfaceId, targetGUA);
460-
if (serverInfo) {
461-
dhcpv6_alloacted_address_entry_t *entry = libdhcpv6_address_get_from_allocated_list(serverInfo, targetGUA);
462-
if (entry) {
463-
if (entry->preferredLifetime && (entry->linkType == DHCPV6_DUID_HARDWARE_EUI64_TYPE)) {
464-
memcpy(targetEUI64, entry->linkId, 8);
465-
return 0;
466-
}
467-
}
468-
}
469-
return -1;
470-
}
471390
#else
472391

473392
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
497416

498417
return -1;
499418
}
500-
int DHCPv6_server_service_set_gua_address_mapping(int8_t interface, uint8_t guaPrefix[static 16], bool mode, uint8_t meshLocalPrefix[8])
501-
{
502-
(void) interface;
503-
(void) guaPrefix;
504-
(void) mode;
505-
(void) meshLocalPrefix;
506-
507-
return -1;
508-
}
509419
int DHCPv6_server_service_set_max_clients_accepts_count(int8_t interface, uint8_t guaPrefix[static 16], uint32_t maxClientCount)
510420
{
511421
(void) interface;
@@ -522,13 +432,5 @@ int DHCPv6_server_service_set_address_validlifetime(int8_t interface, uint8_t gu
522432

523433
return -1;
524434
}
525-
int DHCPv6_server_service_gua_target_mac_check(int8_t interfaceId, const uint8_t *targetGUA, uint8_t *targetEUI64)
526-
{
527-
(void) interfaceId;
528-
(void) targetGUA;
529-
(void) targetEUI64;
530-
531-
return -1;
532-
}
533435

534436
#endif

source/DHCPv6_Server/DHCPv6_server_service.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
#ifndef DHCPV6_SERVER_SERVICE_H_
2525
#define DHCPV6_SERVER_SERVICE_H_
26+
27+
#include "libDHCPv6/libDHCPv6.h"
28+
#include "libDHCPv6/libDHCPv6_server.h"
29+
2630
/* Initialize dhcp Global address server.
2731
*
2832
* This instance needs to bee initialized once for each thread network interface.
@@ -38,6 +42,10 @@
3842
*/
3943
int DHCPv6_server_service_init(int8_t interface, uint8_t guaPrefix[static 16], uint8_t serverDUID[static 8], uint16_t serverDUIDType);
4044

45+
46+
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);
47+
48+
4149
/* Delete dhcp thread dhcp router ID server.
4250
*
4351
* 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]
5159

5260
void DHCPv6_server_service_timeout_cb(uint32_t timeUpdateInSeconds);
5361

54-
//void DHCPv6_GUA64_ML64_route_control(int8_t interfaceId, uint8_t *allocatedGuaAddress, uint8_t *clientEUID64, uint8_t *meshLocalPrefix, bool deleteMapping);
55-
5662
/* Control GUA address for client by DUI.Default value is true
5763
*
5864
*
@@ -63,16 +69,6 @@ void DHCPv6_server_service_timeout_cb(uint32_t timeUpdateInSeconds);
6369
int DHCPv6_server_service_set_address_autonous_flag(int8_t interface, uint8_t guaPrefix[static 16], bool mode);
6470

6571

66-
/* Enable or disable GUA64 Address mapping to ML64
67-
*
68-
*
69-
* /param interface interface id of this thread instance.
70-
* /param guaPrefix Prefix which will be removed
71-
* /param mode
72-
* /param meshLocalPrefix mesh local prefix for generate ML6 from client EUID64
73-
*/
74-
int DHCPv6_server_service_set_gua_address_mapping(int8_t interface, uint8_t guaPrefix[static 16], bool mode, uint8_t meshLocalPrefix[8]);
75-
7672
/* SET max accepted clients to server, Default is 200
7773
*
7874
*
@@ -92,6 +88,4 @@ int DHCPv6_server_service_set_max_clients_accepts_count(int8_t interface, uint8_
9288
*/
9389
int DHCPv6_server_service_set_address_validlifetime(int8_t interface, uint8_t guaPrefix[static 16], uint32_t validLifeTimne);
9490

95-
96-
int DHCPv6_server_service_gua_target_mac_check(int8_t interfaceId, const uint8_t *targetGUA, uint8_t *targetEUI64);
9791
#endif /* DHCPV6_SERVER_SERVICE_H_ */

source/libDHCPv6/libDHCPv6_server.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ static dhcpv6_gua_server_entry_s *libdhcpv6_server_entry_allocate(void)
4343
dhcpv6_gua_server_entry_s *entry = ns_dyn_mem_alloc(sizeof(dhcpv6_gua_server_entry_s));
4444
if (entry) {
4545
entry->clientIdSequence = 0;
46-
entry->enableAddressMapping = false;
4746
entry->enableAddressAutonous = true;
4847
entry->clientIdDefaultSuffics = 0x0000000;
4948
entry->maxSuppertedClients = 200;
5049
entry->validLifetime = 7200;
50+
entry->removeCb = NULL;
51+
entry->addCb = NULL;
5152
ns_list_init(&entry->allocatedAddressList);
5253
}
5354
return entry;
@@ -96,7 +97,9 @@ void libdhcpv6_gua_servers_time_update(uint32_t timeUpdateInSeconds)
9697
//Stop use this address for leasequery and delete Route or address map
9798

9899
address->preferredLifetime = 0;
99-
cur->timeoutCb(cur->interfaceId, address->nonTemporalAddress);
100+
if (cur->removeCb) {
101+
cur->removeCb(cur->interfaceId, address->nonTemporalAddress, cur->guaPrefix);
102+
}
100103
} else {
101104
address->preferredLifetime -= timeUpdateInSeconds;
102105
}
@@ -137,7 +140,7 @@ dhcpv6_gua_server_entry_s *libdhcpv6_server_data_get_by_prefix_and_socketinstanc
137140
}
138141

139142

140-
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)
143+
dhcpv6_gua_server_entry_s *libdhcpv6_gua_server_allocate(uint8_t *prefix, int8_t interfaceId, uint8_t *serverDUID, uint16_t serverDUIDType)
141144
{
142145
dhcpv6_gua_server_entry_s *entry = NULL;
143146
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
147150
memcpy(entry->serverDUID, serverDUID, 8);
148151
entry->serverLinkType = serverDUIDType;
149152
entry->interfaceId = interfaceId;
150-
entry->timeoutCb = prefered_timeout_cb;
151153
ns_list_add_to_end(&dhcpv6_gua_server_list, entry);
152154
}
153155
}

0 commit comments

Comments
 (0)