Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thread_dhcpv6_server.c Source File

thread_dhcpv6_server.c

00001 /*
00002  * Copyright (c) 2015, 2018-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: BSD-3-Clause
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holder nor the
00014  *    names of its contributors may be used to endorse or promote products
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 #include "nsconfig.h"
00032 #if defined(HAVE_THREAD) && defined(HAVE_DHCPV6_SERVER)
00033 #include <string.h>
00034 #include <ns_types.h>
00035 #include "eventOS_event.h"
00036 #include "eventOS_event_timer.h"
00037 #include "common_functions.h"
00038 #include "ns_trace.h"
00039 #include "NWK_INTERFACE/Include/protocol.h"
00040 #include "ipv6_stack/protocol_ipv6.h"
00041 #include "Common_Protocols/ipv6_constants.h"
00042 #include "Common_Protocols/ipv6.h"
00043 #include "DHCPv6_Server/DHCPv6_server_service.h"
00044 #include "6LoWPAN/Thread/thread_bbr_api_internal.h"
00045 
00046 #define TRACE_GROUP "thds"
00047 
00048 static void thread_service_remove_GUA_from_neighcache(protocol_interface_info_entry_t *cur, uint8_t *targetAddress)
00049 {
00050     ipv6_neighbour_t *neighbour_entry;
00051 
00052     neighbour_entry = ipv6_neighbour_lookup(&cur->ipv6_neighbour_cache, targetAddress);
00053     if (neighbour_entry) {
00054         tr_debug("Remove from neigh Cache: %s", tr_ipv6(targetAddress));
00055         ipv6_neighbour_entry_remove(&cur->ipv6_neighbour_cache, neighbour_entry);
00056     }
00057 }
00058 
00059 static void thread_dhcp_address_prefer_remove_cb(int8_t interfaceId, uint8_t *targetAddress, void *prefix_info)
00060 {
00061     protocol_interface_info_entry_t *curPtr = protocol_stack_interface_info_get_by_id(interfaceId);
00062     if (!curPtr) {
00063         return;
00064     }
00065     if (!targetAddress) {
00066         //Clear All targets routes
00067         ipv6_route_table_remove_info(interfaceId, ROUTE_THREAD_PROXIED_HOST, prefix_info);
00068     } else {
00069         tr_debug("Address Preferred Timeout");
00070         ipv6_route_delete(targetAddress, 128, interfaceId, NULL, ROUTE_THREAD_PROXIED_HOST);
00071         thread_service_remove_GUA_from_neighcache(curPtr, targetAddress);
00072 
00073     }
00074 
00075 }
00076 
00077 static bool thread_dhcp_address_add_cb(int8_t interfaceId, dhcp_address_cache_update_t *address_info, void *route_src)
00078 {
00079     protocol_interface_info_entry_t *curPtr = protocol_stack_interface_info_get_by_id(interfaceId);
00080     if (!curPtr) {
00081         return false;
00082     }
00083 
00084     // If this is solicit from existing address, flush ND cache.
00085     if (address_info->allocatedNewAddress) {
00086         // coverity[returned_null] for ignoring protocol_stack_interface_info_get_by_id NULL return
00087         thread_service_remove_GUA_from_neighcache(curPtr, address_info->allocatedAddress);
00088     }
00089 
00090     if (thread_bbr_nd_entry_add(interfaceId, address_info->allocatedAddress, address_info->validLifeTime, route_src) == -1) {
00091         // No nanostack BBR present we will put entry for application implemented BBR
00092         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);
00093         if (!route) {
00094             return false;
00095         }
00096 
00097     }
00098     return true;
00099 }
00100 
00101 int thread_dhcp6_server_init(int8_t interface_id, uint8_t prefix[8], uint8_t eui64[8], uint32_t validLifeTimne)
00102 {
00103     if (DHCPv6_server_service_init(interface_id, prefix, eui64, DHCPV6_DUID_HARDWARE_EUI64_TYPE) != 0) {
00104         return -1;
00105     }
00106     //Register Callbacks
00107     DHCPv6_server_service_callback_set(interface_id, prefix, thread_dhcp_address_prefer_remove_cb, thread_dhcp_address_add_cb);
00108     //SET Timeout
00109     DHCPv6_server_service_set_address_validlifetime(interface_id, prefix, validLifeTimne);
00110 
00111     return 0;
00112 }
00113 
00114 #endif