|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "nsconfig.h" |
| 19 | +#if defined(HAVE_THREAD) && defined(HAVE_DHCPV6_SERVER) |
| 20 | +#include <string.h> |
| 21 | +#include <ns_types.h> |
| 22 | +#include "eventOS_event.h" |
| 23 | +#include "eventOS_event_timer.h" |
| 24 | +#include "common_functions.h" |
| 25 | +#include "ns_trace.h" |
| 26 | +#include "NWK_INTERFACE/Include/protocol.h" |
| 27 | +#include "ipv6_stack/protocol_ipv6.h" |
| 28 | +#include "Common_Protocols/ipv6_constants.h" |
| 29 | +#include "Common_Protocols/ipv6.h" |
| 30 | +#include "DHCPv6_Server/DHCPv6_server_service.h" |
| 31 | +#include "6LoWPAN/Thread/thread_bbr_api_internal.h" |
| 32 | + |
| 33 | +#define TRACE_GROUP "thds" |
| 34 | + |
| 35 | +static void thread_service_remove_GUA_from_neighcache(protocol_interface_info_entry_t *cur, uint8_t *targetAddress) |
| 36 | +{ |
| 37 | + ipv6_neighbour_t *neighbour_entry; |
| 38 | + |
| 39 | + neighbour_entry = ipv6_neighbour_lookup(&cur->ipv6_neighbour_cache, targetAddress); |
| 40 | + if (neighbour_entry) { |
| 41 | + tr_debug("Remove from neigh Cache: %s", tr_ipv6(targetAddress)); |
| 42 | + ipv6_neighbour_entry_remove(&cur->ipv6_neighbour_cache, neighbour_entry); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +static void thread_dhcp_address_prefer_remove_cb(int8_t interfaceId, uint8_t *targetAddress, void *prefix_info) |
| 47 | +{ |
| 48 | + protocol_interface_info_entry_t *curPtr = protocol_stack_interface_info_get_by_id(interfaceId); |
| 49 | + if (!curPtr) { |
| 50 | + return; |
| 51 | + } |
| 52 | + if (!targetAddress) { |
| 53 | + //Clear All targets routes |
| 54 | + ipv6_route_table_remove_info(interfaceId, ROUTE_THREAD_PROXIED_HOST,prefix_info); |
| 55 | + } else { |
| 56 | + ipv6_route_delete(targetAddress, 128, interfaceId, NULL, ROUTE_THREAD_PROXIED_HOST); |
| 57 | + thread_service_remove_GUA_from_neighcache(curPtr, targetAddress); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +static bool thread_dhcp_address_add_cb(int8_t interfaceId, dhcp_address_cache_update_t *address_info, void *route_src) |
| 64 | +{ |
| 65 | + protocol_interface_info_entry_t *curPtr = protocol_stack_interface_info_get_by_id(interfaceId); |
| 66 | + if (!curPtr) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + // If this is solicit from existing address, flush ND cache. |
| 71 | + if (address_info->allocatedNewAddress) { |
| 72 | + // coverity[returned_null] for ignoring protocol_stack_interface_info_get_by_id NULL return |
| 73 | + thread_service_remove_GUA_from_neighcache(curPtr, address_info->allocatedAddress); |
| 74 | + } |
| 75 | + |
| 76 | + if (thread_bbr_nd_entry_add(interfaceId,address_info->allocatedAddress, address_info->validLifeTime, route_src) == -1) { |
| 77 | + // No nanostack BBR present we will put entry for application implemented BBR |
| 78 | + ipv6_route_t *route = ipv6_route_add_with_info(address_info->allocatedAddress, 128, interfaceId, NULL, ROUTE_THREAD_PROXIED_HOST,route_src,0, address_info->validLifeTime, 0); |
| 79 | + if (!route) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +int thread_dhcp6_server_init(int8_t interface_id, uint8_t prefix[8], uint8_t eui64[8], uint32_t validLifeTimne) |
| 88 | +{ |
| 89 | + if (DHCPv6_server_service_init(interface_id, prefix, eui64, DHCPV6_DUID_HARDWARE_EUI64_TYPE) != 0) { |
| 90 | + return -1; |
| 91 | + } |
| 92 | + //Register Callbacks |
| 93 | + DHCPv6_server_service_callback_set(interface_id, prefix, thread_dhcp_address_prefer_remove_cb, thread_dhcp_address_add_cb); |
| 94 | + //SET Timeout |
| 95 | + DHCPv6_server_service_set_address_validlifetime(interface_id, prefix, validLifeTimne); |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +#endif |
0 commit comments