Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers network_lib.c Source File

network_lib.c

00001 /*
00002  * Copyright (c) 2013-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "nsconfig.h"
00019 #include "ns_types.h"
00020 #include <string.h>
00021 #include "Core/include/ns_socket.h"
00022 #include "nsdynmemLIB.h"
00023 #include "ns_trace.h"
00024 #include "NWK_INTERFACE/Include/protocol.h"
00025 #include "Common_Protocols/icmpv6.h"
00026 #include "Common_Protocols/udp.h"
00027 #include "6LoWPAN/Bootstraps/network_lib.h"
00028 #include "MLE/mle.h"
00029 #include "shalib.h"
00030 #include "Common_Protocols/ipv6_constants.h"
00031 
00032 #ifdef PANA
00033 #ifdef ECC
00034 #include "libX509_V3.h"
00035 #include "ecc.h"
00036 #endif
00037 #include "Security/TLS/tls_lib.h"
00038 #include "Security/Common/sec_lib.h"
00039 #include "net_nvm_api.h"
00040 #include "Security/PANA/pana.h"
00041 #include "ccmLIB.h"
00042 #endif
00043 #include "6LoWPAN/ND/nd_router_object.h"
00044 #include "6LoWPAN/IPHC_Decode/cipv6.h"
00045 #include "net_nwk_scan.h"
00046 #include "nwk_stats_api.h"
00047 #include "NWK_INTERFACE/Include/protocol_stats.h"
00048 #ifdef HAVE_RPL
00049 #include "RPL/rpl_data.h"
00050 #endif
00051 
00052 #include "platform/arm_hal_interrupt.h"
00053 #include "common_functions.h"
00054 #include "6LoWPAN/Thread/thread_common.h"
00055 #include "6LoWPAN/Thread/thread_bootstrap.h"
00056 
00057 #define MAC_MLME_SACN_PERIO_PARAM 5
00058 
00059 #define TRACE_GROUP "nw"
00060 
00061 /**
00062  * \brief Check That Network Have IPv6 Address Ready.
00063  *
00064  * \return 1 when IPv6 address is valid and 0 when it is not valid
00065  */
00066 
00067 uint8_t nwk_ready(nwk_interface_id id)
00068 {
00069     uint8_t ret_val = 0;
00070     protocol_interface_info_entry_t *cur = 0;
00071     cur = protocol_stack_interface_info_get(id);
00072     if (cur) {
00073         if (thread_info(cur)) {
00074             if (thread_attach_ready(cur) == 0) {
00075                 ret_val = 1;
00076             }
00077         } else {
00078             if ((cur->lowpan_info & INTERFACE_NWK_BOOTSRAP_ADDRESS_REGISTER_READY)) {
00079                 ret_val =  1;
00080             }
00081         }
00082     }
00083     return ret_val;
00084 
00085 }
00086 
00087 void nwk_parent_poll_fail_cb(int8_t id)
00088 {
00089     protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(id);
00090     if (!cur) {
00091         tr_error("Data Poll Fail Event: No interface");
00092         return;
00093     }
00094     if (thread_info(cur)) {
00095         //Initialize Bootsrap
00096         thread_bootstrap_connection_error(cur->id, CON_ERROR_POLL, NULL);
00097     } else {
00098         nwk_bootsrap_state_update(ARM_NWK_NWK_PARENT_POLL_FAIL, cur);
00099     }
00100 
00101 }
00102 
00103 void network_library_init(void)
00104 {
00105     socket_init();
00106     address_module_init();
00107     //SET MAC_MLME Handler
00108     protocol_init();
00109 }
00110 
00111 buffer_t *nwk_udp_rx_security_check(buffer_t *buf)
00112 {
00113     protocol_interface_info_entry_t *cur = buf->interface ;
00114     uint8_t drop_unsecured = 0;
00115 
00116     // Hack for PANA and MLE. PANA socket is not unsecured, need to allow unsecured link local traffic.
00117     // MLE need to allow joiner request, that is not secured.
00118     // TODO: Check if there is better fix for these.
00119     if (buf->src_sa .port  == UDP_PORT_PANA || buf->dst_sa .port  == UDP_PORT_PANA) {
00120         if ((buf->dst_sa .address [0] != 0xfe)  && (buf->options .ll_security_bypass_rx )) {
00121             drop_unsecured = 1;
00122         }
00123     } else if (buf->dst_sa .port  == UDP_PORT_MLE) {
00124         // OK
00125     } else if (buf->options .ll_security_bypass_rx ) {
00126         if (addr_ipv6_scope(buf->src_sa .address , cur) > IPV6_SCOPE_LINK_LOCAL) {
00127             drop_unsecured = 1;
00128         } else {
00129             if (!buf->socket ) {
00130                 buffer_socket_set(buf, socket_lookup_ipv6(IPV6_NH_UDP, &buf->dst_sa , &buf->src_sa , true));
00131             }
00132             if (buf->socket  && buf->socket ->inet_pcb ->link_layer_security == 0) {
00133                 // non-secure okay if it's for a socket whose security flag is clear.
00134             } else {
00135                 drop_unsecured = 1;
00136             }
00137         }
00138     }
00139 
00140     if (drop_unsecured) {
00141         tr_warn("Drop UDP Unsecured");
00142         buf = buffer_free(buf);
00143     }
00144 
00145     return buf;
00146 }
00147