diff --git a/.project b/.project index 47e5d3e..52d209e 100644 --- a/.project +++ b/.project @@ -6,6 +6,11 @@ RTL00_SDKV35a + + org.python.pydev.PyDevBuilder + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder clean,full,incremental, @@ -24,6 +29,7 @@ org.eclipse.cdt.core.ccnature org.eclipse.cdt.managedbuilder.core.managedBuildNature org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + org.python.pydev.pythonNature diff --git a/Makefile b/Makefile index 5158de2..a70728c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ mp: ram_all_mp .PHONY: ram_all ram_all: @$(MAKE) -f $(SDK_PATH)sdkbuild.mk - @$(MAKE) -f $(SDK_PATH)flasher.mk genbin1 genbin23 + @$(MAKE) -f $(SDK_PATH)flasher.mk genbin .PHONY: ram_all_mp ram_all_mp: diff --git a/USDK/Makefile b/USDK/Makefile index 5158de2..a70728c 100644 --- a/USDK/Makefile +++ b/USDK/Makefile @@ -6,7 +6,7 @@ mp: ram_all_mp .PHONY: ram_all ram_all: @$(MAKE) -f $(SDK_PATH)sdkbuild.mk - @$(MAKE) -f $(SDK_PATH)flasher.mk genbin1 genbin23 + @$(MAKE) -f $(SDK_PATH)flasher.mk genbin .PHONY: ram_all_mp ram_all_mp: diff --git a/USDK/component/common/api/wifi/wifi_conf.c b/USDK/component/common/api/wifi/wifi_conf.c index c9a19d3..fc42bfb 100644 --- a/USDK/component/common/api/wifi/wifi_conf.c +++ b/USDK/component/common/api/wifi/wifi_conf.c @@ -904,16 +904,17 @@ int wifi_off(void) { uint32 timeout = xTaskGetTickCount(); + if ((rltk_wlan_running(WLAN0_IDX) == 0) + && (rltk_wlan_running(WLAN1_IDX) == 0)) { + info_printf("WIFI is not running\n"); + wifi_mode = RTW_MODE_NONE; + return 0; + } #if CONFIG_LWIP_LAYER dhcps_deinit(); LwIP_DHCP(0, DHCP_STOP); LwIP_DHCP(1, DHCP_STOP); #endif - if ((rltk_wlan_running(WLAN0_IDX) == 0) - && (rltk_wlan_running(WLAN1_IDX) == 0)) { - info_printf("WIFI is not running\n"); - return 0; - } info_printf("Deinitializing WIFI ...\n"); #if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP diff --git a/USDK/component/common/api/wifi_api.c b/USDK/component/common/api/wifi_api.c index 5d529c2..a66676c 100644 --- a/USDK/component/common/api/wifi_api.c +++ b/USDK/component/common/api/wifi_api.c @@ -43,7 +43,7 @@ #include "main.h" #include "wifi_user_set.h" -#if 1 +#if 0 #undef debug_printf #define debug_printf(fmt, ...) rtl_printf(fmt, ##__VA_ARGS__) #undef info_printf @@ -701,6 +701,9 @@ int wifi_run(rtw_mode_t mode) { switch(mode) { case RTW_MODE_STA_AP: ret = wifi_run_ap() | wifi_run_st(); +#if IP_NAPT + xnetif[WLAN_AP_NETIF_NUM].napt = 1; +#endif // _wext_enable_powersave(0, 0, 0); break; case RTW_MODE_STA: diff --git a/USDK/component/common/api/wifi_api.h b/USDK/component/common/api/wifi_api.h index 5734950..9d2484f 100644 --- a/USDK/component/common/api/wifi_api.h +++ b/USDK/component/common/api/wifi_api.h @@ -10,6 +10,7 @@ #include "wifi_constants.h" #include "queue.h" +/* Get one byte from the 4-byte address */ #ifndef ip4_addr1 #define ip4_addr1(ipaddr) (((uint8_t*)(ipaddr))[0]) #define ip4_addr2(ipaddr) (((uint8_t*)(ipaddr))[1]) diff --git a/USDK/component/common/drivers/wlan/realtek/include/ieee80211.h b/USDK/component/common/drivers/wlan/realtek/include/ieee80211.h index 2ef7f75..bdaca57 100644 --- a/USDK/component/common/drivers/wlan/realtek/include/ieee80211.h +++ b/USDK/component/common/drivers/wlan/realtek/include/ieee80211.h @@ -242,7 +242,7 @@ typedef struct ieee_param { u16 capability; int flags; u8 tx_supp_rates[16]; - struct rtw_ieee80211_ht_cap ht_cap; + struct rtw_ieee80211_ht_cap ht_cap; } add_sta; struct { u8 reserved[2];//for set max_num_sta diff --git a/USDK/component/common/drivers/wlan/realtek/include/wlan_bssdef.h b/USDK/component/common/drivers/wlan/realtek/include/wlan_bssdef.h index 2c86f31..756dcd4 100644 --- a/USDK/component/common/drivers/wlan/realtek/include/wlan_bssdef.h +++ b/USDK/component/common/drivers/wlan/realtek/include/wlan_bssdef.h @@ -665,7 +665,7 @@ __inline static uint get_WLAN_BSSID_EX_sz(WLAN_BSSID_EX *bss) } struct wlan_network { - _list list; + struct list_head list; int network_type; //refer to ieee80211.h for WIRELESS_11A/B/G int fixed; // set to fixed when not to be removed as site-surveying unsigned long last_scanned; //timestamp for the network diff --git a/USDK/component/common/drivers/wlan/realtek/src/osdep/lwip_intf.c b/USDK/component/common/drivers/wlan/realtek/src/osdep/lwip_intf.c index 57958d1..fd69474 100644 --- a/USDK/component/common/drivers/wlan/realtek/src/osdep/lwip_intf.c +++ b/USDK/component/common/drivers/wlan/realtek/src/osdep/lwip_intf.c @@ -153,8 +153,18 @@ void rltk_wlan_recv(int idx, struct eth_drv_sg *sg_list, int sg_len) #endif } +/* uses in void __fastcall rltk_netif_rx(sk_buff *skb) */ + int netif_is_valid_IP(int idx, unsigned char *ip_dest) { +#if IP_FORWARD // add pvvx + return 1; +#elif defined(CONFIG_DONT_CARE_TP) + if(pnetif->flags & NETIF_FLAG_IPSWITCH) + return 1; + else + return 0; +#else #if CONFIG_LWIP_LAYER == 1 #if DEVICE_EMAC struct netif *pnetif = xnetif[idx]; @@ -187,13 +197,9 @@ int netif_is_valid_IP(int idx, unsigned char *ip_dest) return 1; DBG_TRACE("invalid IP: %d.%d.%d.%d ",ip_dest[0],ip_dest[1],ip_dest[2],ip_dest[3]); -#endif -#ifdef CONFIG_DONT_CARE_TP - if(pnetif->flags & NETIF_FLAG_IPSWITCH) - return 1; - else -#endif return 0; +#endif // CONFIG_LWIP_LAYER +#endif // IP_FORWARD / CONFIG_DONT_CARE_TP } int netif_get_idx(struct netif *pnetif) diff --git a/USDK/component/common/network/dhcp/dhcps.c b/USDK/component/common/network/dhcp/dhcps.c index ad5be28..2b83bca 100644 --- a/USDK/component/common/network/dhcp/dhcps.c +++ b/USDK/component/common/network/dhcp/dhcps.c @@ -143,7 +143,11 @@ static void add_offer_options(uint8_t *option_start_address) The option specifies a list of DNS servers available to the client. */ temp_option_addr = fill_one_option_content(temp_option_addr, DHCP_OPTION_CODE_DNS_SERVER, DHCP_OPTION_LENGTH_FOUR, +#if IP_NAPT + (void *)&dhcps_local_gateway); +#else (void *)&dhcps_local_address); +#endif /* add DHCP options 51. This option is used to request a lease time for the IP address. */ temp_option_addr = fill_one_option_content(temp_option_addr, diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/bpstruct.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/bpstruct.h index 177758c..6a35c90 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/bpstruct.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/bpstruct.h @@ -30,7 +30,7 @@ * */ -#if defined(__IAR_SYSTEMS_ICC__) +#if defined(__IAR_SYSTEMS_ICC__)|| defined (__GNUC__) #pragma pack(1) #endif diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/cc.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/cc.h index be882d9..61958ea 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/cc.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/cc.h @@ -85,6 +85,7 @@ typedef int sys_prot_t; #define PACK_STRUCT_STRUCT __attribute__ ((__packed__)) #define PACK_STRUCT_END #define PACK_STRUCT_FIELD(x) x +#define PACK_STRUCT_USE_INCLUDES #elif defined (__TASKING__) diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/epstruct.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/epstruct.h index 1e1a049..b5f2738 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/epstruct.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/epstruct.h @@ -32,5 +32,7 @@ #if defined(__IAR_SYSTEMS_ICC__) #pragma pack() +#elif defined (__GNUC__) +#pragma pack() #endif diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/sys_arch.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/sys_arch.h index 83d0c08..2e841bd 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/sys_arch.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/arch/sys_arch.h @@ -42,6 +42,7 @@ #define SYS_DEFAULT_THREAD_STACK_DEPTH configMINIMAL_STACK_SIZE typedef xSemaphoreHandle sys_sem_t; +typedef xSemaphoreHandle sys_mutex_t; typedef xQueueHandle sys_mbox_t; typedef xTaskHandle sys_thread_t; diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/ethernetif.c b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/ethernetif.c index 71e6f18..1484a95 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/ethernetif.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/ethernetif.c @@ -114,10 +114,10 @@ static void low_level_init(struct netif *netif) netif->hwaddr_len = ETHARP_HWADDR_LEN; /* set netif maximum transfer unit */ - netif->mtu = 1500; + netif->mtu = netifMTU; /* Accept broadcast address and ARP traffic */ - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; /* Wlan interface is initialized later */ } diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/sys_arch.c b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/sys_arch.c index 4981b8f..49f2a58 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/sys_arch.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/sys_arch.c @@ -443,7 +443,6 @@ void sys_mutex_unlock(sys_mutex_t *mutex) thread() function. The id of the new thread is returned. Both the id and the priority are system dependent. */ -#if 0 sys_thread_t sys_thread_new_tcm(const char *name, lwip_thread_fn thread , void *arg, int stacksize, int prio) { xTaskHandle CreatedTask; @@ -456,6 +455,9 @@ int result; { void *stack_addr = tcm_heap_malloc(stacksize * sizeof(int)); + if(stack_addr == NULL){ + } + result = xTaskGenericCreate( thread, ( signed portCHAR * ) name, @@ -489,7 +491,6 @@ int result; return NULL; } } -#endif /*-----------------------------------------------------------------------------------*/ // TODO /*-----------------------------------------------------------------------------------*/ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/bpstruct.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/bpstruct.h deleted file mode 100644 index 177758c..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/bpstruct.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ - -#if defined(__IAR_SYSTEMS_ICC__) -#pragma pack(1) -#endif - diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/cc.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/cc.h deleted file mode 100644 index 1d6a5fa..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/cc.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __CC_H__ -#define __CC_H__ - -#include "cpu.h" - -typedef unsigned char u8_t; -typedef signed char s8_t; -typedef unsigned short u16_t; -typedef signed short s16_t; -typedef unsigned int u32_t; -typedef signed int s32_t; -typedef u32_t mem_ptr_t; -typedef int sys_prot_t; - - -#define U16_F "hu" -#define S16_F "d" -#define X16_F "hx" -#define U32_F "u" -#define S32_F "d" -#define X32_F "x" -#define SZT_F "uz" - - - - - -/* define compiler specific symbols */ -#if defined (__ICCARM__) - -#define PACK_STRUCT_BEGIN -#define PACK_STRUCT_STRUCT -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x -#define PACK_STRUCT_USE_INCLUDES - -#elif defined (__CC_ARM) - -#define PACK_STRUCT_BEGIN __packed -#define PACK_STRUCT_STRUCT -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x - -#elif defined (__GNUC__) - -#define PACK_STRUCT_BEGIN -#define PACK_STRUCT_STRUCT __attribute__ ((__packed__)) -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x - -#elif defined (__TASKING__) - -#define PACK_STRUCT_BEGIN -#define PACK_STRUCT_STRUCT -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x - -#endif - -#define LWIP_PLATFORM_ASSERT(x) //do { if(!(x)) while(1); } while(0) - -#endif /* __CC_H__ */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/cpu.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/cpu.h deleted file mode 100644 index a02f86d..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/cpu.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __CPU_H__ -#define __CPU_H__ - -#define BYTE_ORDER LITTLE_ENDIAN - -#endif /* __CPU_H__ */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/epstruct.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/epstruct.h deleted file mode 100644 index 1e1a049..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/epstruct.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ - -#if defined(__IAR_SYSTEMS_ICC__) -#pragma pack() -#endif - diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/init.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/init.h deleted file mode 100644 index e622c73..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/init.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __ARCH_INIT_H__ -#define __ARCH_INIT_H__ - -#define TCPIP_INIT_DONE(arg) tcpip_init_done(arg) - -void tcpip_init_done(void *); -int wait_for_tcpip_init(void); - -#endif /* __ARCH_INIT_H__ */ - - - - diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/lib.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/lib.h deleted file mode 100644 index 378f25b..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/lib.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __LIB_H__ -#define __LIB_H__ - -#include - - -#endif /* __LIB_H__ */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/perf.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/perf.h deleted file mode 100644 index 334d42a..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/perf.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __PERF_H__ -#define __PERF_H__ - -#define PERF_START /* null definition */ -#define PERF_STOP(x) /* null definition */ - -#endif /* __PERF_H__ */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/sys_arch.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/sys_arch.h deleted file mode 100644 index 2e841bd..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/arch/sys_arch.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __SYS_RTXC_H__ -#define __SYS_RTXC_H__ - -#include "FreeRTOS.h" -#include "task.h" -#include "queue.h" -#include "semphr.h" - -#define SYS_MBOX_NULL (xQueueHandle)0 -#define SYS_SEM_NULL (xSemaphoreHandle)0 -#define SYS_DEFAULT_THREAD_STACK_DEPTH configMINIMAL_STACK_SIZE - -typedef xSemaphoreHandle sys_sem_t; -typedef xSemaphoreHandle sys_mutex_t; -typedef xQueueHandle sys_mbox_t; -typedef xTaskHandle sys_thread_t; - -typedef struct _sys_arch_state_t -{ - // Task creation data. - char cTaskName[configMAX_TASK_NAME_LEN]; - unsigned short nStackDepth; - unsigned short nTaskCount; -} sys_arch_state_t; - - - -//extern sys_arch_state_t s_sys_arch_state; - -//void sys_set_default_state(); -//void sys_set_state(signed char *pTaskName, unsigned short nStackSize); - -/* Message queue constants. */ -#define archMESG_QUEUE_LENGTH ( 6 ) -#endif /* __SYS_RTXC_H__ */ - diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/MFC6A0B.tmp b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/MFC6A0B.tmp deleted file mode 100644 index 5097f29..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/MFC6A0B.tmp +++ /dev/null @@ -1,489 +0,0 @@ -/** -* @file -* Ethernet Interface Skeleton -* -*/ - -/* -* Copyright (c) 2001-2004 Swedish Institute of Computer Science. -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without modification, -* are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, -* this list of conditions and the following disclaimer. -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* 3. The name of the author may not be used to endorse or promote products -* derived from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED -* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT -* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT -* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING -* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY -* OF SUCH DAMAGE. -* -* This file is part of the lwIP TCP/IP stack. -* -* Author: Adam Dunkels -* -*/ - -/* -* This file is a skeleton for developing Ethernet network interface -* drivers for lwIP. Add code to the low_level functions and do a -* search-and-replace for the word "ethernetif" to replace it with -* something that better describes your network interface. -*/ - -#include "lwip/opt.h" -#include "lwip/def.h" -#include "lwip/mem.h" -#include "lwip/pbuf.h" -//#include "lwip/sys.h" -//#include "lwip/tcpip.h" -//#include "lwip/icmp.h" -#include "lwip/lwip_timers.h" -#include "netif/etharp.h" -#include "err.h" -#include "ethernetif.h" -//#include "queue.h" - -#include "main.h" // for the definition of CONFIG_WLAN - -#if !CONFIG_WLAN -#include "stm32f2x7_eth.h" -#else -#include -#endif -#include - -#include - - -#define netifMTU (1500) -#define netifINTERFACE_TASK_STACK_SIZE ( 350 ) -#define netifINTERFACE_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) -#define netifGUARD_BLOCK_TIME ( 250 ) -/* The time to block waiting for input. */ -#define emacBLOCK_TIME_WAITING_FOR_INPUT ( ( portTickType ) 100 ) - -#if !CONFIG_WLAN -/* Define those to better describe your network interface. */ -#define IFNAME0 's' -#define IFNAME1 't' - -static struct netif *s_pxNetIf = NULL; -xSemaphoreHandle s_xSemaphore = NULL; - - -/* Ethernet Rx & Tx DMA Descriptors */ -extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB]; - -/* Ethernet Receive buffers */ -extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; - -/* Ethernet Transmit buffers */ -extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; - -/* Global pointers to track current transmit and receive descriptors */ -extern ETH_DMADESCTypeDef *DMATxDescToSet; -extern ETH_DMADESCTypeDef *DMARxDescToGet; - -/* Global pointer for last received frame infos */ -extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos; - - -static void ethernetif_input( void * pvParameters ); -#endif - -static void arp_timer(void *arg); - - -/** -* In this function, the hardware should be initialized. -* Called from ethernetif_init(). -* -* @param netif the already initialized lwip network interface structure -* for this ethernetif -*/ -static void low_level_init(struct netif *netif) -{ - uint32_t i; - - /* set netif MAC hardware address length */ - netif->hwaddr_len = ETHARP_HWADDR_LEN; - -#if !CONFIG_WLAN - /* set netif MAC hardware address */ - netif->hwaddr[0] = MAC_ADDR0; - netif->hwaddr[1] = MAC_ADDR1; - netif->hwaddr[2] = MAC_ADDR2; - netif->hwaddr[3] = MAC_ADDR3; - netif->hwaddr[4] = MAC_ADDR4; - netif->hwaddr[5] = MAC_ADDR5; -#endif - - /* set netif maximum transfer unit */ - netif->mtu = 1500; - - /* Accept broadcast address and ARP traffic */ - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; - -#if !CONFIG_WLAN - s_pxNetIf =netif; - - /* create binary semaphore used for informing ethernetif of frame reception */ - if (s_xSemaphore == NULL) - { - vSemaphoreCreateBinary(s_xSemaphore); - xSemaphoreTake( s_xSemaphore, 0); - } - - /* initialize MAC address in ethernet MAC */ - ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr); - - /* Initialize Tx Descriptors list: Chain Mode */ - ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB); - /* Initialize Rx Descriptors list: Chain Mode */ - ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB); - - /* Enable Ethernet Rx interrrupt */ - { - for(i=0; iBuffer1Addr); - bufferoffset = 0; - - for(q = p; q != NULL; q = q->next) - { - if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET) - { - goto error; - } - - /* Get bytes in current lwIP buffer */ - byteslefttocopy = q->len; - payloadoffset = 0; - - /* Check if the length of data to copy is bigger than Tx buffer size*/ - while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE ) - { - /* Copy data to Tx buffer*/ - memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) ); - - /* Point to next descriptor */ - DmaTxDesc = (ETH_DMADESCTypeDef *)(DmaTxDesc->Buffer2NextDescAddr); - - /* Check if the buffer is available */ - if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET) - { - goto error; - } - - buffer = (u8 *)(DmaTxDesc->Buffer1Addr); - - byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset); - payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset); - framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset); - bufferoffset = 0; - } - - /* Copy the remaining bytes */ - memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), byteslefttocopy ); - bufferoffset = bufferoffset + byteslefttocopy; - framelength = framelength + byteslefttocopy; - } - - /* Prepare transmit descriptors to give to DMA*/ - ETH_Prepare_Transmit_Descriptors(framelength); - - /* Give semaphore and exit */ - error: - - xSemaphoreGive(xTxSemaphore); - } -#endif // #if !CONFIG_WLAN - - return ERR_OK; -} - - -#if !CONFIG_WLAN -/** -* Should allocate a pbuf and transfer the bytes of the incoming -* packet from the interface into the pbuf. -* -* @param netif the lwip network interface structure for this ethernetif -* @return a pbuf filled with the received packet (including MAC header) -* NULL on memory error -*/ -static struct pbuf * low_level_input(struct netif *netif) -{ - struct pbuf *p= NULL, *q; - u32_t len; - FrameTypeDef frame; - u8 *buffer; - __IO ETH_DMADESCTypeDef *DMARxDesc; - uint32_t bufferoffset = 0; - uint32_t payloadoffset = 0; - uint32_t byteslefttocopy = 0; - uint32_t i=0; - - /* get received frame */ - frame = ETH_Get_Received_Frame_interrupt(); - - /* Obtain the size of the packet and put it into the "len" variable. */ - len = frame.length; - buffer = (u8 *)frame.buffer; - - if (len > 0) - { - /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */ - p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); - } - - if (p != NULL) - { - DMARxDesc = frame.descriptor; - bufferoffset = 0; - for(q = p; q != NULL; q = q->next) - { - byteslefttocopy = q->len; - payloadoffset = 0; - - /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/ - while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE ) - { - /* Copy data to pbuf*/ - memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset)); - - /* Point to next descriptor */ - DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr); - buffer = (unsigned char *)(DMARxDesc->Buffer1Addr); - - byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset); - payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset); - bufferoffset = 0; - } - - /* Copy remaining data in pbuf */ - memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), byteslefttocopy); - bufferoffset = bufferoffset + byteslefttocopy; - } - - /* Release descriptors to DMA */ - DMARxDesc =frame.descriptor; - - /* Set Own bit in Rx descriptors: gives the buffers back to DMA */ - for (i=0; iSeg_Count; i++) - { - DMARxDesc->Status = ETH_DMARxDesc_OWN; - DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr); - } - - /* Clear Segment_Count */ - DMA_RX_FRAME_infos->Seg_Count =0; - /* added for test*/ - } - - /* When Rx Buffer unavailable flag is set: clear it and resume reception */ - if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) - { - /* Clear RBUS ETHERNET DMA flag */ - ETH->DMASR = ETH_DMASR_RBUS; - /* Resume DMA reception */ - ETH->DMARPDR = 0; - } - return p; -} - - -/** -* This function is the ethernetif_input task, it is processed when a packet -* is ready to be read from the interface. It uses the function low_level_input() -* that should handle the actual reception of bytes from the network -* interface. Then the type of the received packet is determined and -* the appropriate input function is called. -* -* @param netif the lwip network interface structure for this ethernetif -*/ -void ethernetif_input( void * pvParameters ) -{ - struct pbuf *p; - - for( ;; ) - { - if (xSemaphoreTake( s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT)==pdTRUE) - { -TRY_GET_NEXT_FRAME: - p = low_level_input( s_pxNetIf ); - if (p != NULL) - { - if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf)) - { - pbuf_free(p); - } - else - { - goto TRY_GET_NEXT_FRAME; - } - } - - } - } -} -#endif -/** -* Should be called at the beginning of the program to set up the -* network interface. It calls the function low_level_init() to do the -* actual setup of the hardware. -* -* This function should be passed as a parameter to netif_add(). -* -* @param netif the lwip network interface structure for this ethernetif -* @return ERR_OK if the loopif is initialized -* ERR_MEM if private data couldn't be allocated -* any other err_t on error -*/ -err_t ethernetif_init(struct netif *netif) -{ - LWIP_ASSERT("netif != NULL", (netif != NULL)); - -#if !CONFIG_WLAN - /* ethernet */ - netif->name[0] = IFNAME0; - netif->name[1] = IFNAME1; -#else - /* wlan interface*/ -#if LWIP_NETIF_HOSTNAME - /* Initialize interface hostname */ - netif->hostname = "lwip"; -#endif /* LWIP_NETIF_HOSTNAME */ -#endif // WLAN - - netif->output = etharp_output; - netif->linkoutput = low_level_output; - - /* initialize the hardware */ - low_level_init(netif); - - etharp_init(); - sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); - - return ERR_OK; -} - - -static void arp_timer(void *arg) -{ - etharp_tmr(); - sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); -} - -/* - * For FreeRTOS tickless - */ -int lwip_tickless_used = 0; - -/* -int arp_timeout_exist(void) -{ - struct sys_timeouts *timeouts; - struct sys_timeo *t; - - timeouts = sys_arch_timeouts(); - - for(t = timeouts->next; t != NULL;t = t->next) - if(t->h == arp_timer) - return 1; - - return 0; -} -*/ -//Called by rltk_wlan_PRE_SLEEP_PROCESSING() -void lwip_PRE_SLEEP_PROCESSING(void) -{ - if(arp_timeout_exist()) { - tcpip_untimeout(arp_timer, NULL); - } - lwip_tickless_used = 1; -} - -//Called in ips_leave() path, support tickless when wifi power wakeup due to ioctl or deinit -void lwip_POST_SLEEP_PROCESSING(void) -{ - if(lwip_tickless_used) { - tcpip_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); - } -} - diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/ethernetif.c b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/ethernetif.c deleted file mode 100644 index 0f67357..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/ethernetif.c +++ /dev/null @@ -1,653 +0,0 @@ -/** -* @file -* Ethernet Interface Skeleton -* -*/ - -/* -* Copyright (c) 2001-2004 Swedish Institute of Computer Science. -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without modification, -* are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, -* this list of conditions and the following disclaimer. -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* 3. The name of the author may not be used to endorse or promote products -* derived from this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED -* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT -* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT -* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING -* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY -* OF SUCH DAMAGE. -* -* This file is part of the lwIP TCP/IP stack. -* -* Author: Adam Dunkels -* -*/ - -/* -* This file is a skeleton for developing Ethernet network interface -* drivers for lwIP. Add code to the low_level functions and do a -* search-and-replace for the word "ethernetif" to replace it with -* something that better describes your network interface. -*/ - -#include "lwip/opt.h" -#include "lwip/def.h" -#include "lwip/mem.h" -#include "lwip/pbuf.h" -#include "lwip/sys.h" -#include "lwip/tcpip.h" -#include "lwip/icmp.h" -#include "lwip/lwip_timers.h" -#include "netif/etharp.h" -#include "err.h" -#include "ethernetif.h" -#include "queue.h" - -#include "main.h" // for the definition of CONFIG_WLAN - - -#if !CONFIG_WLAN -#include "stm32f2x7_eth.h" -#else -#include -#endif -#include - -#include - -#ifdef CONFIG_DONT_CARE_TP -#define netifMTU (576) -#else -#define netifMTU (1500) -#endif -#define netifINTERFACE_TASK_STACK_SIZE ( 350 ) -#define netifINTERFACE_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) -#define netifGUARD_BLOCK_TIME ( 250 ) -/* The time to block waiting for input. */ -#define emacBLOCK_TIME_WAITING_FOR_INPUT ( ( portTickType ) 100 ) -#define FAKE_PING_REPLY 0 - -#if !CONFIG_WLAN -/* Define those to better describe your network interface. */ -#define IFNAME0 's' -#define IFNAME1 't' - -static struct netif *s_pxNetIf = NULL; -xSemaphoreHandle s_xSemaphore = NULL; - - -/* Ethernet Rx & Tx DMA Descriptors */ -extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB]; - -/* Ethernet Receive buffers */ -extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; - -/* Ethernet Transmit buffers */ -extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; - -/* Global pointers to track current transmit and receive descriptors */ -extern ETH_DMADESCTypeDef *DMATxDescToSet; -extern ETH_DMADESCTypeDef *DMARxDescToGet; - -/* Global pointer for last received frame infos */ -extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos; - -static void ethernetif_input( void * pvParameters ); -#endif - -static void arp_timer(void *arg); - - -/** -* In this function, the hardware should be initialized. -* Called from ethernetif_init(). -* -* @param netif the already initialized lwip network interface structure -* for this ethernetif -*/ -static void low_level_init(struct netif *netif) -{ - uint32_t i; - - /* set netif MAC hardware address length */ - netif->hwaddr_len = ETHARP_HWADDR_LEN; - -#if !CONFIG_WLAN - /* set netif MAC hardware address */ - netif->hwaddr[0] = MAC_ADDR0; - netif->hwaddr[1] = MAC_ADDR1; - netif->hwaddr[2] = MAC_ADDR2; - netif->hwaddr[3] = MAC_ADDR3; - netif->hwaddr[4] = MAC_ADDR4; - netif->hwaddr[5] = MAC_ADDR5; -#endif - - /* set netif maximum transfer unit */ - netif->mtu = netifMTU; - - /* Accept broadcast address and ARP traffic */ - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; - -#if !CONFIG_WLAN - s_pxNetIf =netif; - - /* create binary semaphore used for informing ethernetif of frame reception */ - if (s_xSemaphore == NULL) - { - s_xSemaphore= xSemaphoreCreateCounting(20,0); - } - - /* initialize MAC address in ethernet MAC */ - ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr); - - /* Initialize Tx Descriptors list: Chain Mode */ - ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB); - /* Initialize Rx Descriptors list: Chain Mode */ - ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB); - - /* Enable Ethernet Rx interrrupt */ - { - for(i=0; itot_len, PBUF_POOL); - if (p == NULL) { - printf("\n\rCannot allocate pbuf to receive packet"); - return; - } - for (tq = q, tp = p, q_len = tq->len; tp != NULL ; tp = tp->next) - { - p_len = tp->len; - while(p_len) - { - if(q_len > p_len) { - memcpy((char*)tq->payload + (tq->len - q_len), (char*)tp->payload + (tp->len - p_len), p_len); - q_len -= p_len; - p_len = 0; - }else{ - memcpy((char*)tq->payload + (tq->len - q_len), (char*)tp->payload + (tp->len - p_len), q_len); - p_len -= q_len; - tq = tq->next; - q_len = tq->len; - } - } - } - p_eth = (struct eth_hdr *)p->payload; - q_eth = (struct eth_hdr *)q->payload; - p_arp = (struct etharp_hdr *)((char*)(p->payload) + sizeof(struct eth_hdr)); - q_arp = (struct etharp_hdr *)((char*)(q->payload) + sizeof(struct eth_hdr)); - - q_eth->dest = p_eth->src; - q_eth->src = fake_src_mac; - q_arp->opcode = htons(ARP_REPLY); - q_arp->shwaddr = fake_src_mac; - q_arp->sipaddr = p_arp->dipaddr; - q_arp->dhwaddr = p_eth->src; - q_arp->dipaddr = p_arp->sipaddr; - - if(0){ - int i; - char *buf = q->payload; - printf("\n\r"); - for(i=0;itot_len;i++) - printf("0x%02x, ", buf[i]); - printf("\n\r"); - } - if (ERR_OK != netif->input(q, netif)){ - printf("\n\rfake_arp_reply input error"); - pbuf_free(q); - }else - printf("\n\rfake arp reply \n\r"); -} - -void fake_echo_reply(struct netif *netif, struct pbuf *p) -{ - struct pbuf *q, *tq, *tp; - int q_len, p_len; - struct eth_hdr *p_eth, *q_eth; - struct ip_hdr *p_ip, *q_ip; - struct icmp_echo_hdr *p_echo, *q_echo; - - // Allocate buffer to store received packet - q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_POOL); - if (p == NULL) { - printf("\n\rCannot allocate pbuf to receive packet"); - return; - } - for (tq = q, tp = p, q_len = tq->len; tp != NULL ; tp = tp->next) - { - p_len = tp->len; - while(p_len) - { - if(q_len > p_len) { - memcpy((char*)tq->payload + (tq->len - q_len), (char*)tp->payload + (tp->len - p_len), p_len); - q_len -= p_len; - p_len = 0; - }else{ - memcpy((char*)tq->payload + (tq->len - q_len), (char*)tp->payload + (tp->len - p_len), q_len); - p_len -= q_len; - tq = tq->next; - q_len = tq->len; - } - } - } - p_eth = (struct eth_hdr *)p->payload; - q_eth = (struct eth_hdr *)q->payload; - p_ip = (struct ip_hdr *)((char*)(p->payload) + sizeof(struct eth_hdr)); - q_ip = (struct ip_hdr *)((char*)(q->payload) + sizeof(struct eth_hdr)); - p_echo = (struct icmp_echo_hdr *)((char*)(p->payload) + sizeof(struct eth_hdr) + sizeof(struct ip_hdr)); - q_echo = (struct icmp_echo_hdr *)((char*)(q->payload) + sizeof(struct eth_hdr) + sizeof(struct ip_hdr)); - - q_eth->dest = p_eth->src; - q_eth->src = p_eth->dest; - q_ip->src.addr = p_ip->dest.addr; - q_ip->dest.addr = p_ip->src.addr; - q_ip->_chksum = 0; - q_ip->_chksum = inet_chksum(q_ip, sizeof(struct ip_hdr)); - q_echo->type = ICMP_ER; - q_echo->code = 0; - q_echo->chksum = 0; - q_echo->chksum = inet_chksum(q_echo, q->tot_len - sizeof(struct eth_hdr) - sizeof(struct ip_hdr)); - - if(0){ - int i; - char *buf = q->payload; - printf("\n\r"); - for(i=0;itot_len;i++) - printf("0x%02x, ", buf[i]); - printf("\n\r"); - } - if (ERR_OK != netif->input(q, netif)){ - printf("\n\rfake_echo_reply input error"); - pbuf_free(q); - }else - printf("\n\rfake echo reply \n\r"); -} -#endif // #if FAKE_PING_REPLY - -/** -* This function should do the actual transmission of the packet. The packet is -* contained in the pbuf that is passed to the function. This pbuf -* might be chained. -* -* @param netif the lwip network interface structure for this ethernetif -* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) -* @return ERR_OK if the packet could be sent -* an err_t value if the packet couldn't be sent -* -* @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to -* strange results. You might consider waiting for space in the DMA queue -* to become availale since the stack doesn't retry to send a packet -* dropped because of memory failure (except for the TCP timers). -*/ - -static err_t low_level_output(struct netif *netif, struct pbuf *p) -{ -#if !CONFIG_WLAN - static xSemaphoreHandle xTxSemaphore = NULL; - struct pbuf *q; - uint32_t l = 0; - u8 *buffer ; - - if (xTxSemaphore == NULL) - { - vSemaphoreCreateBinary (xTxSemaphore); - } - - if (xSemaphoreTake(xTxSemaphore, netifGUARD_BLOCK_TIME)) - { - buffer = (u8 *)(DMATxDescToSet->Buffer1Addr); - for(q = p; q != NULL; q = q->next) - { - memcpy((u8_t*)&buffer[l], q->payload, q->len); - l = l + q->len; - } - ETH_Prepare_Transmit_Descriptors(l); - xSemaphoreGive(xTxSemaphore); - } -#else - /* Refer to eCos lwip eth_drv_send() */ - struct eth_drv_sg sg_list[MAX_ETH_DRV_SG]; - int sg_len = 0; - struct pbuf *q; - - if(!rltk_wlan_running(netif_get_idx(netif))) - return ERR_IF; - - for (q = p; q != NULL && sg_len < MAX_ETH_DRV_SG; q = q->next) { - sg_list[sg_len].buf = (unsigned int) q->payload; - sg_list[sg_len++].len = q->len; - } - -#if FAKE_PING_REPLY - { - char *header = p->payload; - if(header[12] == 0x08 && header[13] == 0x06) - { // arp packet - if(header[21] == 0x01) - { // arp request packet - printf("\n\rfake_ping: arp request packet."); - if(0) - { - int i; - printf("\n\r"); - for (q = p; q != NULL; q = q->next) - { - char *buf = q->payload; - for(i=0;ilen;i++) - printf("0x%02x, ", buf[i]); - } - printf("\n\r"); - } - fake_arp_reply(netif, p); - return ERR_OK; - } - }else if(header[12] == 0x08 && header[13] == 0x00) - { // ip packet - if(header[15] == 0x00 && header[23] == 0x01) - { // icmp packet - printf("\n\rfake_ping: icmp packet."); - if(0){ - int i; - printf("\n\r"); - for (q = p; q != NULL; q = q->next) - { - char *buf = q->payload; - for(i=0;ilen;i++) - printf("0x%02x, ", buf[i]); - } - printf("\n\r"); - } - fake_echo_reply(netif, p); - return ERR_OK; - } - } - } -#endif // #if FAKE_PING_REPLY - if (sg_len) - rltk_wlan_send(netif_get_idx(netif), sg_list, sg_len, p->tot_len); -#endif // #if !CONFIG_WLAN - - return ERR_OK; -} - - -#if !CONFIG_WLAN -/** -* Should allocate a pbuf and transfer the bytes of the incoming -* packet from the interface into the pbuf. -* -* @param netif the lwip network interface structure for this ethernetif -* @return a pbuf filled with the received packet (including MAC header) -* NULL on memory error -*/ -static struct pbuf * low_level_input(struct netif *netif) -{ - struct pbuf *p, *q; - u16_t len; - uint32_t l=0,i =0; - FrameTypeDef frame; - u8 *buffer; - __IO ETH_DMADESCTypeDef *DMARxNextDesc; - - p = NULL; - - /* Get received frame */ - frame = ETH_Get_Received_Frame_interrupt(); - - /* check that frame has no error */ - if ((frame.descriptor->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) - { - - /* Obtain the size of the packet and put it into the "len" variable. */ - len = frame.length; - buffer = (u8 *)frame.buffer; - - /* We allocate a pbuf chain of pbufs from the pool. */ - p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); - - /* Copy received frame from ethernet driver buffer to stack buffer */ - if (p != NULL) - { - for (q = p; q != NULL; q = q->next) - { - memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len); - l = l + q->len; - } - } - } - - /* Release descriptors to DMA */ - /* Check if received frame with multiple DMA buffer segments */ - if (DMA_RX_FRAME_infos->Seg_Count > 1) - { - DMARxNextDesc = DMA_RX_FRAME_infos->FS_Rx_Desc; - } - else - { - DMARxNextDesc = frame.descriptor; - } - - /* Set Own bit in Rx descriptors: gives the buffers back to DMA */ - for (i=0; iSeg_Count; i++) - { - DMARxNextDesc->Status = ETH_DMARxDesc_OWN; - DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr); - } - - /* Clear Segment_Count */ - DMA_RX_FRAME_infos->Seg_Count =0; - - - /* When Rx Buffer unavailable flag is set: clear it and resume reception */ - if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) - { - /* Clear RBUS ETHERNET DMA flag */ - ETH->DMASR = ETH_DMASR_RBUS; - - /* Resume DMA reception */ - ETH->DMARPDR = 0; - } - return p; -} - - -/** -* This function is the ethernetif_input task, it is processed when a packet -* is ready to be read from the interface. It uses the function low_level_input() -* that should handle the actual reception of bytes from the network -* interface. Then the type of the received packet is determined and -* the appropriate input function is called. -* -* @param netif the lwip network interface structure for this ethernetif -*/ -void ethernetif_input( void * pvParameters ) -{ - struct pbuf *p; - - for( ;; ) - { - if (xSemaphoreTake( s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT)==pdTRUE) - { - p = low_level_input( s_pxNetIf ); - if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf)) - { - pbuf_free(p); - p=NULL; - } - } - } -} -#endif - -/* Refer to eCos eth_drv_recv to do similarly in ethernetif_input */ -void ethernetif_recv(struct netif *netif, int total_len) -{ -#if CONFIG_WLAN - struct eth_drv_sg sg_list[MAX_ETH_DRV_SG]; - struct pbuf *p, *q; - int sg_len = 0; - - if(!rltk_wlan_running(netif_get_idx(netif))) - return; - - if ((total_len > MAX_ETH_MSG) || (total_len < 0)) - total_len = MAX_ETH_MSG; - - // Allocate buffer to store received packet - p = pbuf_alloc(PBUF_RAW, total_len, PBUF_POOL); - if (p == NULL) { - printf("\n\rCannot allocate pbuf to receive packet"); - return; - } - - // Create scatter list - for (q = p; q != NULL && sg_len < MAX_ETH_DRV_SG; q = q->next) { - sg_list[sg_len].buf = (unsigned int) q->payload; - sg_list[sg_len++].len = q->len; - } - - // Copy received packet to scatter list from wrapper rx skb - //printf("\n\rwlan:%c: Recv sg_len: %d, tot_len:%d", netif->name[1],sg_len, total_len); - rltk_wlan_recv(netif_get_idx(netif), sg_list, sg_len); - - // Pass received packet to the interface - if (ERR_OK != netif->input(p, netif)) - pbuf_free(p); -#endif -} - -/** -* Should be called at the beginning of the program to set up the -* network interface. It calls the function low_level_init() to do the -* actual setup of the hardware. -* -* This function should be passed as a parameter to netif_add(). -* -* @param netif the lwip network interface structure for this ethernetif -* @return ERR_OK if the loopif is initialized -* ERR_MEM if private data couldn't be allocated -* any other err_t on error -*/ -err_t ethernetif_init(struct netif *netif) -{ - LWIP_ASSERT("netif != NULL", (netif != NULL)); - -#if !CONFIG_WLAN - /* ethernet */ - netif->name[0] = IFNAME0; - netif->name[1] = IFNAME1; -#else - /* wlan interface*/ -#if LWIP_NETIF_HOSTNAME - /* Initialize interface hostname */ - if(netif->name[1] == '0') - netif->hostname = "lwip0"; - else if(netif->name[1] == '1') - netif->hostname = "lwip1"; -#endif /* LWIP_NETIF_HOSTNAME */ - -#endif // WLAN - - netif->output = etharp_output; - netif->linkoutput = low_level_output; - - /* initialize the hardware */ - low_level_init(netif); - - etharp_init(); - sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); - - return ERR_OK; -} - - -static void arp_timer(void *arg) -{ - etharp_tmr(); - sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); -} - -/* - * For FreeRTOS tickless - */ -int lwip_tickless_used = 0; - - -int arp_timeout_exist(void) -{ - struct sys_timeouts *timeouts; - struct sys_timeo *t; - - timeouts = sys_arch_timeouts(); - - for(t = timeouts->next; t != NULL;t = t->next) - if(t->h == arp_timer) - return 1; - - return 0; -} - -//Called by rltk_wlan_PRE_SLEEP_PROCESSING() -void lwip_PRE_SLEEP_PROCESSING(void) -{ - if(arp_timeout_exist()) { - tcpip_untimeout(arp_timer, NULL); - } - lwip_tickless_used = 1; -} - -//Called in ips_leave() path, support tickless when wifi power wakeup due to ioctl or deinit -void lwip_POST_SLEEP_PROCESSING(void) -{ - if(lwip_tickless_used) { - tcpip_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); - } -} diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/ethernetif.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/ethernetif.h deleted file mode 100644 index 7a436d2..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/ethernetif.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __ETHERNETIF_H__ -#define __ETHERNETIF_H__ - - -#include "lwip/err.h" -#include "lwip/netif.h" -//----- ------------------------------------------------------------------ -// Ethernet Buffer -//----- ------------------------------------------------------------------ -struct eth_drv_sg { - unsigned int buf; - unsigned int len; -}; - -#define MAX_ETH_DRV_SG 32 -#define MAX_ETH_MSG 1540 - -void ethernetif_recv(struct netif *netif, int total_len); -err_t ethernetif_init(struct netif *netif); -void lwip_PRE_SLEEP_PROCESSING(void); -void lwip_POST_SLEEP_PROCESSING(void); - -#endif diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/sys_arch.c b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/sys_arch.c deleted file mode 100644 index 3d111c6..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/sys_arch.c +++ /dev/null @@ -1,515 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ - -/* lwIP includes. */ -#include "lwip/debug.h" -#include "lwip/def.h" -#include "lwip/sys.h" -#include "lwip/mem.h" -#include "lwip/stats.h" -#include "FreeRTOS.h" -#include "task.h" -#include "queue.h" -#include "lwip/lwip_timers.h" - -xTaskHandle xTaskGetCurrentTaskHandle( void ) PRIVILEGED_FUNCTION; - -struct timeoutlist -{ - struct sys_timeouts timeouts; - xTaskHandle pid; -}; - -/* This is the number of threads that can be started with sys_thread_new() */ -#define SYS_THREAD_MAX 6 - -static struct timeoutlist s_timeoutlist[SYS_THREAD_MAX]; -static u16_t s_nextthread = 0; - - -/*-----------------------------------------------------------------------------------*/ -// Creates an empty mailbox. -err_t sys_mbox_new(sys_mbox_t *mbox, int size) -{ - (void ) size; - - *mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) ); - -#if SYS_STATS - ++lwip_stats.sys.mbox.used; - if (lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used) { - lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used; - } -#endif /* SYS_STATS */ - if (*mbox == NULL) - return ERR_MEM; - - return ERR_OK; -} - -/*-----------------------------------------------------------------------------------*/ -/* - Deallocates a mailbox. If there are messages still present in the - mailbox when the mailbox is deallocated, it is an indication of a - programming error in lwIP and the developer should be notified. -*/ -void sys_mbox_free(sys_mbox_t *mbox) -{ - if( uxQueueMessagesWaiting( *mbox ) ) - { - /* Line for breakpoint. Should never break here! */ - portNOP(); -#if SYS_STATS - lwip_stats.sys.mbox.err++; -#endif /* SYS_STATS */ - - // TODO notify the user of failure. - } - - vQueueDelete( *mbox ); - -#if SYS_STATS - --lwip_stats.sys.mbox.used; -#endif /* SYS_STATS */ -} - -/*-----------------------------------------------------------------------------------*/ -// Posts the "msg" to the mailbox. -void sys_mbox_post(sys_mbox_t *mbox, void *data) -{ - while ( xQueueSendToBack(*mbox, &data, portMAX_DELAY ) != pdTRUE ){} -} - - -/*-----------------------------------------------------------------------------------*/ -// Try to post the "msg" to the mailbox. -err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) -{ -err_t result; - - if ( xQueueSend( *mbox, &msg, 0 ) == pdPASS ) - { - result = ERR_OK; - } - else { - // could not post, queue must be full - result = ERR_MEM; - -#if SYS_STATS - lwip_stats.sys.mbox.err++; -#endif /* SYS_STATS */ - - } - - return result; -} - -/*-----------------------------------------------------------------------------------*/ -/* - Blocks the thread until a message arrives in the mailbox, but does - not block the thread longer than "timeout" milliseconds (similar to - the sys_arch_sem_wait() function). The "msg" argument is a result - parameter that is set by the function (i.e., by doing "*msg = - ptr"). The "msg" parameter maybe NULL to indicate that the message - should be dropped. - - The return values are the same as for the sys_arch_sem_wait() function: - Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a - timeout. - - Note that a function with a similar name, sys_mbox_fetch(), is - implemented by lwIP. -*/ -u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout) -{ -void *dummyptr; -portTickType StartTime, EndTime, Elapsed; - - StartTime = xTaskGetTickCount(); - - if ( msg == NULL ) - { - msg = &dummyptr; - } - - if ( timeout != 0 ) - { - if ( pdTRUE == xQueueReceive( *mbox, &(*msg), timeout / portTICK_RATE_MS ) ) - { - EndTime = xTaskGetTickCount(); - Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; - - return ( Elapsed ); - } - else // timed out blocking for message - { - *msg = NULL; - - return SYS_ARCH_TIMEOUT; - } - } - else // block forever for a message. - { - while( pdTRUE != xQueueReceive( *mbox, &(*msg), portMAX_DELAY ) ){} // time is arbitrary - EndTime = xTaskGetTickCount(); - Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; - - return ( Elapsed ); // return time blocked TODO test - } -} - -/*-----------------------------------------------------------------------------------*/ -/* - Similar to sys_arch_mbox_fetch, but if message is not ready immediately, we'll - return with SYS_MBOX_EMPTY. On success, 0 is returned. -*/ -u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) -{ -void *dummyptr; - - if ( msg == NULL ) - { - msg = &dummyptr; - } - - if ( pdTRUE == xQueueReceive( *mbox, &(*msg), 0 ) ) - { - return ERR_OK; - } - else - { - return SYS_MBOX_EMPTY; - } -} -/*----------------------------------------------------------------------------------*/ -int sys_mbox_valid(sys_mbox_t *mbox) -{ - if (*mbox == SYS_MBOX_NULL) - return 0; - else - return 1; -} -/*-----------------------------------------------------------------------------------*/ -void sys_mbox_set_invalid(sys_mbox_t *mbox) -{ - *mbox = SYS_MBOX_NULL; -} - -/*-----------------------------------------------------------------------------------*/ -// Creates a new semaphore. The "count" argument specifies -// the initial state of the semaphore. -err_t sys_sem_new(sys_sem_t *sem, u8_t count) -{ - vSemaphoreCreateBinary(*sem ); - if(*sem == NULL) - { -#if SYS_STATS - ++lwip_stats.sys.sem.err; -#endif /* SYS_STATS */ - return ERR_MEM; - } - - if(count == 0) // Means it can't be taken - { - xSemaphoreTake(*sem,1); - } - -#if SYS_STATS - ++lwip_stats.sys.sem.used; - if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) { - lwip_stats.sys.sem.max = lwip_stats.sys.sem.used; - } -#endif /* SYS_STATS */ - - return ERR_OK; -} - -/*-----------------------------------------------------------------------------------*/ -/* - Blocks the thread while waiting for the semaphore to be - signaled. If the "timeout" argument is non-zero, the thread should - only be blocked for the specified time (measured in - milliseconds). - - If the timeout argument is non-zero, the return value is the number of - milliseconds spent waiting for the semaphore to be signaled. If the - semaphore wasn't signaled within the specified time, the return value is - SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore - (i.e., it was already signaled), the function may return zero. - - Notice that lwIP implements a function with a similar name, - sys_sem_wait(), that uses the sys_arch_sem_wait() function. -*/ -u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) -{ -portTickType StartTime, EndTime, Elapsed; - - StartTime = xTaskGetTickCount(); - - if( timeout != 0) - { - if( xSemaphoreTake( *sem, timeout / portTICK_RATE_MS ) == pdTRUE ) - { - EndTime = xTaskGetTickCount(); - Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; - - return (Elapsed); // return time blocked TODO test - } - else - { - return SYS_ARCH_TIMEOUT; - } - } - else // must block without a timeout - { - while( xSemaphoreTake(*sem, portMAX_DELAY) != pdTRUE){} - EndTime = xTaskGetTickCount(); - Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; - - return ( Elapsed ); // return time blocked - - } -} - -/*-----------------------------------------------------------------------------------*/ -// Signals a semaphore -void sys_sem_signal(sys_sem_t *sem) -{ - xSemaphoreGive(*sem); -} - -/*-----------------------------------------------------------------------------------*/ -// Deallocates a semaphore -void sys_sem_free(sys_sem_t *sem) -{ -#if SYS_STATS - --lwip_stats.sys.sem.used; -#endif /* SYS_STATS */ - - vQueueDelete(*sem); -} -/*-----------------------------------------------------------------------------------*/ -int sys_sem_valid(sys_sem_t *sem) -{ - if (*sem == SYS_SEM_NULL) - return 0; - else - return 1; -} - -/*-----------------------------------------------------------------------------------*/ -void sys_sem_set_invalid(sys_sem_t *sem) -{ - *sem = SYS_SEM_NULL; -} - -/*-----------------------------------------------------------------------------------*/ -// Initialize sys arch -void sys_init(void) -{ - int i; - - // Initialize the the per-thread sys_timeouts structures - // make sure there are no valid pids in the list - for(i = 0; i < SYS_THREAD_MAX; i++) - { - s_timeoutlist[i].pid = 0; - s_timeoutlist[i].timeouts.next = NULL; - } - - // keep track of how many threads have been created - s_nextthread = 0; -} - -/* - Returns a pointer to the per-thread sys_timeouts structure. In lwIP, - each thread has a list of timeouts which is represented as a linked - list of sys_timeout structures. The sys_timeouts structure holds a - pointer to a linked list of timeouts. This function is called by - the lwIP timeout scheduler and must not return a NULL value. - - In a single threaded sys_arch implementation, this function will - simply return a pointer to a global sys_timeouts variable stored in - the sys_arch module. -*/ -struct sys_timeouts* sys_arch_timeouts(void) -{ -int i; -xTaskHandle pid; -struct timeoutlist *tl; - - pid = xTaskGetCurrentTaskHandle(); - - for(i = 0; i < s_nextthread; i++) - { - tl = &(s_timeoutlist[i]); - if(tl->pid == pid) - { - return &(tl->timeouts); - } - } - // Error - return NULL; -} -/*-----------------------------------------------------------------------------------*/ - /* Mutexes*/ -/*-----------------------------------------------------------------------------------*/ -/*-----------------------------------------------------------------------------------*/ -#if LWIP_COMPAT_MUTEX == 0 -/* Create a new mutex*/ -err_t sys_mutex_new(sys_mutex_t *mutex) { - - *mutex = xSemaphoreCreateMutex(); - if(*mutex == NULL) - { -#if SYS_STATS - ++lwip_stats.sys.mutex.err; -#endif /* SYS_STATS */ - return ERR_MEM; - } - -#if SYS_STATS - ++lwip_stats.sys.mutex.used; - if (lwip_stats.sys.mutex.max < lwip_stats.sys.mutex.used) { - lwip_stats.sys.mutex.max = lwip_stats.sys.mutex.used; - } -#endif /* SYS_STATS */ - return ERR_OK; -} -/*-----------------------------------------------------------------------------------*/ -/* Deallocate a mutex*/ -void sys_mutex_free(sys_mutex_t *mutex) -{ -#if SYS_STATS - --lwip_stats.sys.mutex.used; -#endif /* SYS_STATS */ - - vQueueDelete(*mutex); -} -/*-----------------------------------------------------------------------------------*/ -/* Lock a mutex*/ -void sys_mutex_lock(sys_mutex_t *mutex) -{ - sys_arch_sem_wait(*mutex, 0); -} - -/*-----------------------------------------------------------------------------------*/ -/* Unlock a mutex*/ -void sys_mutex_unlock(sys_mutex_t *mutex) -{ - xSemaphoreGive(*mutex); -} -#endif /*LWIP_COMPAT_MUTEX*/ -/*-----------------------------------------------------------------------------------*/ -// TODO -/*-----------------------------------------------------------------------------------*/ -/* - Starts a new thread with priority "prio" that will begin its execution in the - function "thread()". The "arg" argument will be passed as an argument to the - thread() function. The id of the new thread is returned. Both the id and - the priority are system dependent. -*/ -sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread , void *arg, int stacksize, int prio) -{ -xTaskHandle CreatedTask; -int result; - - if ( s_nextthread < SYS_THREAD_MAX ) - { - vPortEnterCritical(); - result = xTaskCreate( thread, ( signed portCHAR * ) name, stacksize, arg, prio, &CreatedTask ); - - // For each task created, store the task handle (pid) in the timers array. - // This scheme doesn't allow for threads to be deleted - s_timeoutlist[s_nextthread++].pid = CreatedTask; - vPortExitCritical(); - - if(result == pdPASS) - { - return CreatedTask; - } - else - { - return NULL; - } - } - else - { - return NULL; - } -} - -/* - This optional function does a "fast" critical region protection and returns - the previous protection level. This function is only called during very short - critical regions. An embedded system which supports ISR-based drivers might - want to implement this function by disabling interrupts. Task-based systems - might want to implement this by using a mutex or disabling tasking. This - function should support recursive calls from the same task or interrupt. In - other words, sys_arch_protect() could be called while already protected. In - that case the return value indicates that it is already protected. - - sys_arch_protect() is only required if your port is supporting an operating - system. -*/ -sys_prot_t sys_arch_protect(void) -{ - vPortEnterCritical(); - return 1; -} - -/* - This optional function does a "fast" set of critical region protection to the - value specified by pval. See the documentation for sys_arch_protect() for - more information. This function is only required if your port is supporting - an operating system. -*/ -void sys_arch_unprotect(sys_prot_t pval) -{ - ( void ) pval; - vPortExitCritical(); -} - -/* - * Prints an assertion messages and aborts execution. - */ -void sys_assert( const char *msg ) -{ - ( void ) msg; - /*FSL:only needed for debugging - printf(msg); - printf("\n\r"); - */ - vPortEnterCritical( ); - for(;;) - ; -} diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/sys_arch.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/sys_arch.h deleted file mode 100644 index 83d0c08..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/freertos/sys_arch.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __SYS_RTXC_H__ -#define __SYS_RTXC_H__ - -#include "FreeRTOS.h" -#include "task.h" -#include "queue.h" -#include "semphr.h" - -#define SYS_MBOX_NULL (xQueueHandle)0 -#define SYS_SEM_NULL (xSemaphoreHandle)0 -#define SYS_DEFAULT_THREAD_STACK_DEPTH configMINIMAL_STACK_SIZE - -typedef xSemaphoreHandle sys_sem_t; -typedef xQueueHandle sys_mbox_t; -typedef xTaskHandle sys_thread_t; - -typedef struct _sys_arch_state_t -{ - // Task creation data. - char cTaskName[configMAX_TASK_NAME_LEN]; - unsigned short nStackDepth; - unsigned short nTaskCount; -} sys_arch_state_t; - - - -//extern sys_arch_state_t s_sys_arch_state; - -//void sys_set_default_state(); -//void sys_set_state(signed char *pTaskName, unsigned short nStackSize); - -/* Message queue constants. */ -#define archMESG_QUEUE_LENGTH ( 6 ) -#endif /* __SYS_RTXC_H__ */ - diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/standalone/ethernetif.c b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/standalone/ethernetif.c deleted file mode 100644 index f3b74ee..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/standalone/ethernetif.c +++ /dev/null @@ -1,366 +0,0 @@ -/** - * @file - * Ethernet Interface for standalone applications (without RTOS) - works only for - * ethernet polling mode (polling for ethernet frame reception) - * - */ - -/* - * Copyright (c) 2001-2004 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ - -#include "lwip/opt.h" -#include "lwip/mem.h" -#include "netif/etharp.h" -#include "ethernetif.h" -#include "stm32f2x7_eth.h" -#include "main.h" -#include - -/* Network interface name */ -#define IFNAME0 's' -#define IFNAME1 't' - - -/* Ethernet Rx & Tx DMA Descriptors */ -extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB]; - -/* Ethernet Driver Receive buffers */ -extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; - -/* Ethernet Driver Transmit buffers */ -extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; - -/* Global pointers to track current transmit and receive descriptors */ -extern ETH_DMADESCTypeDef *DMATxDescToSet; -extern ETH_DMADESCTypeDef *DMARxDescToGet; - -/* Global pointer for last received frame infos */ -extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos; - -/** - * In this function, the hardware should be initialized. - * Called from ethernetif_init(). - * - * @param netif the already initialized lwip network interface structure - * for this ethernetif - */ -static void low_level_init(struct netif *netif) -{ -#ifdef CHECKSUM_BY_HARDWARE - int i; -#endif - /* set MAC hardware address length */ - netif->hwaddr_len = ETHARP_HWADDR_LEN; - - /* set MAC hardware address */ - netif->hwaddr[0] = MAC_ADDR0; - netif->hwaddr[1] = MAC_ADDR1; - netif->hwaddr[2] = MAC_ADDR2; - netif->hwaddr[3] = MAC_ADDR3; - netif->hwaddr[4] = MAC_ADDR4; - netif->hwaddr[5] = MAC_ADDR5; - - /* initialize MAC address in ethernet MAC */ - ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr); - - /* maximum transfer unit */ - netif->mtu = 1500; - - /* device capabilities */ - /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; - - /* Initialize Tx Descriptors list: Chain Mode */ - ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB); - /* Initialize Rx Descriptors list: Chain Mode */ - ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB); - -#ifdef CHECKSUM_BY_HARDWARE - /* Enable the TCP, UDP and ICMP checksum insertion for the Tx frames */ - for(i=0; iBuffer1Addr); - __IO ETH_DMADESCTypeDef *DmaTxDesc; - uint16_t framelength = 0; - uint32_t bufferoffset = 0; - uint32_t byteslefttocopy = 0; - uint32_t payloadoffset = 0; - - DmaTxDesc = DMATxDescToSet; - bufferoffset = 0; - - /* copy frame from pbufs to driver buffers */ - for(q = p; q != NULL; q = q->next) - { - /* Is this buffer available? If not, goto error */ - if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET) - { - errval = ERR_BUF; - goto error; - } - - /* Get bytes in current lwIP buffer */ - byteslefttocopy = q->len; - payloadoffset = 0; - - /* Check if the length of data to copy is bigger than Tx buffer size*/ - while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE ) - { - /* Copy data to Tx buffer*/ - memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) ); - - /* Point to next descriptor */ - DmaTxDesc = (ETH_DMADESCTypeDef *)(DmaTxDesc->Buffer2NextDescAddr); - - /* Check if the buffer is available */ - if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET) - { - errval = ERR_USE; - goto error; - } - - buffer = (u8 *)(DmaTxDesc->Buffer1Addr); - - byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset); - payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset); - framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset); - bufferoffset = 0; - } - - /* Copy the remaining bytes */ - memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), byteslefttocopy ); - bufferoffset = bufferoffset + byteslefttocopy; - framelength = framelength + byteslefttocopy; - } - - /* Note: padding and CRC for transmitted frame - are automatically inserted by DMA */ - - /* Prepare transmit descriptors to give to DMA*/ - ETH_Prepare_Transmit_Descriptors(framelength); - - errval = ERR_OK; - -error: - - /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */ - if ((ETH->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET) - { - /* Clear TUS ETHERNET DMA flag */ - ETH->DMASR = ETH_DMASR_TUS; - - /* Resume DMA transmission*/ - ETH->DMATPDR = 0; - } - return errval; -} - -/** - * Should allocate a pbuf and transfer the bytes of the incoming - * packet from the interface into the pbuf. - * - * @param netif the lwip network interface structure for this ethernetif - * @return a pbuf filled with the received packet (including MAC header) - * NULL on memory error - */ -static struct pbuf * low_level_input(struct netif *netif) -{ - struct pbuf *p, *q; - uint32_t len; - FrameTypeDef frame; - u8 *buffer; - __IO ETH_DMADESCTypeDef *DMARxDesc; - uint32_t bufferoffset = 0; - uint32_t payloadoffset = 0; - uint32_t byteslefttocopy = 0; - uint32_t i=0; - - /* get received frame */ - frame = ETH_Get_Received_Frame(); - - /* Obtain the size of the packet and put it into the "len" variable. */ - len = frame.length; - buffer = (u8 *)frame.buffer; - - /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */ - p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); - - if (p != NULL) - { - DMARxDesc = frame.descriptor; - bufferoffset = 0; - for(q = p; q != NULL; q = q->next) - { - byteslefttocopy = q->len; - payloadoffset = 0; - - /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/ - while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE ) - { - /* Copy data to pbuf*/ - memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset)); - - /* Point to next descriptor */ - DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr); - buffer = (unsigned char *)(DMARxDesc->Buffer1Addr); - - byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset); - payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset); - bufferoffset = 0; - } - /* Copy remaining data in pbuf */ - memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), byteslefttocopy); - bufferoffset = bufferoffset + byteslefttocopy; - } - } - - /* Release descriptors to DMA */ - DMARxDesc =frame.descriptor; - - /* Set Own bit in Rx descriptors: gives the buffers back to DMA */ - for (i=0; iSeg_Count; i++) - { - DMARxDesc->Status = ETH_DMARxDesc_OWN; - DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr); - } - - /* Clear Segment_Count */ - DMA_RX_FRAME_infos->Seg_Count =0; - - /* When Rx Buffer unavailable flag is set: clear it and resume reception */ - if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) - { - /* Clear RBUS ETHERNET DMA flag */ - ETH->DMASR = ETH_DMASR_RBUS; - /* Resume DMA reception */ - ETH->DMARPDR = 0; - } - return p; -} - -/** - * This function should be called when a packet is ready to be read - * from the interface. It uses the function low_level_input() that - * should handle the actual reception of bytes from the network - * interface. Then the type of the received packet is determined and - * the appropriate input function is called. - * - * @param netif the lwip network interface structure for this ethernetif - */ -err_t ethernetif_input(struct netif *netif) -{ - err_t err; - struct pbuf *p; - - /* move received packet into a new pbuf */ - p = low_level_input(netif); - - /* no packet could be read, silently ignore this */ - if (p == NULL) return ERR_MEM; - - /* entry point to the LwIP stack */ - err = netif->input(p, netif); - - if (err != ERR_OK) - { - LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); - pbuf_free(p); - } - return err; -} - -/** - * Should be called at the beginning of the program to set up the - * network interface. It calls the function low_level_init() to do the - * actual setup of the hardware. - * - * This function should be passed as a parameter to netif_add(). - * - * @param netif the lwip network interface structure for this ethernetif - * @return ERR_OK if the loopif is initialized - * ERR_MEM if private data couldn't be allocated - * any other err_t on error - */ -err_t ethernetif_init(struct netif *netif) -{ - LWIP_ASSERT("netif != NULL", (netif != NULL)); - -#if LWIP_NETIF_HOSTNAME - /* Initialize interface hostname */ - netif->hostname = "lwip"; -#endif /* LWIP_NETIF_HOSTNAME */ - - netif->name[0] = IFNAME0; - netif->name[1] = IFNAME1; - /* We directly use etharp_output() here to save a function call. - * You can instead declare your own function an call etharp_output() - * from it if you have to do some checks before sending (e.g. if link - * is available...) */ - netif->output = etharp_output; - netif->linkoutput = low_level_output; - - /* initialize the hardware */ - low_level_init(netif); - - return ERR_OK; -} \ No newline at end of file diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/standalone/ethernetif.h b/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/standalone/ethernetif.h deleted file mode 100644 index 9ff1408..0000000 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7/standalone/ethernetif.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __ETHERNETIF_H__ -#define __ETHERNETIF_H__ - - -#include "lwip/err.h" -#include "lwip/netif.h" - -err_t ethernetif_init(struct netif *netif); -err_t ethernetif_input(struct netif *netif); - -#endif diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dhcp.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dhcp.c index 8b62123..c2be8e5 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dhcp.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dhcp.c @@ -81,6 +81,7 @@ #include "lwip/autoip.h" #include "lwip/dns.h" #include "netif/etharp.h" +#include "lwip/igmp.h" #include @@ -343,11 +344,20 @@ dhcp_coarse_tmr() while (netif != NULL) { /* only act on DHCP configured interfaces */ if (netif->dhcp != NULL) { + /* Realtek add */ + /* Not check t0/t1/t2 when dhcp state is DHCP_OFF */ + if(netif->dhcp->state == DHCP_OFF){ + netif = netif->next; + continue; + } + /* Realtek add end */ /* timer is active (non zero), and triggers (zeroes) now? */ if (++netif->dhcp->lease_used == netif->dhcp->t0_timeout) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t0 timeout\n")); /* this clients' lease time has expired */ + igmp_report_groups_leave(netif); // not remove group to make able to report group when dhcp bind dhcp_release(netif); + netif->dhcp->seconds_elapsed = sys_now(); dhcp_discover(netif); }else if (netif->dhcp->t2_rebind_time-- == 1) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n")); @@ -480,6 +490,7 @@ dhcp_t1_timeout(struct netif *netif) ("dhcp_t1_timeout(): must renew\n")); /* This slightly different to RFC2131: DHCPREQUEST will be sent from state DHCP_RENEWING, not DHCP_BOUND */ + dhcp->seconds_elapsed = sys_now(); dhcp_renew(netif); } } @@ -682,6 +693,11 @@ dhcp_start(struct netif *netif) LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL ); } +#if DHCP_CREATE_RAND_XID && defined(LWIP_SRAND) + /* For each system startup, fill in a random seed with different system ticks. */ + LWIP_SRAND(); +#endif /* DHCP_CREATE_RAND_XID && defined(LWIP_SRAND) */ + /* clear data structure */ memset(dhcp, 0, sizeof(struct dhcp)); /* dhcp_set_state(&dhcp, DHCP_OFF); */ @@ -692,6 +708,7 @@ dhcp_start(struct netif *netif) return ERR_MEM; } ip_set_option(dhcp->pcb, SOF_BROADCAST); + ip_set_option(dhcp->pcb, SOF_REUSEADDR); /* set up local and remote port for the pcb */ udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT); @@ -699,6 +716,7 @@ dhcp_start(struct netif *netif) udp_recv(dhcp->pcb, dhcp_recv, netif); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n")); /* (re)start the DHCP negotiation */ + dhcp->seconds_elapsed = sys_now(); result = dhcp_discover(netif); if (result != ERR_OK) { /* free resources allocated above */ @@ -742,6 +760,7 @@ dhcp_inform(struct netif *netif) } dhcp.pcb = pcb; ip_set_option(dhcp.pcb, SOF_BROADCAST); + ip_set_option(dhcp.pcb, SOF_REUSEADDR); udp_bind(dhcp.pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_inform(): created new udp pcb\n")); } @@ -853,6 +872,9 @@ dhcp_decline(struct netif *netif) dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4); dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr))); + dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4); + dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(&dhcp->server_ip_addr))); + dhcp_option_trailer(dhcp); /* resize pbuf to reflect true size of options */ pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len); @@ -1182,6 +1204,21 @@ dhcp_reboot(struct netif *netif) return result; } +/** check if DHCP supplied netif->ip_addr + * + * @param netif the netif to check + * @return 1 if DHCP supplied netif->ip_addr (states BOUND or RENEWING), + * 0 otherwise + */ +u8_t +dhcp_supplied_address(const struct netif *netif) +{ + if ((netif != NULL) && (netif->dhcp != NULL)) { + struct dhcp* dhcp = netif->dhcp; + return (dhcp->state == DHCP_BOUND) || (dhcp->state == DHCP_RENEWING); + } + return 0; +} /** * Release a DHCP lease. @@ -1194,8 +1231,15 @@ dhcp_release(struct netif *netif) struct dhcp *dhcp = netif->dhcp; err_t result; u16_t msecs; + ip_addr_t server_ip_addr; + u8_t is_dhcp_supplied_address; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release()\n")); + if (dhcp == NULL) { + return ERR_ARG; + } + ip_addr_copy(server_ip_addr, dhcp->server_ip_addr); + is_dhcp_supplied_address = dhcp_supplied_address(netif); /* idle DHCP client */ dhcp_set_state(dhcp, DHCP_OFF); /* clean old DHCP offer */ @@ -1207,15 +1251,24 @@ dhcp_release(struct netif *netif) ip_addr_set_zero(&dhcp->offered_si_addr); #endif /* LWIP_DHCP_BOOTP_FILE */ dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0; - + dhcp->t1_renew_time = dhcp->t2_rebind_time = dhcp->lease_used = dhcp->t0_timeout = 0; + + if (!is_dhcp_supplied_address) { + /* don't issue release message when address is not dhcp-assigned */ + return ERR_OK; + } + /* create and initialize the DHCP message header */ result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE); if (result == ERR_OK) { + dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4); + dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(&server_ip_addr))); + dhcp_option_trailer(dhcp); pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len); - udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif); + udp_sendto_if(dhcp->pcb, dhcp->p_out, &server_ip_addr, DHCP_SERVER_PORT, netif); dhcp_delete_msg(dhcp); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n")); } else { @@ -1751,15 +1804,18 @@ dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type) LWIP_ASSERT("dhcp_create_msg: check that first pbuf can hold struct dhcp_msg", (dhcp->p_out->len >= sizeof(struct dhcp_msg))); - /* reuse transaction identifier in retransmissions */ - if (dhcp->tries == 0) { + /* DHCP_REQUEST should reuse 'xid' from DHCPOFFER */ + if (message_type != DHCP_REQUEST) { + /* reuse transaction identifier in retransmissions */ + if (dhcp->tries == 0) { #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND) - xid = LWIP_RAND(); + xid = LWIP_RAND(); #else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ - xid++; + xid++; #endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ + } + dhcp->xid = xid; } - dhcp->xid = xid; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("transaction id xid(%"X32_F")\n", xid)); @@ -1771,13 +1827,17 @@ dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type) dhcp->msg_out->hlen = netif->hwaddr_len; dhcp->msg_out->hops = 0; dhcp->msg_out->xid = htonl(dhcp->xid); - dhcp->msg_out->secs = 0; + if ((message_type == DHCP_DISCOVER) || (message_type == DHCP_REQUEST)) { + dhcp->msg_out->secs = (uint16_t)((sys_now() - dhcp->seconds_elapsed) / configTICK_RATE_HZ); + } else { + dhcp->msg_out->secs = 0; + } /* we don't need the broadcast flag since we can receive unicast traffic before being fully configured! */ dhcp->msg_out->flags = 0; ip_addr_set_zero(&dhcp->msg_out->ciaddr); /* set ciaddr to netif->ip_addr based on message_type and state */ - if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) || + if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) || (message_type == DHCP_RELEASE) || ((message_type == DHCP_REQUEST) && /* DHCP_BOUND not used for sending! */ ((dhcp->state==DHCP_RENEWING) || dhcp->state==DHCP_REBINDING))) { ip_addr_copy(dhcp->msg_out->ciaddr, netif->ip_addr); diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dns.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dns.c index d31ff8c..cfa8c2a 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dns.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/dns.c @@ -11,6 +11,8 @@ * Port to lwIP from uIP * by Jim Pettinato April 2007 + * security fixes and more by Simon Goldschmidt + * uIP version Copyright (c) 2002-2003, Adam Dunkels. * All rights reserved. * @@ -49,13 +51,13 @@ * The lwIP version of the resolver also adds a non-blocking version of * gethostbyname() that will work with a raw API application. This function * checks for an IP address string first and converts it if it is valid. - * gethostbyname() then does a dns_lookup() to see if the name is - * already in the table. If so, the IP is returned. If not, a query is + * gethostbyname() then does a dns_lookup() to see if the name is + * already in the table. If so, the IP is returned. If not, a query is * issued and the function returns with a ERR_INPROGRESS status. The app * using the dns client must then go into a waiting state. * * Once a hostname has been resolved (or found to be non-existent), - * the resolver code calls a specified callback function (which + * the resolver code calls a specified callback function (which * must be implemented by the module that uses the resolver). */ @@ -83,6 +85,32 @@ #include +/* A list of DNS security features follows */ +#define LWIP_DNS_SECURE_RAND_XID 1 +#define LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING 2 +#define LWIP_DNS_SECURE_RAND_SRC_PORT 4 +/** Use all DNS security features by default. + * This is overridable but should only be needed by very small targets + * or when using against non standard DNS servers. */ +#ifndef LWIP_DNS_SECURE +#define LWIP_DNS_SECURE (LWIP_DNS_SECURE_RAND_XID | LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT) +#endif + +/** Random generator function to create random TXIDs and source ports for queries */ +#ifndef DNS_RAND_TXID +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0) +#define DNS_RAND_TXID LWIP_RAND +#else +static u16_t dns_txid; +#define DNS_RAND_TXID() (++dns_txid) +#endif +#endif + +/** Limits the source port to be >= 1024 by default */ +#ifndef DNS_PORT_ALLOWED +#define DNS_PORT_ALLOWED(port) ((port) >= 1024) +#endif + /** DNS server IP address */ #ifndef DNS_SERVER_ADDRESS #define DNS_SERVER_ADDRESS(ipaddr) (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */ @@ -103,6 +131,72 @@ #define DNS_MAX_TTL 604800 #endif +/* The number of parallel requests (i.e. calls to dns_gethostbyname + * that cannot be answered from the DNS table. + * This is set to the table size by default. + */ +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) +#ifndef DNS_MAX_REQUESTS +#define DNS_MAX_REQUESTS DNS_TABLE_SIZE +#endif +#else +/* In this configuration, both arrays have to have the same size and are used + * like one entry (used/free) */ +#define DNS_MAX_REQUESTS DNS_TABLE_SIZE +#endif + +/* The number of UDP source ports used in parallel */ +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) +#ifndef DNS_MAX_SOURCE_PORTS +#define DNS_MAX_SOURCE_PORTS DNS_MAX_REQUESTS +#endif +#else +#ifdef DNS_MAX_SOURCE_PORTS +#undef DNS_MAX_SOURCE_PORTS +#endif +#define DNS_MAX_SOURCE_PORTS 1 +#endif + + +/* DNS protocol flags */ +#define DNS_FLAG1_RESPONSE 0x80 +#define DNS_FLAG1_OPCODE_STATUS 0x10 +#define DNS_FLAG1_OPCODE_INVERSE 0x08 +#define DNS_FLAG1_OPCODE_STANDARD 0x00 +#define DNS_FLAG1_AUTHORATIVE 0x04 +#define DNS_FLAG1_TRUNC 0x02 +#define DNS_FLAG1_RD 0x01 +#define DNS_FLAG2_RA 0x80 +#define DNS_FLAG2_ERR_MASK 0x0f +#define DNS_FLAG2_ERR_NONE 0x00 +#define DNS_FLAG2_ERR_NAME 0x03 + +/* DNS protocol states */ +#define DNS_STATE_UNUSED 0 +#define DNS_STATE_NEW 1 +#define DNS_STATE_ASKING 2 +#define DNS_STATE_DONE 3 + +#ifdef PACK_STRUCT_USE_INCLUDES +# include "arch/bpstruct.h" +#endif +PACK_STRUCT_BEGIN +/** DNS message header */ +struct dns_hdr { + PACK_STRUCT_FIELD(u16_t id); + PACK_STRUCT_FIELD(u8_t flags1); + PACK_STRUCT_FIELD(u8_t flags2); + PACK_STRUCT_FIELD(u16_t numquestions); + PACK_STRUCT_FIELD(u16_t numanswers); + PACK_STRUCT_FIELD(u16_t numauthrr); + PACK_STRUCT_FIELD(u16_t numextrarr); +} PACK_STRUCT_STRUCT; +PACK_STRUCT_END +#ifdef PACK_STRUCT_USE_INCLUDES +# include "arch/epstruct.h" +#endif +#define SIZEOF_DNS_HDR 12 + /** DNS query message structure. No packing needed: only used locally on the stack. */ struct dns_query { @@ -124,21 +218,35 @@ struct dns_answer { u16_t len; }; #define SIZEOF_DNS_ANSWER 10 +/* maximum allowed size for the struct due to non-packed */ +#define SIZEOF_DNS_ANSWER_ASSERT 12 /** DNS table entry */ struct dns_table_entry { + u32_t ttl; + ip_addr_t ipaddr; + u16_t txid; u8_t state; - u8_t numdns; + u8_t server_idx; u8_t tmr; u8_t retries; u8_t seqno; - u8_t err; - u32_t ttl; +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) + u8_t pcb_idx; +#endif char name[DNS_MAX_NAME_LENGTH]; - ip_addr_t ipaddr; +}; + +/** DNS request table entry: used when dns_gehostbyname cannot answer the + * request from the DNS table */ +struct dns_req_entry { /* pointer to callback on DNS query done */ dns_found_callback found; + /* argument passed to the callback function */ void *arg; +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) + u8_t dns_table_idx; +#endif }; #if DNS_LOCAL_HOSTLIST @@ -173,18 +281,56 @@ static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *ad static void dns_check_entries(void); /*----------------------------------------------------------------------------- - * Globales + * Globals *----------------------------------------------------------------------------*/ /* DNS variables */ -static struct udp_pcb *dns_pcb; +static struct udp_pcb *dns_pcbs[DNS_MAX_SOURCE_PORTS]; +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) +static u8_t dns_last_pcb_idx; +#endif static u8_t dns_seqno; static struct dns_table_entry dns_table[DNS_TABLE_SIZE]; +static struct dns_req_entry dns_requests[DNS_MAX_REQUESTS]; static ip_addr_t dns_servers[DNS_MAX_SERVERS]; /** Contiguous buffer for processing responses */ static u8_t dns_payload_buffer[LWIP_MEM_ALIGN_BUFFER(DNS_MSG_SIZE)]; static u8_t* dns_payload; +#ifndef LWIP_DNS_STRICMP +#define LWIP_DNS_STRICMP(str1, str2) dns_stricmp(str1, str2) +/** + * A small but sufficient implementation for case insensitive strcmp. + * This can be defined to e.g. stricmp for windows or strcasecmp for linux. */ +static int +dns_stricmp(const char* str1, const char* str2) +{ + char c1, c2; + + do { + c1 = *str1++; + c2 = *str2++; + if (c1 != c2) { + char c1_upc = c1 | 0x20; + if ((c1_upc >= 'a') && (c1_upc <= 'z')) { + /* characters are not equal an one is in the alphabet range: + downcase both chars and check again */ + char c2_upc = c2 | 0x20; + if (c1_upc != c2_upc) { + /* still not equal */ + /* don't care for < or > */ + return 1; + } + } else { + /* characters are not equal but none is in the alphabet range */ + return 1; + } + } + } while (c1 != 0); + return 0; +} +#endif /* LWIP_DNS_STRICMP */ + /** * Initialize the resolver: set up the UDP pcb and configure the default server * (DNS_SERVER_ADDRESS). @@ -194,31 +340,36 @@ dns_init() { ip_addr_t dnsserver; + LWIP_ASSERT("sanity check SIZEOF_DNS_QUERY", + sizeof(struct dns_query) == SIZEOF_DNS_QUERY); + LWIP_ASSERT("sanity check SIZEOF_DNS_ANSWER", + sizeof(struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT); + dns_payload = (u8_t *)LWIP_MEM_ALIGN(dns_payload_buffer); - + /* initialize default DNS server address */ DNS_SERVER_ADDRESS(&dnsserver); LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n")); /* if dns client not yet initialized... */ - if (dns_pcb == NULL) { - dns_pcb = udp_new(); +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) + if (dns_pcbs[0] == NULL) { + dns_pcbs[0] = udp_new(); + LWIP_ASSERT("dns_pcbs[0] != NULL", dns_pcbs[0] != NULL); - if (dns_pcb != NULL) { - /* initialize DNS table not needed (initialized to zero since it is a - * global variable) */ - LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0", - DNS_STATE_UNUSED == 0); + /* initialize DNS table not needed (initialized to zero since it is a + * global variable) */ + LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0", + DNS_STATE_UNUSED == 0); - /* initialize DNS client */ - udp_bind(dns_pcb, IP_ADDR_ANY, 0); - udp_recv(dns_pcb, dns_recv, NULL); - - /* initialize default DNS primary server */ - dns_setserver(0, &dnsserver); - } + /* initialize DNS client */ + udp_bind(dns_pcbs[0], IP_ADDR_ANY, 0); + udp_recv(dns_pcbs[0], dns_recv, NULL); } +#endif + /* initialize default DNS primary server */ + dns_setserver(0, &dnsserver); #if DNS_LOCAL_HOSTLIST dns_init_local(); #endif @@ -233,9 +384,12 @@ dns_init() void dns_setserver(u8_t numdns, ip_addr_t *dnsserver) { - if ((numdns < DNS_MAX_SERVERS) && (dns_pcb != NULL) && - (dnsserver != NULL) && !ip_addr_isany(dnsserver)) { - dns_servers[numdns] = (*dnsserver); + if (numdns < DNS_MAX_SERVERS) { + if (dnsserver != NULL) { + dns_servers[numdns] = (*dnsserver); + } else { + dns_servers[numdns] = *IP_ADDR_ANY; + } } } @@ -263,10 +417,8 @@ dns_getserver(u8_t numdns) void dns_tmr(void) { - if (dns_pcb != NULL) { - LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n")); - dns_check_entries(); - } + LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n")); + dns_check_entries(); } #if DNS_LOCAL_HOSTLIST @@ -311,7 +463,7 @@ dns_lookup_local(const char *hostname) #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC struct local_hostlist_entry *entry = local_hostlist_dynamic; while(entry != NULL) { - if(strcmp(entry->name, hostname) == 0) { + if (LWIP_DNS_STRICMP(entry->name, hostname) == 0) { return ip4_addr_get_u32(&entry->addr); } entry = entry->next; @@ -319,7 +471,7 @@ dns_lookup_local(const char *hostname) #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ int i; for (i = 0; i < sizeof(local_hostlist_static) / sizeof(struct local_hostlist_entry); i++) { - if(strcmp(local_hostlist_static[i].name, hostname) == 0) { + if (LWIP_DNS_STRICMP(local_hostlist_static[i].name, hostname) == 0) { return ip4_addr_get_u32(&local_hostlist_static[i].addr); } } @@ -343,7 +495,7 @@ dns_local_removehost(const char *hostname, const ip_addr_t *addr) struct local_hostlist_entry *entry = local_hostlist_dynamic; struct local_hostlist_entry *last_entry = NULL; while (entry != NULL) { - if (((hostname == NULL) || !strcmp(entry->name, hostname)) && + if (((hostname == NULL) || !LWIP_DNS_STRICMP(entry->name, hostname)) && ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) { struct local_hostlist_entry *free_entry; if (last_entry != NULL) { @@ -428,7 +580,7 @@ dns_lookup(const char *name) /* Walk through name list, return entry if found. If not, return NULL. */ for (i = 0; i < DNS_TABLE_SIZE; ++i) { if ((dns_table[i].state == DNS_STATE_DONE) && - (strcmp(name, dns_table[i].name) == 0)) { + (LWIP_DNS_STRICMP(name, dns_table[i].name) == 0)) { LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name)); ip_addr_debug_print(DNS_DEBUG, &(dns_table[i].ipaddr)); LWIP_DEBUGF(DNS_DEBUG, ("\n")); @@ -439,7 +591,6 @@ dns_lookup(const char *name) return IPADDR_NONE; } -#if DNS_DOES_NAME_CHECK /** * Compare the "dotted" name "query" with the encoded name "response" * to make sure an answer from the DNS server matches the current dns_table @@ -450,8 +601,8 @@ dns_lookup(const char *name) * @param response encoded hostname in the DNS response * @return 0: names equal; 1: names differ */ -static u8_t -dns_compare_name(unsigned char *query, unsigned char *response) +static char* +dns_compare_name(char *query, char *response) { unsigned char n; @@ -459,13 +610,13 @@ dns_compare_name(unsigned char *query, unsigned char *response) n = *response++; /** @see RFC 1035 - 4.1.4. Message compression */ if ((n & 0xc0) == 0xc0) { - /* Compressed name */ - break; + /* Compressed name: cannot be equal since we don't send them */ + return NULL; } else { /* Not compressed name */ while (n > 0) { if ((*query) != (*response)) { - return 1; + return NULL; } ++response; ++query; @@ -475,9 +626,8 @@ dns_compare_name(unsigned char *query, unsigned char *response) } } while (*response != 0); - return 0; + return response + 1; } -#endif /* DNS_DOES_NAME_CHECK */ /** * Walk through a compact encoded DNS name and return the end of the name. @@ -485,13 +635,13 @@ dns_compare_name(unsigned char *query, unsigned char *response) * @param query encoded DNS name in the DNS server response * @return end of the name */ -static unsigned char * -dns_parse_name(unsigned char *query) +static char * +dns_parse_name(char *query) { unsigned char n; do { - n = *query++; + n = (unsigned char) *query++; /** @see RFC 1035 - 4.1.4. Message compression */ if ((n & 0xc0) == 0xc0) { /* Compressed name */ @@ -511,14 +661,11 @@ dns_parse_name(unsigned char *query) /** * Send a DNS query packet. * - * @param numdns index of the DNS server in the dns_servers table - * @param name hostname to query - * @param id index of the hostname in dns_table, used as transaction ID in the - * DNS query packet + * @param entry the DNS table entry for which to send a request * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise */ static err_t -dns_send(u8_t numdns, const char* name, u8_t id) +dns_send(struct dns_table_entry* entry) { err_t err; struct dns_hdr *hdr; @@ -527,25 +674,27 @@ dns_send(u8_t numdns, const char* name, u8_t id) char *query, *nptr; const char *pHostname; u8_t n; + u8_t pcb_idx; LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n", - (u16_t)(numdns), name)); - LWIP_ASSERT("dns server out of array", numdns < DNS_MAX_SERVERS); - LWIP_ASSERT("dns server has no IP address set", !ip_addr_isany(&dns_servers[numdns])); + (u16_t)(entry->server_idx), entry->name)); + LWIP_ASSERT("dns server out of array", entry->server_idx < DNS_MAX_SERVERS); + LWIP_ASSERT("dns server has no IP address set", !ip_addr_isany(&dns_servers[entry->server_idx])); /* if here, we have either a new query or a retry on a previous query to process */ - p = pbuf_alloc(PBUF_TRANSPORT, SIZEOF_DNS_HDR + DNS_MAX_NAME_LENGTH + + p = pbuf_alloc(PBUF_TRANSPORT, SIZEOF_DNS_HDR + DNS_MAX_NAME_LENGTH + 1 + SIZEOF_DNS_QUERY, PBUF_RAM); if (p != NULL) { + u16_t realloc_size; LWIP_ASSERT("pbuf must be in one piece", p->next == NULL); /* fill dns header */ hdr = (struct dns_hdr*)p->payload; memset(hdr, 0, SIZEOF_DNS_HDR); - hdr->id = htons(id); + hdr->id = htons(entry->txid); hdr->flags1 = DNS_FLAG1_RD; hdr->numquestions = PP_HTONS(1); query = (char*)hdr + SIZEOF_DNS_HDR; - pHostname = name; + pHostname = entry->name; --pHostname; /* convert hostname into suitable query format. */ @@ -568,12 +717,19 @@ dns_send(u8_t numdns, const char* name, u8_t id) SMEMCPY(query, &qry, SIZEOF_DNS_QUERY); /* resize pbuf to the exact dns query */ - pbuf_realloc(p, (u16_t)((query + SIZEOF_DNS_QUERY) - ((char*)(p->payload)))); + realloc_size = (u16_t)((query + SIZEOF_DNS_QUERY) - ((char*)(p->payload))); + LWIP_ASSERT("p->tot_len >= realloc_size", p->tot_len >= realloc_size); + pbuf_realloc(p, realloc_size); - /* connect to the server for faster receiving */ - udp_connect(dns_pcb, &dns_servers[numdns], DNS_SERVER_PORT); +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) + pcb_idx = entry->pcb_idx; +#else + pcb_idx = 0; +#endif /* send dns packet */ - err = udp_sendto(dns_pcb, p, &dns_servers[numdns], DNS_SERVER_PORT); + LWIP_DEBUGF(DNS_DEBUG, ("sending DNS request ID %d for name \"%s\" to server %d\r\n", + entry->txid, entry->name, entry->server_idx)); + err = udp_sendto(dns_pcbs[pcb_idx], p, &dns_servers[entry->server_idx], DNS_SERVER_PORT); /* free pbuf */ pbuf_free(p); @@ -584,8 +740,151 @@ dns_send(u8_t numdns, const char* name, u8_t id) return err; } +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) +static struct udp_pcb* +dns_alloc_random_port(void) +{ + err_t err; + struct udp_pcb* ret; + + ret = udp_new(); + if (ret == NULL) { + /* out of memory, have to reuse an existing pcb */ + return NULL; + } + do { + u16_t port = DNS_RAND_TXID(); + if (!DNS_PORT_ALLOWED(port)) { + /* this port is not allowed, try again */ + err = ERR_USE; + continue; + } + err = udp_bind(ret, IP_ADDR_ANY, port); + } while(err == ERR_USE); + if ((err != ERR_OK) && (err != ERR_USE)) { + udp_remove(ret); + return NULL; + } + udp_recv(ret, dns_recv, NULL); + return ret; +} + /** - * dns_check_entry() - see if pEntry has not yet been queried and, if so, sends out a query. + * dns_alloc_pcb() - allocates a new pcb (or reuses an existing one) to be used + * for sending a request + * + * @return an index into dns_pcbs + */ +static u8_t +dns_alloc_pcb(void) +{ + u8_t i; + u8_t idx; + + for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) { + if (dns_pcbs[i] == NULL) { + break; + } + } + if (i < DNS_MAX_SOURCE_PORTS) { + dns_pcbs[i] = dns_alloc_random_port(); + if (dns_pcbs[i] != NULL) { + /* succeeded */ + dns_last_pcb_idx = i; + return i; + } + } + /* if we come here, creating a new UDP pcb failed, so we have to use + an already existing one */ + idx = dns_last_pcb_idx + 1; + for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) { + if (idx >= DNS_MAX_SOURCE_PORTS) { + idx = 0; + } + if (dns_pcbs[idx] != NULL) { + dns_last_pcb_idx = idx; + return idx; + } + } + return DNS_MAX_SOURCE_PORTS; +} +#endif /* ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) */ + +/** + * dns_call_found() - call the found callback and check if there are duplicate + * entries for the given hostname. If there are any, their found callback will + * be called and they will be removed. + * + * @param idx dns table index of the entry that is resolved or removed + * @param addr IP address for the hostname (or NULL on error or memory shortage) + */ +static void +dns_call_found(u8_t idx, ip_addr_t* addr) +{ + u8_t i; + LWIP_UNUSED_ARG(i); + +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) + for (i = 0; i < DNS_MAX_REQUESTS; i++) { + if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) { + (*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg); + /* flush this entry */ + dns_requests[i].found = NULL; + } + } +#else + if (dns_requests[idx].found) { + (*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg); + } + dns_requests[idx].found = NULL; +#endif +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) + /* close the pcb used unless other request are using it */ + for (i = 0; i < DNS_MAX_REQUESTS; i++) { + if (i == idx) { + continue; /* only check other requests */ + } + if (dns_table[i].state == DNS_STATE_ASKING) { + if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) { + /* another request is still using the same pcb */ + dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS; + break; + } + } + } + if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) { + /* if we come here, the pcb is not used any more and can be removed */ + udp_remove(dns_pcbs[dns_table[idx].pcb_idx]); + dns_pcbs[dns_table[idx].pcb_idx] = NULL; + dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS; + } +#endif +} + +/* Create a query transmission ID that is unique for all outstanding queries */ +static u16_t +dns_create_txid(void) +{ + u16_t txid; + u8_t i; + +again: + txid = DNS_RAND_TXID(); + + /* check whether the ID is unique */ + for (i = 0; i < DNS_TABLE_SIZE; i++) { + if ((dns_table[i].state == DNS_STATE_ASKING) && + (dns_table[i].txid == txid)) { + /* ID already used by another pending query */ + goto again; + } + } + + return txid; +} + +/** + * dns_check_entry() - see if entry has not yet been queried and, if so, sends out a query. * Check an entry in the dns_table: * - send out query for new entries * - retry old pending entries on timeout (also with different servers) @@ -597,21 +896,24 @@ static void dns_check_entry(u8_t i) { err_t err; - struct dns_table_entry *pEntry = &dns_table[i]; + struct dns_table_entry *entry = &dns_table[i]; LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE); - switch(pEntry->state) { + switch (entry->state) { case DNS_STATE_NEW: { + u16_t txid; /* initialize new entry */ - pEntry->state = DNS_STATE_ASKING; - pEntry->numdns = 0; - pEntry->tmr = 1; - pEntry->retries = 0; - + txid = dns_create_txid(); + entry->txid = txid; + entry->state = DNS_STATE_ASKING; + entry->server_idx = 0; + entry->tmr = 1; + entry->retries = 0; + /* send DNS packet for this entry */ - err = dns_send(pEntry->numdns, pEntry->name, i); + err = dns_send(entry); if (err != ERR_OK) { LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, ("dns_send returned error: %s\n", lwip_strerr(err))); @@ -619,50 +921,44 @@ dns_check_entry(u8_t i) break; } - case DNS_STATE_ASKING: { - if (--pEntry->tmr == 0) { - if (++pEntry->retries == DNS_MAX_RETRIES) { - if ((pEntry->numdns+1numdns+1])) { + case DNS_STATE_ASKING: + if (--entry->tmr == 0) { + if (++entry->retries == DNS_MAX_RETRIES) { + if ((entry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany(&dns_servers[entry->server_idx + 1])) { /* change of server */ - pEntry->numdns++; - pEntry->tmr = 1; - pEntry->retries = 0; + entry->server_idx++; + entry->tmr = 1; + entry->retries = 0; break; } else { - LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", pEntry->name)); + LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", entry->name)); /* call specified callback function if provided */ - if (pEntry->found) - (*pEntry->found)(pEntry->name, NULL, pEntry->arg); + dns_call_found(i, NULL); /* flush this entry */ - pEntry->state = DNS_STATE_UNUSED; - pEntry->found = NULL; + entry->state = DNS_STATE_UNUSED; break; } } /* wait longer for the next retry */ - pEntry->tmr = pEntry->retries; + entry->tmr = entry->retries; /* send DNS packet for this entry */ - err = dns_send(pEntry->numdns, pEntry->name, i); + err = dns_send(entry); if (err != ERR_OK) { LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, ("dns_send returned error: %s\n", lwip_strerr(err))); } } break; - } - - case DNS_STATE_DONE: { + case DNS_STATE_DONE: /* if the time to live is nul */ - if (--pEntry->ttl == 0) { - LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name)); - /* flush this entry */ - pEntry->state = DNS_STATE_UNUSED; - pEntry->found = NULL; + if ((entry->ttl == 0) || (--entry->ttl == 0)) { + LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", entry->name)); + /* flush this entry, there cannot be any related pending entries in this state */ + entry->state = DNS_STATE_UNUSED; } break; - } case DNS_STATE_UNUSED: /* nothing to do */ break; @@ -685,6 +981,27 @@ dns_check_entries(void) } } +/** + * Call dns_answer_ip_validate to check the IP in DNS answer is valid or not + */ +static u8_t +dns_answer_ip_validate(char *ptr) +{ + ip_addr_t ipaddr; + u32_t ntohl_addr; + + /* read the IP address after answer resource record's header */ + SMEMCPY(&ipaddr, (ptr + SIZEOF_DNS_ANSWER), sizeof(ip_addr_t)); + ntohl_addr = PP_NTOHL(ipaddr.addr); + + LWIP_DEBUGF(DNS_DEBUG, ("dns_validate_ip: ")); + ip_addr_debug_print(DNS_DEBUG, (&ipaddr)); + LWIP_DEBUGF(DNS_DEBUG, (", n:%x, h:%x\n", ipaddr.addr, ntohl_addr)); + + return ((ntohl_addr == IPADDR_ANY) || (ntohl_addr == IPADDR_LOOPBACK) || (ntohl_addr == IPADDR_BROADCAST) || + IP_MULTICAST(ntohl_addr) || IP_BADCLASS(ntohl_addr)) ? 0 : 1; +} + /** * Receive input function for DNS response packets arriving for the dns UDP pcb. * @@ -693,16 +1010,16 @@ dns_check_entries(void) static void dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port) { - u16_t i; - char *pHostname; + u8_t i, entry_idx = DNS_TABLE_SIZE; + u16_t txid; + char *ptr; struct dns_hdr *hdr; struct dns_answer ans; - struct dns_table_entry *pEntry; + struct dns_query qry; u16_t nquestions, nanswers; LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(pcb); - LWIP_UNUSED_ARG(addr); LWIP_UNUSED_ARG(port); /* is the dns message too big ? */ @@ -719,17 +1036,20 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t goto memerr; } - /* copy dns payload inside static buffer for processing */ + /* copy dns payload inside static buffer for processing */ if (pbuf_copy_partial(p, dns_payload, p->tot_len, 0) == p->tot_len) { - /* The ID in the DNS header should be our entry into the name table. */ + /* Match the ID in the DNS header with the name table. */ hdr = (struct dns_hdr*)dns_payload; - i = htons(hdr->id); - if (i < DNS_TABLE_SIZE) { - pEntry = &dns_table[i]; - if(pEntry->state == DNS_STATE_ASKING) { + txid = htons(hdr->id); + for (i = 0; i < DNS_TABLE_SIZE; i++) { + struct dns_table_entry *entry = &dns_table[i]; + entry_idx = i; + if ((entry->state == DNS_STATE_ASKING) && + (entry->txid == txid)) { + u8_t dns_err; /* This entry is now completed. */ - pEntry->state = DNS_STATE_DONE; - pEntry->err = hdr->flags2 & DNS_FLAG2_ERR_MASK; + entry->state = DNS_STATE_DONE; + dns_err = hdr->flags2 & DNS_FLAG2_ERR_MASK; /* We only care about the question(s) and the answers. The authrr and the extrarr are simply discarded. */ @@ -737,54 +1057,89 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t nanswers = htons(hdr->numanswers); /* Check for error. If so, call callback to inform. */ - if (((hdr->flags1 & DNS_FLAG1_RESPONSE) == 0) || (pEntry->err != 0) || (nquestions != 1)) { - LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", pEntry->name)); + if (((hdr->flags1 & DNS_FLAG1_RESPONSE) == 0) || (nquestions != 1)) { + LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", entry->name)); /* call callback to indicate error, clean up memory and return */ goto responseerr; } -#if DNS_DOES_NAME_CHECK - /* Check if the name in the "question" part match with the name in the entry. */ - if (dns_compare_name((unsigned char *)(pEntry->name), (unsigned char *)dns_payload + SIZEOF_DNS_HDR) != 0) { - LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", pEntry->name)); + /* Check whether response comes from the same network address to which the + question was sent. (RFC 5452) */ + if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) { /* call callback to indicate error, clean up memory and return */ goto responseerr; } -#endif /* DNS_DOES_NAME_CHECK */ - /* Skip the name in the "question" part */ - pHostname = (char *) dns_parse_name((unsigned char *)dns_payload + SIZEOF_DNS_HDR) + SIZEOF_DNS_QUERY; + /* Check if the name in the "question" part match with the name in the entry and + skip it if equal. */ + ptr = dns_compare_name(entry->name, (char*)dns_payload + SIZEOF_DNS_HDR); + if (ptr == NULL) { + LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); + /* call callback to indicate error, clean up memory and return */ + goto responseerr; + } + + /* check if "question" part matches the request */ + SMEMCPY(&qry, ptr, SIZEOF_DNS_QUERY); + if((qry.type != PP_HTONS(DNS_RRTYPE_A)) || (qry.cls != PP_HTONS(DNS_RRCLASS_IN))) { + LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); + /* call callback to indicate error, clean up memory and return */ + goto responseerr; + } + /* skip the rest of the "question" part */ + ptr += SIZEOF_DNS_QUERY; + + /* Check for error. If so, call callback to inform or change to next DNS server. */ + if (dns_err != 0) { + LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", entry->name)); + if ((entry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany(&dns_servers[entry->server_idx + 1])) { + /* change of server */ + entry->server_idx++; + entry->tmr = 1; + entry->retries = 0; + entry->state = DNS_STATE_ASKING; + goto memerr; /* ignore this packet */ + } else { + /* call callback to indicate error, clean up memory and return */ + goto responseerr; + } + } while (nanswers > 0) { /* skip answer resource record's host name */ - pHostname = (char *) dns_parse_name((unsigned char *)pHostname); + ptr = dns_parse_name(ptr); /* Check for IP address type and Internet class. Others are discarded. */ - SMEMCPY(&ans, pHostname, SIZEOF_DNS_ANSWER); + SMEMCPY(&ans, ptr, SIZEOF_DNS_ANSWER); if((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) && - (ans.len == PP_HTONS(sizeof(ip_addr_t))) ) { + (ans.len == PP_HTONS(sizeof(ip_addr_t))) && dns_answer_ip_validate(ptr)) { /* read the answer resource record's TTL, and maximize it if needed */ - pEntry->ttl = ntohl(ans.ttl); - if (pEntry->ttl > DNS_MAX_TTL) { - pEntry->ttl = DNS_MAX_TTL; + entry->ttl = ntohl(ans.ttl); + if (entry->ttl > DNS_MAX_TTL) { + entry->ttl = DNS_MAX_TTL; } /* read the IP address after answer resource record's header */ - SMEMCPY(&(pEntry->ipaddr), (pHostname+SIZEOF_DNS_ANSWER), sizeof(ip_addr_t)); - LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", pEntry->name)); - ip_addr_debug_print(DNS_DEBUG, (&(pEntry->ipaddr))); + SMEMCPY(&(entry->ipaddr), (ptr + SIZEOF_DNS_ANSWER), sizeof(ip_addr_t)); + LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", entry->name)); + ip_addr_debug_print(DNS_DEBUG, (&(entry->ipaddr))); LWIP_DEBUGF(DNS_DEBUG, ("\n")); /* call specified callback function if provided */ - if (pEntry->found) { - (*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg); + dns_call_found(entry_idx, &entry->ipaddr); + if (entry->ttl == 0) { + /* RFC 883, page 29: "Zero values are + interpreted to mean that the RR can only be used for the + transaction in progress, and should not be cached." + -> flush this entry now */ + goto flushentry; } /* deallocate memory and return */ goto memerr; } else { - pHostname = pHostname + SIZEOF_DNS_ANSWER + htons(ans.len); + ptr = ptr + SIZEOF_DNS_ANSWER + htons(ans.len); } --nanswers; } - LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", pEntry->name)); + LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", entry->name)); /* call callback to indicate error, clean up memory and return */ goto responseerr; } @@ -796,12 +1151,10 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t responseerr: /* ERROR: call specified callback function with NULL as name to indicate an error */ - if (pEntry->found) { - (*pEntry->found)(pEntry->name, NULL, pEntry->arg); - } + dns_call_found(entry_idx, NULL); +flushentry: /* flush this entry */ - pEntry->state = DNS_STATE_UNUSED; - pEntry->found = NULL; + dns_table[entry_idx].state = DNS_STATE_UNUSED; memerr: /* free pbuf */ @@ -813,30 +1166,55 @@ memerr: * Queues a new hostname to resolve and sends out a DNS query for that hostname * * @param name the hostname that is to be queried + * @param hostnamelen length of the hostname * @param found a callback founction to be called on success, failure or timeout * @param callback_arg argument to pass to the callback function * @return @return a err_t return code. */ static err_t -dns_enqueue(const char *name, dns_found_callback found, void *callback_arg) +dns_enqueue(const char *name, size_t hostnamelen, dns_found_callback found, + void *callback_arg) { u8_t i; u8_t lseq, lseqi; - struct dns_table_entry *pEntry = NULL; + struct dns_table_entry *entry = NULL; size_t namelen; + struct dns_req_entry* req; + +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) + u8_t r; + /* check for duplicate entries */ + for (i = 0; i < DNS_TABLE_SIZE; i++) { + if ((dns_table[i].state == DNS_STATE_ASKING) && + (LWIP_DNS_STRICMP(name, dns_table[i].name) == 0)) { + /* this is a duplicate entry, find a free request entry */ + for (r = 0; r < DNS_MAX_REQUESTS; r++) { + if (dns_requests[r].found == 0) { + dns_requests[r].found = found; + dns_requests[r].arg = callback_arg; + dns_requests[r].dns_table_idx = i; + LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": duplicate request\n", name)); + return ERR_INPROGRESS; + } + } + } + } + /* no duplicate entries found */ +#endif /* search an unused entry, or the oldest one */ - lseq = lseqi = 0; + lseq = 0; + lseqi = DNS_TABLE_SIZE; for (i = 0; i < DNS_TABLE_SIZE; ++i) { - pEntry = &dns_table[i]; + entry = &dns_table[i]; /* is it an unused entry ? */ - if (pEntry->state == DNS_STATE_UNUSED) + if (entry->state == DNS_STATE_UNUSED) { break; - + } /* check if this is the oldest completed entry */ - if (pEntry->state == DNS_STATE_DONE) { - if ((dns_seqno - pEntry->seqno) > lseq) { - lseq = dns_seqno - pEntry->seqno; + if (entry->state == DNS_STATE_DONE) { + if ((dns_seqno - entry->seqno) > lseq) { + lseq = dns_seqno - entry->seqno; lseqi = i; } } @@ -845,27 +1223,61 @@ dns_enqueue(const char *name, dns_found_callback found, void *callback_arg) /* if we don't have found an unused entry, use the oldest completed one */ if (i == DNS_TABLE_SIZE) { if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) { - /* no entry can't be used now, table is full */ + /* no entry can be used now, table is full */ LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name)); return ERR_MEM; } else { /* use the oldest completed one */ i = lseqi; - pEntry = &dns_table[i]; + entry = &dns_table[i]; } } +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) + /* find a free request entry */ + req = NULL; + for (r = 0; r < DNS_MAX_REQUESTS; r++) { + if (dns_requests[r].found == NULL) { + req = &dns_requests[r]; + break; + } + } + if (req == NULL) { + /* no request entry can be used now, table is full */ + LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS request entries table is full\n", name)); + return ERR_MEM; + } + req->dns_table_idx = i; +#else + /* in this configuration, the entry index is the same as the request index */ + req = &dns_requests[i]; +#endif + /* use this entry */ LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i))); /* fill the entry */ - pEntry->state = DNS_STATE_NEW; - pEntry->seqno = dns_seqno++; - pEntry->found = found; - pEntry->arg = callback_arg; - namelen = LWIP_MIN(strlen(name), DNS_MAX_NAME_LENGTH-1); - MEMCPY(pEntry->name, name, namelen); - pEntry->name[namelen] = 0; + entry->state = DNS_STATE_NEW; + entry->seqno = dns_seqno; + req->found = found; + req->arg = callback_arg; + namelen = LWIP_MIN(hostnamelen, DNS_MAX_NAME_LENGTH-1); + MEMCPY(entry->name, name, namelen); + entry->name[namelen] = 0; + +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) + entry->pcb_idx = dns_alloc_pcb(); + if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) { + /* failed to get a UDP pcb */ + LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": failed to allocate a pcb\n", name)); + entry->state = DNS_STATE_UNUSED; + req->found = NULL; + return ERR_MEM; + } + LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS pcb %"U16_F"\n", name, (u16_t)(entry->pcb_idx))); +#endif + + dns_seqno++; /* force to send query without waiting timer */ dns_check_entry(i); @@ -876,7 +1288,7 @@ dns_enqueue(const char *name, dns_found_callback found, void *callback_arg) /** * Resolve a hostname (string) into an IP address. - * NON-BLOCKING callback version for use with raw API! + * NON-BLOCKING callback version for use with raw API!!! * * Returns immediately with one of err_t return codes: * - ERR_OK if hostname is a valid IP address string or the host @@ -898,16 +1310,27 @@ dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback foun void *callback_arg) { u32_t ipaddr; + size_t hostnamelen; /* not initialized or no valid server yet, or invalid addr pointer * or invalid hostname or invalid hostname length */ - if ((dns_pcb == NULL) || (addr == NULL) || - (!hostname) || (!hostname[0]) || - (strlen(hostname) >= DNS_MAX_NAME_LENGTH)) { + if ((addr == NULL) || + (!hostname) || (!hostname[0])) { + return ERR_ARG; + } +#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) + if (dns_pcbs[0] == NULL) { + return ERR_ARG; + } +#endif + hostnamelen = strlen(hostname); + if (hostnamelen >= DNS_MAX_NAME_LENGTH) { + LWIP_DEBUGF(DNS_DEBUG, ("dns_gethostbyname: name too long to resolve")); return ERR_ARG; } + #if LWIP_HAVE_LOOPIF - if (strcmp(hostname, "localhost")==0) { + if (strcmp(hostname, "localhost") == 0) { ip_addr_set_loopback(addr); return ERR_OK; } @@ -925,7 +1348,7 @@ dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback foun } /* queue query with specified callback */ - return dns_enqueue(hostname, found, callback_arg); + return dns_enqueue(hostname, hostnamelen, found, callback_arg); } #endif /* LWIP_DNS */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/init.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/init.c index 8aea49d..3498c08 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/init.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/init.c @@ -57,6 +57,9 @@ #include "lwip/lwip_timers.h" #include "netif/etharp.h" #include "lwip/api.h" +#if IP_NAPT +#include "lwip/lwip_napt.h" +#endif /* Compile-time sanity checks for configuration errors. * These can be done independently of LWIP_DEBUG, without penalty. @@ -329,4 +332,8 @@ lwip_init(void) #if LWIP_TIMERS sys_timeouts_init(); #endif /* LWIP_TIMERS */ + +#if IP_NAPT && !IP_NAPT_DYNAMIC + ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX); +#endif /* IP_NAPT */ } diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/igmp.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/igmp.c index 45bb5d9..6d0d368 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/igmp.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/igmp.c @@ -271,13 +271,27 @@ igmp_report_groups(struct netif *netif) LWIP_DEBUGF(IGMP_DEBUG, ("igmp_report_groups: sending IGMP reports on if %p\n", netif)); while (group != NULL) { - if (group->netif == netif) { + if ((group->netif == netif) && (!ip_addr_cmp(&group->group_address, &allsystems))) { igmp_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR); } group = group->next; } } +/* Realtek added to only send igmp leave, but not remove group */ +void +igmp_report_groups_leave(struct netif *netif) +{ + struct igmp_group *group = igmp_group_list; + + while (group != NULL) { + if ((group->netif == netif) && (!ip_addr_cmp(&group->group_address, &allsystems)) && (group->last_reporter_flag)) { + igmp_send(group, IGMP_LEAVE_GROUP); + } + group = group->next; + } +} + /** * Search for a group in the global igmp_group_list * diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip.c index 16319ba..3174205 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip.c @@ -59,6 +59,13 @@ #include +#if IP_NAPT +#include "lwip/lwip_napt.h" +#endif + +extern struct netif xnetif[]; +#define lwip_getif(n) (&xnetif[n]) + /** Set this to 0 in the rare case of wanting to call an extra function to * generate the IP checksum (in contrast to calculating it on-the-fly). */ #ifndef LWIP_INLINE_IP_CHKSUM @@ -84,7 +91,7 @@ || (LWIP_IP_ACCEPT_UDP_PORT(port))) #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ /* accept custom port only */ -#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port)) +#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(dst_port)) #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ /* accept DHCP client port only */ #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT)) @@ -112,9 +119,6 @@ ip_addr_t current_iphdr_dest; /** The IP header ID of the next outgoing IP packet */ static u16_t ip_id; -/** The flag indicating whether the ethernet/mii is the default interface */ -extern int ethernet_if_default; - /** * Finds the appropriate network interface for a given IP address. It * searches the list of network interfaces linearly. A match is found @@ -128,40 +132,26 @@ struct netif * ip_route(ip_addr_t *dest) { struct netif *netif; -#if CONFIG_ETHERNET - struct netif *last_netif = NULL; -#endif - -#ifdef LWIP_HOOK_IP4_ROUTE - netif = LWIP_HOOK_IP4_ROUTE(dest); - if (netif != NULL) { - return netif; - } -#endif /* iterate through netifs */ - for (netif = netif_list; netif != NULL; netif = netif->next) { + for(netif = netif_list; netif != NULL; netif = netif->next) { /* network mask matches? */ if (netif_is_up(netif)) { if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) { /* return netif on which to forward IP packet */ -#if CONFIG_ETHERNET - if(ethernet_if_default == 1) - last_netif = netif; - else - return netif; -#else return netif; -#endif } } } - -#if CONFIG_ETHERNET - if(ethernet_if_default == 1 && last_netif != NULL) - return last_netif; -#endif - + /* iterate through netifs */ + for(netif = netif_list; netif != NULL; netif = netif->next) { + /* network mask matches? */ + if (netif_is_up(netif)) { + if (!ip_addr_isbroadcast(dest, netif) && netif == (struct netif *)lwip_getif(0)) { + return netif; + } + } + } if ((netif_default == NULL) || (!netif_is_up(netif_default))) { LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); @@ -173,42 +163,704 @@ ip_route(ip_addr_t *dest) return netif_default; } -#if IP_FORWARD /** - * Determine whether an IP address is in a reserved set of addresses - * that may not be forwarded, or whether datagrams to that destination - * may be forwarded. - * @param p the packet to forward - * @param dest the destination IP address - * @return 1: can forward 0: discard + * Finds the appropriate network interface for a source IP address. It + * searches the list of network interfaces linearly. A match is found + * if the masked IP address of the network interface equals the masked + * IP address given to the function. + * + * @param source the sourcination IP address for which to find the route + * @return the netif on which to send to reach source */ -static int -ip_canforward(struct pbuf *p) -{ - u32_t addr = ip4_addr_get_u32(ip_current_dest_addr()); - if (p->flags & PBUF_FLAG_LLBCAST) { - /* don't route link-layer broadcasts */ - return 0; - } - if ((p->flags & PBUF_FLAG_LLMCAST) && !IP_MULTICAST(addr)) { - /* don't route link-layer multicasts unless the destination address is an IP - multicast address */ - return 0; - } - if (IP_EXPERIMENTAL(addr)) { - return 0; - } - if (IP_CLASSA(addr)) { - u32_t net = addr & IP_CLASSA_NET; - if ((net == 0) || (net == (IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) { - /* don't route loopback packets */ - return 0; +struct netif * +ip_router(ip_addr_t *dest, ip_addr_t *source){ + struct netif *netif; + /* iterate through netifs */ + for(netif = netif_list; netif != NULL; netif = netif->next) { + /* network mask matches? */ + + if (netif_is_up(netif)) { + if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) { + /* return netif on which to forward IP packet */ + return netif; + } + } + + if (netif_is_up(netif)) { + if (ip_addr_netcmp(source, &(netif->ip_addr), &(netif->netmask))) { + /* return netif on which to forward IP packet */ + return netif; + } + } + } + + if ((netif_default == NULL) || (!netif_is_up(netif_default))) { + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); + IP_STATS_INC(ip.rterr); + snmp_inc_ipoutnoroutes(); + return NULL; + } + /* no matching netif found, use default netif */ + printf("ip_router %d %p\n", __LINE__, netif_default); + return netif_default; +} + + +#if IP_FORWARD +#if IP_NAPT +extern void *pvPortZalloc(size_t xWantedSize); +#define os_zalloc pvPortZalloc + +#define NO_IDX ((u16_t)-1) +#define NT(x) ((x) == NO_IDX ? NULL : &ip_napt_table[x]) + +u16_t napt_list = NO_IDX, napt_list_last = NO_IDX, napt_free = 0; + +static struct napt_table *ip_napt_table; +struct portmap_table *ip_portmap_table; + +int nr_active_napt_tcp = 0, nr_active_napt_udp = 0, nr_active_napt_icmp = 0; +uint16_t ip_napt_max = 0; +uint8_t ip_portmap_max = 0; + +void +ip_napt_init(uint16_t max_nat, uint8_t max_portmap) +{ + u16_t i; + + ip_napt_max = max_nat; + ip_portmap_max = max_portmap; + + ip_napt_table = (struct napt_table*)os_zalloc(sizeof(struct napt_table[ip_napt_max])); + ip_portmap_table = (struct portmap_table*)os_zalloc(sizeof(struct portmap_table[ip_portmap_max])); + + for (i = 0; i < ip_napt_max - 1; i++) + ip_napt_table[i].next = i + 1; + ip_napt_table[i].next = NO_IDX; +} + +void +ip_napt_enable(u32_t addr, int enable) +{ + struct netif *netif; + for (netif = netif_list; netif; netif = netif->next) { + if (netif_is_up(netif) && !ip_addr_isany(&netif->ip_addr) && netif->ip_addr.addr == addr) { + netif->napt = !!enable; + break; } } +} + +void +ip_napt_enable_no(u8_t number, int enable) +{ + struct netif *netif; + for (netif = netif_list; netif; netif = netif->next) { + if (netif->num == number) { + netif->napt = !!enable; + break; + } + } +} + +void checksumadjust(unsigned char *chksum, unsigned char *optr, + int olen, unsigned char *nptr, int nlen) + /* assuming: unsigned char is 8 bits, long is 32 bits. + - chksum points to the chksum in the packet + - optr points to the old data in the packet + - nptr points to the new data in the packet + */ + { + long x, old, new; + x=chksum[0]*256+chksum[1]; + x=~x & 0xFFFF; + while (olen) + { + old=optr[0]*256+optr[1]; optr+=2; + x-=old & 0xffff; + if (x<=0) { x--; x&=0xffff; } + olen-=2; + } + while (nlen) + { + new=nptr[0]*256+nptr[1]; nptr+=2; + x+=new & 0xffff; + if (x & 0x10000) { x++; x&=0xffff; } + nlen-=2; + } + x=~x & 0xFFFF; + chksum[0]=x/256; chksum[1]=x & 0xff; + } + + +/* t must be indexed by napt_free */ +static void +ip_napt_insert(struct napt_table *t) +{ + u16_t ti = t - ip_napt_table; + if (ti != napt_free) *((int*)1)=1; //DEBUG + napt_free = t->next; + t->prev = NO_IDX; + t->next = napt_list; + if (napt_list != NO_IDX) + NT(napt_list)->prev = ti; + napt_list = ti; + if (napt_list_last == NO_IDX) + napt_list_last = ti; + +#if LWIP_TCP + if (t->proto == IP_PROTO_TCP) + nr_active_napt_tcp++; +#endif +#if LWIP_UDP + if (t->proto == IP_PROTO_UDP) + nr_active_napt_udp++; +#endif +#if LWIP_ICMP + if (t->proto == IP_PROTO_ICMP) + nr_active_napt_icmp++; +#endif +//os_printf("T: %d, U: %d, I: %d\r\n", nr_active_napt_tcp, nr_active_napt_udp, nr_active_napt_icmp); +} + +static void +ip_napt_free(struct napt_table *t) +{ + u16_t ti = t - ip_napt_table; + if (ti == napt_list) + napt_list = t->next; + if (ti == napt_list_last) + napt_list_last = t->prev; + if (t->next != NO_IDX) + NT(t->next)->prev = t->prev; + if (t->prev != NO_IDX) + NT(t->prev)->next = t->next; + t->prev = NO_IDX; + t->next = napt_free; + napt_free = ti; + +#if LWIP_TCP + if (t->proto == IP_PROTO_TCP) + nr_active_napt_tcp--; +#endif +#if LWIP_UDP + if (t->proto == IP_PROTO_UDP) + nr_active_napt_udp--; +#endif +#if LWIP_ICMP + if (t->proto == IP_PROTO_ICMP) + nr_active_napt_icmp--; +#endif + LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_free\n")); + napt_debug_print(); +} + +#if LWIP_TCP +static u8_t +ip_napt_find_port(u8_t proto, u16_t port) +{ + int i, next; + for (i = napt_list; i != NO_IDX; i = next) { + struct napt_table *t = &ip_napt_table[i]; + next = t->next; + if (t->proto == proto && t->mport == port) + return 1; + } + return 0; +} + +static struct portmap_table * +ip_portmap_find(u8_t proto, u16_t mport); + +static u8_t +tcp_listening(u16_t port) +{ + struct tcp_pcb_listen *t; + for (t = tcp_listen_pcbs.listen_pcbs; t; t = t->next) + if (t->local_port == port) + return 1; + if (ip_portmap_find(IP_PROTO_TCP, port)) + return 1; + return 0; +} +#endif // LWIP_TCP + +#if LWIP_UDP +static u8_t +udp_listening(u16_t port) +{ + struct udp_pcb *pcb; + for (pcb = udp_pcbs; pcb; pcb = pcb->next) + if (pcb->local_port == port) + return 1; + if (ip_portmap_find(IP_PROTO_UDP, port)) + return 1; + return 0; +} +#endif // LWIP_UDP + +static u16_t +ip_napt_new_port(u8_t proto, u16_t port) +{ + if (PP_NTOHS(port) >= IP_NAPT_PORT_RANGE_START && PP_NTOHS(port) <= IP_NAPT_PORT_RANGE_END) + if (!ip_napt_find_port(proto, port) && !tcp_listening(port)) + return port; + for (;;) { + port = PP_HTONS(IP_NAPT_PORT_RANGE_START + + LWIP_RAND() % (IP_NAPT_PORT_RANGE_END - IP_NAPT_PORT_RANGE_START + 1)); + if (ip_napt_find_port(proto, port)) + continue; +#if LWIP_TCP + if (proto == IP_PROTO_TCP && tcp_listening(port)) + continue; +#endif // LWIP_TCP +#if LWIP_UDP + if (proto == IP_PROTO_UDP && udp_listening(port)) + continue; +#endif // LWIP_UDP + + return port; + } +} + +static struct napt_table* +ip_napt_find(u8_t proto, u32_t addr, u16_t port, u16_t mport, u8_t dest) +{ + u16_t i, next; + struct napt_table *t; + + LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_find\n")); + LWIP_DEBUGF(NAPT_DEBUG, ("looking up in table %s: %"U16_F".%"U16_F".%"U16_F".%"U16_F", port: %u, mport: %u\n", + (dest ? "dest" : "src"), + ip4_addr1_16(&addr), ip4_addr2_16(&addr), + ip4_addr3_16(&addr), ip4_addr4_16(&addr), + PP_HTONS(port), + PP_HTONS(mport))); + napt_debug_print(); + + u32_t now = sys_now(); + for (i = napt_list; i != NO_IDX; i = next) { + t = NT(i); + next = t->next; +#if LWIP_TCP + if (t->proto == IP_PROTO_TCP && + (((t->finack1 && (t->finack2 || !t->synack)) && + now - t->last > IP_NAPT_TIMEOUT_MS_TCP_DISCON) || + now - t->last > IP_NAPT_TIMEOUT_MS_TCP)) { + ip_napt_free(t); + continue; + } +#endif +#if LWIP_UDP + if (t->proto == IP_PROTO_UDP && now - t->last > IP_NAPT_TIMEOUT_MS_UDP) { + ip_napt_free(t); + continue; + } +#endif +#if LWIP_ICMP + if (t->proto == IP_PROTO_ICMP && now - t->last > IP_NAPT_TIMEOUT_MS_ICMP) { + ip_napt_free(t); + continue; + } +#endif + if (dest == 0 && t->proto == proto && t->src == addr && t->sport == port) { + t->last = now; + LWIP_DEBUGF(NAPT_DEBUG, ("found\n")); + return t; + } + if (dest == 1 && t->proto == proto && t->dest == addr && t->dport == port + && t->mport == mport) { + t->last = now; + LWIP_DEBUGF(NAPT_DEBUG, ("found\n")); + return t; + } + } + + LWIP_DEBUGF(NAPT_DEBUG, ("not found\n")); + return NULL; +} + +static u16_t +ip_napt_add(u8_t proto, u32_t src, u16_t sport, u32_t dest, u16_t dport) +{ + struct napt_table *t = ip_napt_find(proto, src, sport, 0, 0); + if (t) { + t->last = sys_now(); + t->dest = dest; + t->dport = dport; + /* move this entry to the top of napt_list */ + ip_napt_free(t); + ip_napt_insert(t); + + LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_add\n")); + napt_debug_print(); + + return t->mport; + } + t = NT(napt_free); + if (t) { + u16_t mport = sport; +#if LWIP_TCP + if (proto == IP_PROTO_TCP) + mport = ip_napt_new_port(IP_PROTO_TCP, sport); +#endif +#if LWIP_TCP + if (proto == IP_PROTO_UDP) + mport = ip_napt_new_port(IP_PROTO_UDP, sport); +#endif + t->last = sys_now(); + t->src = src; + t->dest = dest; + t->sport = sport; + t->dport = dport; + t->mport = mport; + t->proto = proto; + t->fin1 = t->fin2 = t->finack1 = t->finack2 = t->synack = t->rst = 0; + ip_napt_insert(t); + + LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_add\n")); + napt_debug_print(); + + return mport; + } + LWIP_DEBUGF(NAPT_DEBUG,("NAT table full\n")); + return 0; +} + +u8_t +ip_portmap_add(u8_t proto, u32_t maddr, u16_t mport, u32_t daddr, u16_t dport) +{ + mport = PP_HTONS(mport); + dport = PP_HTONS(dport); + int i; + + for (i = 0; i < ip_portmap_max; i++) { + struct portmap_table *p = &ip_portmap_table[i]; + if (p->valid && p->proto == proto && p->mport == mport) { + p->dport = dport; + p->daddr = daddr; + } else if (!p->valid) { + p->maddr = maddr; + p->daddr = daddr; + p->mport = mport; + p->dport = dport; + p->proto = proto; + p->valid = 1; + return 1; + } + } + return 0; +} + +static struct portmap_table * +ip_portmap_find(u8_t proto, u16_t mport) +{ + int i; + for (i = 0; i < ip_portmap_max; i++) { + struct portmap_table *p = &ip_portmap_table[i]; + if (!p->valid) + return 0; + if (p->proto == proto && p->mport == mport) + return p; + } + return NULL; +} + +static struct portmap_table * +ip_portmap_find_dest(u8_t proto, u16_t dport, u32_t daddr) +{ + int i; + for (i = 0; i < ip_portmap_max; i++) { + struct portmap_table *p = &ip_portmap_table[i]; + if (!p->valid) + return 0; + if (p->proto == proto && p->dport == dport && p->daddr == daddr) + return p; + } + return NULL; +} + + +u8_t +ip_portmap_remove(u8_t proto, u16_t mport) +{ + mport = PP_HTONS(mport); + struct portmap_table *last = &ip_portmap_table[ip_portmap_max - 1]; + struct portmap_table *m = ip_portmap_find(proto, mport); + if (!m) + return 0; + for (; m != last; m++) + memcpy(m, m + 1, sizeof(*m)); + last->valid = 0; return 1; } +#if LWIP_TCP +void +ip_napt_modify_port_tcp(struct tcp_hdr *tcphdr, u8_t dest, u16_t newval) +{ + if (dest) { + checksumadjust((char *)&tcphdr->chksum, (char *)&tcphdr->dest, 2, (char *)&newval, 2); + tcphdr->dest = newval; + } else { + checksumadjust((char *)&tcphdr->chksum, (char *)&tcphdr->src, 2, (char *)&newval, 2); + tcphdr->src = newval; + } +} + + +void +ip_napt_modify_addr_tcp(struct tcp_hdr *tcphdr, ip_addr_p_t *oldval, u32_t newval) +{ + checksumadjust((char *)&tcphdr->chksum, (char *)&oldval->addr, 4, (char *)&newval, 4); +} +#endif // LWIP_TCP + +#if LWIP_UDP +void +ip_napt_modify_port_udp(struct udp_hdr *udphdr, u8_t dest, u16_t newval) +{ + if (dest) { + checksumadjust((char *)&udphdr->chksum, (char *)&udphdr->dest, 2, (char *)&newval, 2); + udphdr->dest = newval; + } else { + checksumadjust((char *)&udphdr->chksum, (char *)&udphdr->src, 2, (char *)&newval, 2); + udphdr->src = newval; + } +} + +void +ip_napt_modify_addr_udp(struct udp_hdr *udphdr, ip_addr_p_t *oldval, u32_t newval) +{ + checksumadjust((char *)&udphdr->chksum, (char *)&oldval->addr, 4, (char *)&newval, 4); +} +#endif // LWIP_UDP + +void +ip_napt_modify_addr(struct ip_hdr *iphdr, ip_addr_p_t *field, u32_t newval) +{ + checksumadjust((char *)&IPH_CHKSUM(iphdr), (char *)&field->addr, 4, (char *)&newval, 4); + field->addr = newval; +} + + +/** + * NAPT for an input packet. It checks weather the destination is on NAPT + * table and modifythe packet destination address and port if needed. + * + * @param p the packet to forward (p->payload points to IP header) + * @param iphdr the IP header of the input packet + * @param inp the netif on which this packet was received + */ +static void +ip_napt_recv(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) +{ + struct portmap_table *m; + struct napt_table *t; + +#if LWIP_ICMP + /* NAPT for ICMP Echo Request using identifier */ + if (IPH_PROTO(iphdr) == IP_PROTO_ICMP) { + struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4); + if (iecho->type == ICMP_ER) { + t = ip_napt_find(IP_PROTO_ICMP, iphdr->src.addr, iecho->id, iecho->id, 1); + if (!t) + return; + ip_napt_modify_addr(iphdr, &iphdr->dest, t->src); + return; + } + + return; + } +#endif // LWIP_ICMP + +#if LWIP_TCP + if (IPH_PROTO(iphdr) == IP_PROTO_TCP) { + struct tcp_hdr *tcphdr = (struct tcp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4); + + LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_recv\n")); + LWIP_DEBUGF(NAPT_DEBUG, ("src: %"U16_F".%"U16_F".%"U16_F".%"U16_F", dest: %"U16_F".%"U16_F".%"U16_F".%"U16_F", ", + ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src), + ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), + ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest), + ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest))); + + LWIP_DEBUGF(NAPT_DEBUG, ("sport %u, dport: %u\n", + PP_HTONS(tcphdr->src), + PP_HTONS(tcphdr->dest))); + + m = ip_portmap_find(IP_PROTO_TCP, tcphdr->dest); + if (m) { + /* packet to mapped port: rewrite destination */ + if (m->dport != tcphdr->dest) + ip_napt_modify_port_tcp(tcphdr, 1, m->dport); + ip_napt_modify_addr_tcp(tcphdr, &iphdr->dest, m->daddr); + ip_napt_modify_addr(iphdr, &iphdr->dest, m->daddr); + return; + } + t = ip_napt_find(IP_PROTO_TCP, iphdr->src.addr, tcphdr->src, tcphdr->dest, 1); + if (!t) + return; /* Unknown TCP session; do nothing */ + + if (t->sport != tcphdr->dest) + ip_napt_modify_port_tcp(tcphdr, 1, t->sport); + ip_napt_modify_addr_tcp(tcphdr, &iphdr->dest, t->src); + ip_napt_modify_addr(iphdr, &iphdr->dest, t->src); + + if ((TCPH_FLAGS(tcphdr) & (TCP_SYN|TCP_ACK)) == (TCP_SYN|TCP_ACK)) + t->synack = 1; + if ((TCPH_FLAGS(tcphdr) & TCP_FIN)) + t->fin1 = 1; + if (t->fin2 && (TCPH_FLAGS(tcphdr) & TCP_ACK)) + t->finack2 = 1; /* FIXME: Currently ignoring ACK seq... */ + if (TCPH_FLAGS(tcphdr) & TCP_RST) + t->rst = 1; + return; + } +#endif // LWIP_TCP + +#if LWIP_UDP + if (IPH_PROTO(iphdr) == IP_PROTO_UDP) { + struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4); + m = ip_portmap_find(IP_PROTO_UDP, udphdr->dest); + if (m) { + /* packet to mapped port: rewrite destination */ + if (m->dport != udphdr->dest) + ip_napt_modify_port_udp(udphdr, 1, m->dport); + ip_napt_modify_addr_udp(udphdr, &iphdr->dest, m->daddr); + ip_napt_modify_addr(iphdr, &iphdr->dest, m->daddr); + return; + } + t = ip_napt_find(IP_PROTO_UDP, iphdr->src.addr, udphdr->src, udphdr->dest, 1); + if (!t) + return; /* Unknown session; do nothing */ + + if (t->sport != udphdr->dest) + ip_napt_modify_port_udp(udphdr, 1, t->sport); + ip_napt_modify_addr_udp(udphdr, &iphdr->dest, t->src); + ip_napt_modify_addr(iphdr, &iphdr->dest, t->src); + return; + } +#endif // LWIP_UDP +} + +/** + * NAPT for a forwarded packet. It checks weather we need NAPT and modify + * the packet source address and port if needed. + * + * @param p the packet to forward (p->payload points to IP header) + * @param iphdr the IP header of the input packet + * @param inp the netif on which this packet was received + * @param outp the netif on which this packet will be sent + * @return ERR_OK if packet should be sent, or ERR_RTE if it should be dropped + */ +static err_t +ip_napt_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp, struct netif *outp) +{ + if (!inp->napt) + return ERR_OK; + +#if LWIP_ICMP + /* NAPT for ICMP Echo Request using identifier */ + if (IPH_PROTO(iphdr) == IP_PROTO_ICMP) { + struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4); + if (iecho->type == ICMP_ECHO) { + /* register src addr and iecho->id and dest info */ + ip_napt_add(IP_PROTO_ICMP, iphdr->src.addr, iecho->id, iphdr->dest.addr, iecho->id); + + ip_napt_modify_addr(iphdr, &iphdr->src, outp->ip_addr.addr); + } + return ERR_OK; + } +#endif + +#if LWIP_TCP + if (IPH_PROTO(iphdr) == IP_PROTO_TCP) { + struct tcp_hdr *tcphdr = (struct tcp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4); + u16_t mport; + + struct portmap_table *m = ip_portmap_find_dest(IP_PROTO_TCP, tcphdr->src, iphdr->src.addr); + if (m) { + /* packet from port-mapped dest addr/port: rewrite source to this node */ + if (m->mport != tcphdr->src) + ip_napt_modify_port_tcp(tcphdr, 0, m->mport); + ip_napt_modify_addr_tcp(tcphdr, &iphdr->src, m->maddr); + ip_napt_modify_addr(iphdr, &iphdr->src, m->maddr); + return ERR_OK; + } + if ((TCPH_FLAGS(tcphdr) & (TCP_SYN|TCP_ACK)) == TCP_SYN && + PP_NTOHS(tcphdr->src) >= 1024) { + /* Register new TCP session to NAPT */ + mport = ip_napt_add(IP_PROTO_TCP, iphdr->src.addr, tcphdr->src, + iphdr->dest.addr, tcphdr->dest); + } else { + struct napt_table *t = ip_napt_find(IP_PROTO_TCP, iphdr->src.addr, tcphdr->src, 0, 0); + if (!t || t->dest != iphdr->dest.addr || t->dport != tcphdr->dest) { +#if LWIP_ICMP + icmp_dest_unreach(p, ICMP_DUR_PORT); +#endif + return ERR_RTE; /* Drop unknown TCP session */ + } + mport = t->mport; + if ((TCPH_FLAGS(tcphdr) & TCP_FIN)) + t->fin2 = 1; + if (t->fin1 && (TCPH_FLAGS(tcphdr) & TCP_ACK)) + t->finack1 = 1; /* FIXME: Currently ignoring ACK seq... */ + if (TCPH_FLAGS(tcphdr) & TCP_RST) + t->rst = 1; + } + + if (mport != tcphdr->src) + ip_napt_modify_port_tcp(tcphdr, 0, mport); + ip_napt_modify_addr_tcp(tcphdr, &iphdr->src, outp->ip_addr.addr); + ip_napt_modify_addr(iphdr, &iphdr->src, outp->ip_addr.addr); + return ERR_OK; + } +#endif + +#if LWIP_UDP + if (IPH_PROTO(iphdr) == IP_PROTO_UDP) { + struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4); + u16_t mport; + + struct portmap_table *m = ip_portmap_find_dest(IP_PROTO_UDP, udphdr->src, iphdr->src.addr); + if (m) { + /* packet from port-mapped dest addr/port: rewrite source to this node */ + if (m->mport != udphdr->src) + ip_napt_modify_port_udp(udphdr, 0, m->mport); + ip_napt_modify_addr_udp(udphdr, &iphdr->src, m->maddr); + ip_napt_modify_addr(iphdr, &iphdr->src, m->maddr); + return ERR_OK; + } + if (PP_NTOHS(udphdr->src) >= 1024) { + /* Register new UDP session */ + mport = ip_napt_add(IP_PROTO_UDP, iphdr->src.addr, udphdr->src, + iphdr->dest.addr, udphdr->dest); + } else { + struct napt_table *t = ip_napt_find(IP_PROTO_UDP, iphdr->src.addr, udphdr->src, 0, 0); + if (!t || t->dest != iphdr->dest.addr || t->dport != udphdr->dest) { +#if LWIP_ICMP + icmp_dest_unreach(p, ICMP_DUR_PORT); +#endif + return ERR_RTE; /* Drop unknown UDP session */ + } + mport = t->mport; + } + + if (mport != udphdr->src) + ip_napt_modify_port_udp(udphdr, 0, mport); + ip_napt_modify_addr_udp(udphdr, &iphdr->src, outp->ip_addr.addr); + ip_napt_modify_addr(iphdr, &iphdr->src, outp->ip_addr.addr); + return ERR_OK; + } +#endif + + return ERR_OK; +} +#endif // IP_NAPT + /** * Forwards an IP packet. It finds an appropriate route for the * packet, decrements the TTL value of the packet, adjusts the @@ -225,10 +877,6 @@ ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) PERF_START; - if (!ip_canforward(p)) { - goto return_noroute; - } - /* RFC3927 2.7: do not forward link-local addresses */ if (ip_addr_islinklocal(¤t_iphdr_dest)) { LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", @@ -243,24 +891,15 @@ ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n", ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest), ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest))); - /* @todo: send ICMP_DUR_NET? */ goto return_noroute; } -#ifdef CONFIG_DONT_CARE_TP /* Do not forward packets onto the same network interface on which * they arrived. */ - if((netif->flags & NETIF_FLAG_IPSWITCH) == 0) - { -#endif -#if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF - if (netif == inp) { - LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n")); - goto return_noroute; - } -#endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */ -#ifdef CONFIG_DONT_CARE_TP - } -#endif + if (netif == inp) { + LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n")); + goto return_noroute; + } + /* decrement TTL */ IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1); /* send ICMP if TTL == 0 */ @@ -275,13 +914,24 @@ ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) return; } +#if IP_NAPT + if (ip_napt_forward(p, iphdr, inp, netif) != ERR_OK) + return; +#endif + /* Incrementally update the IP checksum. */ - if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) { + if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffff - 0x100)) { IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1); } else { IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100)); } +/* os_printf("Old: %4x ", PP_NTOHS(IPH_CHKSUM(iphdr))); + + IPH_CHKSUM_SET(iphdr, 0); + IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); + os_printf("Now: %4x\r\n", PP_NTOHS(IPH_CHKSUM(iphdr))); +*/ LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest), ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest))); @@ -291,24 +941,6 @@ ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) snmp_inc_ipforwdatagrams(); PERF_STOP("ip_forward"); - /* don't fragment if interface has mtu set to 0 [loopif] */ -#ifdef CONFIG_DONT_CARE_TP - if ((netif->flags & NETIF_FLAG_IPSWITCH) &&(netif->mtu && (p->tot_len > netif->mtu)) ){ -#else - if (netif->mtu && (p->tot_len > netif->mtu)) { -#endif - if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) { -#if IP_FRAG - ip_frag(p, netif, ip_current_dest_addr()); -#else /* IP_FRAG */ - /* @todo: send ICMP Destination Unreacheable code 13 "Communication administratively prohibited"? */ -#endif /* IP_FRAG */ - } else { - /* send ICMP Destination Unreacheable code 4: "Fragmentation Needed and DF Set" */ - icmp_dest_unreach(p, ICMP_DUR_FRAG); - } - return; - } /* transmit pbuf on chosen interface */ netif->output(netif, p, ¤t_iphdr_dest); return; @@ -357,13 +989,6 @@ ip_input(struct pbuf *p, struct netif *inp) return ERR_OK; } -#ifdef LWIP_HOOK_IP4_INPUT - if (LWIP_HOOK_IP4_INPUT(p, inp)) { - /* the packet has been eaten */ - return ERR_OK; - } -#endif - /* obtain IP header length in number of 32-bit words */ iphdr_hlen = IPH_HL(iphdr); /* calculate IP header length in bytes */ @@ -406,6 +1031,12 @@ ip_input(struct pbuf *p, struct netif *inp) } #endif +#if IP_NAPT + /* for unicast packet, check NAPT table and modify dest if needed */ + if (!inp->napt && ip_addr_cmp(&iphdr->dest, &(inp->ip_addr))) + ip_napt_recv(p, iphdr, netif); +#endif + /* Trim pbuf. This should have been done at the netif layer, * but we'll do it anyway just to be sure that its done. */ pbuf_realloc(p, iphdr_len); @@ -500,7 +1131,7 @@ ip_input(struct pbuf *p, struct netif *inp) /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */ #if IP_ACCEPT_LINK_LAYER_ADDRESSING /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */ - if (check_ip_src && current_iphdr_src.addr != IPADDR_ANY) // !ip_addr_isany(¤t_iphdr_src)) + if (check_ip_src && (¤t_iphdr_src)->addr != IPADDR_ANY) #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ { if ((ip_addr_isbroadcast(¤t_iphdr_src, inp)) || (ip_addr_ismulticast(¤t_iphdr_src))) { @@ -521,12 +1152,7 @@ ip_input(struct pbuf *p, struct netif *inp) LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n")); #if IP_FORWARD /* non-broadcast packet? */ -#ifdef CONFIG_DONT_CARE_TP - if(inp->flags & NETIF_FLAG_IPSWITCH) -#else - if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp)) -#endif -{ + if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp)) { /* try to forward IP packet on (other) interfaces */ ip_forward(p, iphdr, inp); } else @@ -593,6 +1219,7 @@ ip_input(struct pbuf *p, struct netif *inp) if (raw_input(p, inp) == 0) #endif /* LWIP_RAW */ { + switch (IPH_PROTO(iphdr)) { #if LWIP_UDP case IP_PROTO_UDP: @@ -681,6 +1308,8 @@ ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0); } +#define _IPH_VHLTOS_SET(hdr, v, hl, tos) (hdr)->_v_hl = (htons(((v) << 12) | ((hl) << 8) | (tos))) + /** * Same as ip_output_if() but with the possibility to include IP options: * @@ -760,11 +1389,10 @@ err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF; chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16; #endif /* CHECKSUM_GEN_IP_INLINE */ - IPH_VHL_SET(iphdr, 4, ip_hlen / 4); IPH_TOS_SET(iphdr, tos); #if CHECKSUM_GEN_IP_INLINE - chk_sum += LWIP_MAKE_U16(tos, iphdr->_v_hl); + chk_sum += iphdr->_v_hl; #endif /* CHECKSUM_GEN_IP_INLINE */ IPH_LEN_SET(iphdr, htons(p->tot_len)); #if CHECKSUM_GEN_IP_INLINE @@ -828,7 +1456,7 @@ err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, } #endif /* IP_FRAG */ - LWIP_DEBUGF(IP_DEBUG, ("netif->output()")); + LWIP_DEBUGF(IP_DEBUG, ("netif->output()\n")); return netif->output(netif, p, dest); } @@ -906,9 +1534,9 @@ ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, return ERR_RTE; } - NETIF_SET_HWADDRHINT(netif, addr_hint); + netif->addr_hint = addr_hint; err = ip_output_if(p, src, dest, ttl, tos, proto, netif); - NETIF_SET_HWADDRHINT(netif, NULL); + netif->addr_hint = NULL; return err; } @@ -922,6 +1550,9 @@ void ip_debug_print(struct pbuf *p) { struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; + u8_t *payload; + + payload = (u8_t *)iphdr + IP_HLEN; LWIP_DEBUGF(IP_DEBUG, ("IP header:\n")); LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); @@ -957,3 +1588,38 @@ ip_debug_print(struct pbuf *p) LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); } #endif /* IP_DEBUG */ + +#if NAPT_DEBUG +/* Print NAPT table using LWIP_DEBUGF + */ +void +napt_debug_print() +{ + int i, next; + LWIP_DEBUGF(NAPT_DEBUG, ("NAPT table:\n")); + LWIP_DEBUGF(NAPT_DEBUG, (" src dest sport dport mport \n")); + LWIP_DEBUGF(NAPT_DEBUG, ("+-----------------------+-----------------------+-------+-------+-------+\n")); + for (i = napt_list; i != NO_IDX; i = next) { + struct napt_table *t = &ip_napt_table[i]; + next = t->next; + + LWIP_DEBUGF(NAPT_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" |", + ip4_addr1_16(&t->src), + ip4_addr2_16(&t->src), + ip4_addr3_16(&t->src), + ip4_addr4_16(&t->src))); + + LWIP_DEBUGF(NAPT_DEBUG, (" %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" |", + ip4_addr1_16(&t->dest), + ip4_addr2_16(&t->dest), + ip4_addr3_16(&t->dest), + ip4_addr4_16(&t->dest))); + + LWIP_DEBUGF(NAPT_DEBUG, (" %5"U16_F" | %5"U16_F" | %5"U16_F" |\n", + PP_HTONS(t->sport), + PP_HTONS(t->dport), + PP_HTONS(t->mport))); + + } +} +#endif /* NAPT_DEBUG */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip_frag.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip_frag.c index 8d18434..dd3922c 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip_frag.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip_frag.c @@ -223,7 +223,7 @@ ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed) /* @todo Can't we simply remove the last datagram in the * linked list behind reassdatagrams? */ - struct ip_reassdata *r, *oldest, *prev; + struct ip_reassdata *r, *oldest, *prev, *oldest_prev; int pbufs_freed = 0, pbufs_freed_current; int other_datagrams; @@ -232,6 +232,7 @@ ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed) do { oldest = NULL; prev = NULL; + oldest_prev = NULL; other_datagrams = 0; r = reassdatagrams; while (r != NULL) { @@ -240,9 +241,11 @@ ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed) other_datagrams++; if (oldest == NULL) { oldest = r; + oldest_prev = prev; } else if (r->timer <= oldest->timer) { /* older than the previous oldest */ oldest = r; + oldest_prev = prev; } } if (r->next != NULL) { @@ -251,7 +254,7 @@ ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed) r = r->next; } if (oldest != NULL) { - pbufs_freed_current = ip_reass_free_complete_datagram(oldest, prev); + pbufs_freed_current = ip_reass_free_complete_datagram(oldest, oldest_prev); pbufs_freed += pbufs_freed_current; } } while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1)); diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/lwip_timers.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/lwip_timers.c index cf7cb03..8b9134a 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/lwip_timers.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/lwip_timers.c @@ -62,7 +62,7 @@ /** The one and only timeout list */ static struct sys_timeo *next_timeout; -#if NO_SYS +#if NO_SYS || CONFIG_DYNAMIC_TICKLESS static u32_t timeouts_last_time; #endif /* NO_SYS */ @@ -242,7 +242,7 @@ void sys_timeouts_init(void) sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL); #endif /* LWIP_DNS */ -#if NO_SYS +#if NO_SYS || CONFIG_DYNAMIC_TICKLESS /* Initialise timestamp for sys_check_timeouts */ timeouts_last_time = sys_now(); #endif @@ -436,7 +436,54 @@ sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg) } else { time_needed = SYS_ARCH_TIMEOUT; } + +#if CONFIG_DYNAMIC_TICKLESS + if (time_needed == SYS_ARCH_TIMEOUT) { + /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message + could be fetched. We should now call the timeout handler and + deallocate the memory allocated for the timeout. */ + u8_t had_one; + u32_t now; + u32_t diff; + + now = sys_now(); + /* this cares for wraparounds */ + diff = now - timeouts_last_time; + do + { + had_one = 0; + tmptimeout = next_timeout; + if (tmptimeout && (tmptimeout->time <= diff)) { + /* timeout has expired */ + had_one = 1; + timeouts_last_time = now; + diff -= tmptimeout->time; + next_timeout = tmptimeout->next; + handler = tmptimeout->h; + arg = tmptimeout->arg; +#if LWIP_DEBUG_TIMERNAMES + if (handler != NULL) { + LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n", + tmptimeout->handler_name, arg)); + } +#endif /* LWIP_DEBUG_TIMERNAMES */ + memp_free(MEMP_SYS_TIMEOUT, tmptimeout); + if (handler != NULL) { + LOCK_TCPIP_CORE(); + handler(arg); + UNLOCK_TCPIP_CORE(); + } + } + /* repeat until all expired timers have been called */ + }while(had_one); + + LWIP_TCPIP_THREAD_ALIVE(); + /* We try again to fetch a message from the mbox. */ + goto again; + } + +#else if (time_needed == SYS_ARCH_TIMEOUT) { /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message could be fetched. We should now call the timeout handler and @@ -463,7 +510,9 @@ sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg) /* We try again to fetch a message from the mbox. */ goto again; - } else { + } +#endif + else { /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout occured. The time variable is set to the number of milliseconds we waited for the message. */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/mem.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/mem.c index 568407c..d3ab9ee 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/mem.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/mem.c @@ -274,6 +274,7 @@ void mem_init(void) { struct mem *mem; + LWIP_ASSERT("Sanity check alignment", (SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT-1)) == 0); diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/tcp.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/tcp.c index 8687693..79c75de 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/tcp.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/core/tcp.c @@ -1011,6 +1011,8 @@ tcp_slowtmr_start: if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { ++pcb_remove; } + + /* If the PCB should be removed, do it. */ if (pcb_remove) { diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/igmp.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/igmp.h index 8aabac2..949a904 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/igmp.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/igmp.h @@ -96,6 +96,8 @@ void igmp_input(struct pbuf *p, struct netif *inp, ip_addr_t *dest); err_t igmp_joingroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr); err_t igmp_leavegroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr); void igmp_tmr(void); +void igmp_report_groups_leave(struct netif *netif); + #ifdef __cplusplus } diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/ip.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/ip.h index 00c83a0..64cfc10 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/ip.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/ipv4/lwip/ip.h @@ -214,6 +214,12 @@ void ip_debug_print(struct pbuf *p); #define ip_debug_print(p) #endif /* IP_DEBUG */ +#if NAPT_DEBUG +void napt_debug_print()ICACHE_FLASH_ATTR; +#else +#define napt_debug_print(p) +#endif /* NAPT_DEBUG */ + #ifdef __cplusplus } #endif diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/debug.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/debug.h index 346e0f0..ba1065c 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/debug.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/debug.h @@ -75,8 +75,7 @@ LWIP_PLATFORM_ASSERT(message); handler;}} while(0) #endif /* LWIP_ERROR */ -#if LWIP_DEBUG -//#ifdef LWIP_DEBUG +#if LWIP_DEBUG //#ifdef LWIP_DEBUG /** print debug message only if debug message type is enabled... * AND is of correct type AND is at least LWIP_DBG_LEVEL */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dhcp.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dhcp.h index c8a8761..a724030 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dhcp.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dhcp.h @@ -68,6 +68,7 @@ struct dhcp ip_addr_t offered_si_addr; char boot_file_name[DHCP_FILE_LEN]; #endif /* LWIP_DHCP_BOOTPFILE */ + u32_t seconds_elapsed; }; /* MUST be compiled with "pack structs" or equivalent! */ @@ -138,7 +139,7 @@ void dhcp_coarse_tmr(void); void dhcp_fine_tmr(void); err_t dhcp_release_unicast(struct netif *netif); - + /** DHCP message item offsets and length */ #define DHCP_OP_OFS 0 #define DHCP_HTYPE_OFS 1 diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dns.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dns.h index 23646ed..cfc231a 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dns.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/dns.h @@ -70,45 +70,6 @@ extern "C" { #define DNS_RRCLASS_HS 4 /* Hesiod [Dyer 87] */ #define DNS_RRCLASS_FLUSH 0x800 /* Flush bit */ -/* DNS protocol flags */ -#define DNS_FLAG1_RESPONSE 0x80 -#define DNS_FLAG1_OPCODE_STATUS 0x10 -#define DNS_FLAG1_OPCODE_INVERSE 0x08 -#define DNS_FLAG1_OPCODE_STANDARD 0x00 -#define DNS_FLAG1_AUTHORATIVE 0x04 -#define DNS_FLAG1_TRUNC 0x02 -#define DNS_FLAG1_RD 0x01 -#define DNS_FLAG2_RA 0x80 -#define DNS_FLAG2_ERR_MASK 0x0f -#define DNS_FLAG2_ERR_NONE 0x00 -#define DNS_FLAG2_ERR_NAME 0x03 - -/* DNS protocol states */ -#define DNS_STATE_UNUSED 0 -#define DNS_STATE_NEW 1 -#define DNS_STATE_ASKING 2 -#define DNS_STATE_DONE 3 - -#ifdef PACK_STRUCT_USE_INCLUDES -# include "arch/bpstruct.h" -#endif -PACK_STRUCT_BEGIN -/** DNS message header */ -struct dns_hdr { - PACK_STRUCT_FIELD(u16_t id); - PACK_STRUCT_FIELD(u8_t flags1); - PACK_STRUCT_FIELD(u8_t flags2); - PACK_STRUCT_FIELD(u16_t numquestions); - PACK_STRUCT_FIELD(u16_t numanswers); - PACK_STRUCT_FIELD(u16_t numauthrr); - PACK_STRUCT_FIELD(u16_t numextrarr); -} PACK_STRUCT_STRUCT; -PACK_STRUCT_END -#ifdef PACK_STRUCT_USE_INCLUDES -# include "arch/epstruct.h" -#endif -#define SIZEOF_DNS_HDR 12 - /* The size used for the next line is rather a hack, but it prevents including socket.h in all files that include memp.h, and that would possibly break portability (since socket.h defines some types and constants possibly already define by the OS). @@ -148,6 +109,7 @@ void dns_setserver(u8_t numdns, ip_addr_t *dnsserver); ip_addr_t dns_getserver(u8_t numdns); err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg); +void dns_reset_ttl(); #if DNS_LOCAL_HOSTLIST && DNS_LOCAL_HOSTLIST_IS_DYNAMIC int dns_local_removehost(const char *hostname, const ip_addr_t *addr); diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/lwip_napt.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/lwip_napt.h new file mode 100644 index 0000000..945cddf --- /dev/null +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/lwip_napt.h @@ -0,0 +1,115 @@ +#ifndef __LWIP_NAPT_H__ +#define __LWIP_NAPT_H__ + +#include "lwip/opt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if IP_FORWARD +#if IP_NAPT + +/* Default size of the tables used for NAPT */ +#define IP_NAPT_MAX 512 +#define IP_PORTMAP_MAX 32 + +/* Timeouts in sec for the various protocol types */ +#define IP_NAPT_TIMEOUT_MS_TCP (30*60*1000) +#define IP_NAPT_TIMEOUT_MS_TCP_DISCON (20*1000) +#define IP_NAPT_TIMEOUT_MS_UDP (2*1000) +#define IP_NAPT_TIMEOUT_MS_ICMP (2*1000) + +#define IP_NAPT_PORT_RANGE_START 49152 +#define IP_NAPT_PORT_RANGE_END 61439 + +struct napt_table { + u32_t last; + u32_t src; + u32_t dest; + u16_t sport; + u16_t dport; + u16_t mport; + u8_t proto; + u8_t fin1 : 1; + u8_t fin2 : 1; + u8_t finack1 : 1; + u8_t finack2 : 1; + u8_t synack : 1; + u8_t rst : 1; + u16_t next, prev; +}; + +struct portmap_table { + u32_t maddr; + u32_t daddr; + u16_t mport; + u16_t dport; + u8_t proto; + u8 valid; +}; + +extern struct portmap_table *ip_portmap_table; + +/** + * Allocates and initializes the NAPT tables. + * + * @param max_nat max number of enties in the NAPT table (use IP_NAPT_MAX if in doubt) + * @param max_portmap max number of enties in the NAPT table (use IP_PORTMAP_MAX if in doubt) + */ +void +ip_napt_init(uint16_t max_nat, uint8_t max_portmap); + + +/** + * Enable/Disable NAPT for a specified interface. + * + * @param addr ip address of the interface + * @param enable non-zero to enable NAPT, or 0 to disable. + */ +void +ip_napt_enable(u32_t addr, int enable); + + +/** + * Enable/Disable NAPT for a specified interface. + * + * @param netif number of the interface + * @param enable non-zero to enable NAPT, or 0 to disable. + */ +void +ip_napt_enable_no(u8_t number, int enable); + + +/** + * Register port mapping on the external interface to internal interface. + * When the same port mapping is registered again, the old mapping is overwritten. + * In this implementation, only 1 unique port mapping can be defined for each target address/port. + * + * @param proto target protocol + * @param maddr ip address of the external interface + * @param mport mapped port on the external interface, in host byte order. + * @param daddr destination ip address + * @param dport destination port, in host byte order. + */ +u8_t +ip_portmap_add(u8_t proto, u32_t maddr, u16_t mport, u32_t daddr, u16_t dport); + + +/** + * Unregister port mapping on the external interface to internal interface. + * + * @param proto target protocol + * @param maddr ip address of the external interface + */ +u8_t +ip_portmap_remove(u8_t proto, u16_t mport); + +#endif /* IP_NAPT */ +#endif /* IP_FORWARD */ + +#ifdef __cplusplus +} +#endif + +#endif /* __LWIP_NAPT_H__ */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/netif.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/netif.h index fd44ec0..cecae44 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/netif.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/netif.h @@ -230,6 +230,9 @@ struct netif { u16_t loop_cnt_current; #endif /* LWIP_LOOPBACK_MAX_PBUFS */ #endif /* ENABLE_LOOPBACK */ +#if IP_NAPT + u8_t napt; +#endif }; #if LWIP_SNMP diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/opt.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/opt.h index e644178..ad1f71d 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/opt.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/opt.h @@ -508,6 +508,14 @@ #define IP_FORWARD 0 #endif +/** + * IP_NAPT==1: Enables the ability to do Network Address Port Translation (NAPT) + * on forwarded packets. This only makes sense with IP_FORWARD==1. + */ +#ifndef IP_NAPT +#define IP_NAPT 0 +#endif + /** * IP_OPTIONS_ALLOWED: Defines the behavior for IP options. * IP_OPTIONS_ALLOWED==0: All packets with IP options are dropped. @@ -1297,7 +1305,7 @@ * PPP_THREAD_NAME: The name assigned to the pppInputThread. */ #ifndef PPP_THREAD_NAME -#define PPP_THREAD_NAME "pppInputThread" +#define PPP_THREAD_NAME "pppIThrd" #endif /** @@ -2131,4 +2139,5 @@ #define DNS_DEBUG LWIP_DBG_OFF #endif + #endif /* __LWIP_OPT_H__ */ diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/sockets.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/sockets.h index 167ec1c..5046890 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/sockets.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/lwip/sockets.h @@ -363,7 +363,6 @@ int lwip_last_err_socket(int s); // errno #define select(a,b,c,d,e) lwip_select(a,b,c,d,e) #define ioctlsocket(a,b,c) lwip_ioctl(a,b,c) - #if LWIP_POSIX_SOCKETS_IO_NAMES #define read(a,b,c) lwip_read(a,b,c) #define write(a,b,c) lwip_write(a,b,c) diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/netif/etharp.h b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/netif/etharp.h index 859608d..cd53ee8 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/netif/etharp.h +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/include/netif/etharp.h @@ -140,6 +140,12 @@ PACK_STRUCT_END #define ETHTYPE_PPPOEDISC 0x8863U /* PPP Over Ethernet Discovery Stage */ #define ETHTYPE_PPPOE 0x8864U /* PPP Over Ethernet Session Stage */ +#define ETHTYPE_RARP 0x8035U +#define RARP_REQUEST 3 +#define RARP_REPLY 4 +void rarp_retrieve_hook_callback(void (*callback)(u8_t *, u8_t *)); + + /** MEMCPY-like macro to copy to/from struct eth_addr's that are local variables * or known to be 32-bit aligned within the protocol header. */ #ifndef ETHADDR32_COPY diff --git a/USDK/component/common/network/lwip/lwip_v1.4.1/src/netif/etharp.c b/USDK/component/common/network/lwip/lwip_v1.4.1/src/netif/etharp.c index a0d3d8e..0d65063 100644 --- a/USDK/component/common/network/lwip/lwip_v1.4.1/src/netif/etharp.c +++ b/USDK/component/common/network/lwip/lwip_v1.4.1/src/netif/etharp.c @@ -811,7 +811,7 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n")); /* request was not directed to us */ } else { - #ifdef CONFIG_DONT_CARE_TP +#ifdef CONFIG_DONT_CARE_TP if(netif->flags & NETIF_FLAG_IPSWITCH) netif->linkoutput(netif, p); else @@ -844,6 +844,72 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) pbuf_free(p); } +static void (*rarp_retrieve_cb)(u8_t *, u8_t *) = NULL; + +void rarp_retrieve_hook_callback(void (*callback)(u8_t *, u8_t *)) { + rarp_retrieve_cb = callback; +} + +static void +etharp_rarp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) +{ + struct etharp_hdr *hdr; + struct eth_hdr *ethhdr; +#if LWIP_AUTOIP + const u8_t * ethdst_hwaddr; +#endif /* LWIP_AUTOIP */ + + LWIP_ERROR("netif != NULL", (netif != NULL), return;); + + /* drop short ARP packets: we have to check for p->len instead of p->tot_len here + since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */ + if (p->len < SIZEOF_ETHARP_PACKET) { + ETHARP_STATS_INC(etharp.lenerr); + ETHARP_STATS_INC(etharp.drop); + pbuf_free(p); + return; + } + + ethhdr = (struct eth_hdr *)p->payload; + hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR); + + /* RFC 826 "Packet Reception": */ + if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) || + (hdr->hwlen != ETHARP_HWADDR_LEN) || + (hdr->protolen != sizeof(ip_addr_t)) || + (hdr->proto != PP_HTONS(ETHTYPE_IP))) { + + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, + ("etharp_arp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n", + hdr->hwtype, hdr->hwlen, hdr->proto, hdr->protolen)); + ETHARP_STATS_INC(etharp.proterr); + ETHARP_STATS_INC(etharp.drop); + pbuf_free(p); + return; + } + ETHARP_STATS_INC(etharp.recv); + + switch (hdr->opcode) { + /* RARP request? */ + case PP_HTONS(RARP_REQUEST): + LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_rarp_input: incoming RARP request\n")); + break; + + case PP_HTONS(RARP_REPLY): + if(rarp_retrieve_cb != NULL) + rarp_retrieve_cb((u8_t *)&hdr->dipaddr, (u8_t *)&hdr->dhwaddr.addr); + break; + + default: + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_rarp_input: RARP unknown opcode type %"S16_F"\n", htons(hdr->opcode))); + ETHARP_STATS_INC(etharp.err); + break; + + } + /* free ARP packet */ + pbuf_free(p); +} + /** Just a small helper function that sends a pbuf to an ethernet address * in the arp_table specified by the index 'arp_idx'. */ @@ -1243,7 +1309,10 @@ etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr, hdr->hwlen = ETHARP_HWADDR_LEN; hdr->protolen = sizeof(ip_addr_t); - ethhdr->type = PP_HTONS(ETHTYPE_ARP); + if((opcode == RARP_REQUEST) | (opcode == RARP_REPLY)) + ethhdr->type = PP_HTONS(ETHTYPE_RARP); + else + ethhdr->type = PP_HTONS(ETHTYPE_ARP); /* send ARP query */ result = netif->linkoutput(netif, p); ETHARP_STATS_INC(etharp.xmit); @@ -1380,6 +1449,14 @@ ethernet_input(struct pbuf *p, struct netif *netif) /* pass p to ARP module */ etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p); break; + + case PP_HTONS(ETHTYPE_RARP): + if (!(netif->flags & NETIF_FLAG_ETHARP)) { + goto free_and_return; + } + /* pass p to RARP module */ + etharp_rarp_input(netif, (struct eth_addr*)(netif->hwaddr), p); + break; #endif /* LWIP_ARP */ #if PPPOE_SUPPORT case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */ diff --git a/USDK/component/soc/realtek/8195a/fwlib/hal_crypto.h b/USDK/component/soc/realtek/8195a/fwlib/hal_crypto.h index 4269dfe..f5b023e 100644 --- a/USDK/component/soc/realtek/8195a/fwlib/hal_crypto.h +++ b/USDK/component/soc/realtek/8195a/fwlib/hal_crypto.h @@ -53,7 +53,10 @@ typedef enum _SHA2_TYPE_ { extern int rtl_cryptoEngine_init(void); extern void rtl_cryptoEngine_info(void); +extern _LONG_CALL_ int __rtl_cryptoEngine_exit_v1_00(void); +extern int init_status; +#define rtl_cryptoEngine_deinit __rtl_cryptoEngine_exit_v1_00 // // Authentication diff --git a/USDK/component/soc/realtek/8195a/misc/iar_utility/common/tools/rtlaimage.exe b/USDK/component/soc/realtek/8195a/misc/iar_utility/common/tools/rtlaimage.exe new file mode 100644 index 0000000..7308aed Binary files /dev/null and b/USDK/component/soc/realtek/8195a/misc/iar_utility/common/tools/rtlaimage.exe differ diff --git a/USDK/flasher.mk b/USDK/flasher.mk index 3a88c49..32f6848 100644 --- a/USDK/flasher.mk +++ b/USDK/flasher.mk @@ -37,6 +37,7 @@ endif PICK = $(TOOLS_PATH)pick$(EXE) PADDING = $(TOOLS_PATH)padding$(EXE) CHCKSUM = $(TOOLS_PATH)checksum$(EXE) +IMAGETOOL = $(TOOLS_PATH)rtlaimage$(EXE) # openocd tools OPENOCD = $(OPENOCD_PATH)openocd.exe @@ -72,11 +73,9 @@ NMAPFILE = $(OBJ_DIR)/$(TARGET).nmap RAM1_IMAGE ?= $(BIN_DIR)/ram_1.bin RAM1P_IMAGE ?= $(BIN_DIR)/ram_1.p.bin -RAM1R_IMAGE ?= $(BIN_DIR)/ram_1.r.bin RAM2_IMAGE = $(BIN_DIR)/ram_2.bin RAM2P_IMAGE = $(BIN_DIR)/ram_2.p.bin -RAM2NS_IMAGE = $(BIN_DIR)/ram_2.ns.bin RAM3_IMAGE = $(BIN_DIR)/sdram.bin RAM3P_IMAGE = $(BIN_DIR)/sdram.p.bin @@ -89,19 +88,29 @@ OTA_IMAGE = $(BIN_DIR)/ota.bin mp: FLASH_IMAGE = $(BIN_DIR)/ram_all_mp.bin mp: OTA_IMAGE = $(BIN_DIR)/ota_mp.bin -TST_IMAGE = $(BIN_DIR)/ram_2.bin -.PHONY: genbin1 genbin23 flashburn reset test readfullflash flashboot flashwebfs flash_OTA runram runsdram +.PHONY: genbin flashburn reset test readfullflash flashboot flashwebfs flash_OTA runram runsdram .NOTPARALLEL: all mp genbin1 genbin23 flashburn reset test readfullflash _endgenbin flashwebfs flash_OTA -all: $(ELFFILE) $(OTA_IMAGE) $(FLASH_IMAGE) _endgenbin -mp: $(ELFFILE) $(OTA_IMAGE) $(FLASH_IMAGE) _endgenbin +all: $(ELFFILE) $(FLASH_IMAGE) _endgenbin +mp: $(ELFFILE) $(FLASH_IMAGE) _endgenbin -genbin1: $(ELFFILE) $(RAM1P_IMAGE) +genbin: $(ELFFILE) $(FLASH_IMAGE) _endgenbin -genbin23: $(ELFFILE) $(OTA_IMAGE) $(FLASH_IMAGE) _endgenbin +$(ELFFILE): + $(error Falsher: file $@ not found) +$(NMAPFILE): $(ELFFILE) + @echo "===========================================================" + @echo "Build names map file" + @echo $@ + @$(NM) $< | sort > $@ +# @echo "===========================================================" +$(FLASH_IMAGE):$(ELFFILE) + @echo "===========================================================" + $(IMAGETOOL) -a -r -o $(BIN_DIR)/ $(ELFFILE) + _endgenbin: @echo "-----------------------------------------------------------" @echo "Image ($(OTA_IMAGE)) size $(shell printf '%d\n' $$(( $$(stat --printf="%s" $(OTA_IMAGE)) )) ) bytes" @@ -121,7 +130,7 @@ runsdram: readfullflash: @$(JLINK_PATH)$(JLINK_EXE) -Device CORTEX-M3 -If SWD -Speed 1000 $(FLASHER_PATH)RTL_FFlash.JLinkScript - + flashburn: @echo define call1>$(FLASHER_PATH)flash_file.jlink @@ -233,14 +242,14 @@ reset: runram: @$(OPENOCD) -f interface/$(FLASHER).cfg -c 'transport select swd' -c 'adapter_khz 1000' \ -f $(FLASHER_PATH)rtl8710.ocd -c 'init' -c 'reset halt' -c 'adapter_khz $(FLASHER_SPEED)' \ - -c 'load_image $(RAM1R_IMAGE) 0x10000bc8 bin' \ + -c 'load_image $(RAM1_IMAGE) 0x10000bc8 bin' \ -c 'load_image $(RAM2_IMAGE) 0x10006000 bin' \ -c 'mww 0x40000210 0x20111157' -c 'rtl8710_reboot' -c shutdown runsdram: @$(OPENOCD) -f interface/$(FLASHER).cfg -c 'transport select swd' -c 'adapter_khz 1000' \ -f $(FLASHER_PATH)rtl8710.ocd -c 'init' -c 'reset halt' -c 'adapter_khz $(FLASHER_SPEED)' \ - -c 'load_image $(RAM1R_IMAGE) 0x10000bc8 bin' \ + -c 'load_image $(RAM1_IMAGE) 0x10000bc8 bin' \ -c 'load_image $(RAM2_IMAGE) 0x10006000 bin' \ -c 'boot_load_srdam $(RAM3_IMAGE) 0x30000000' \ -c shutdown @@ -248,120 +257,6 @@ runsdram: endif endif -$(NMAPFILE): $(ELFFILE) - @echo "===========================================================" - @echo "Build names map file" - @echo $@ - @$(NM) $< | sort > $@ -# @echo "===========================================================" - -$(FLASH_IMAGE): $(RAM1P_IMAGE) $(RAM2P_IMAGE) $(RAM3P_IMAGE) - @echo "===========================================================" - @echo "Make Flash image ($(FLASH_IMAGE))" -# @echo "===========================================================" - @mkdir -p $(BIN_DIR) - @rm -f $(FLASH_IMAGE) - @cat $(RAM1P_IMAGE) > $(FLASH_IMAGE) -# @chmod 777 $(FLASH_IMAGE) -ifdef PADDINGSIZE - @$(PADDING) $(PADDINGSIZE) 0xFF $(FLASH_IMAGE) -endif - @cat $(RAM2P_IMAGE) >> $(FLASH_IMAGE) - @cat $(RAM3P_IMAGE) >> $(FLASH_IMAGE) -# @echo "Image ($(FLASH_IMAGE)) size $(shell printf '%d\n' $$(( $$(stat --printf="%s" $(FLASH_IMAGE)) )) ) bytes" -# @echo "===========================================================" -# @rm $(BIN_DIR)/ram_*.p.bin - -$(OTA_IMAGE): $(RAM2NS_IMAGE) $(RAM3_IMAGE) - @echo "===========================================================" - @echo "Make OTA image ($(OTA_IMAGE))" - @rm -f $(OTA_IMAGE) - @cat $(RAM2NS_IMAGE) > $(OTA_IMAGE) - @cat $(RAM3P_IMAGE) >> $(OTA_IMAGE) -# @chmod 777 $(OTA_IMAGE) - @$(CHCKSUM) $(OTA_IMAGE) || true -# @echo "===========================================================" - -$(RAM1P_IMAGE): $(ELFFILE) $(NMAPFILE) - @echo "===========================================================" - @echo "Create image1r ($(RAM1R_IMAGE))" -# @echo "===========================================================" .bootloader -ifdef COMPILED_BOOT - @mkdir -p $(BIN_DIR) - @rm -f $(RAM1_IMAGE) $(RAM1R_IMAGE) -ifdef COMPILED_BOOT_BIN - @$(eval RAM1_START_ADDR := $(shell grep _binary_build_bin_ram_1_r_bin_start $(NMAPFILE) | awk '{print $$1}')) - @$(eval RAM1_END_ADDR := $(shell grep _binary_build_bin_ram_1_r_bin_end $(NMAPFILE) | awk '{print $$1}')) -else - @$(eval RAM1_START_ADDR := $(shell grep __ram_image1_text_start__ $(NMAPFILE) | awk '{print $$1}')) - @$(eval RAM1_END_ADDR := $(shell grep __ram_image1_text_end__ $(NMAPFILE) | awk '{print $$1}')) -endif - $(if $(RAM1_START_ADDR),,$(error "Not found __ram_image1_text_start__!")) - $(if $(RAM1_END_ADDR),,$(error "Not found __ram_image1_text_end__!")) -ifeq ($(RAM1_START_ADDR),$(RAM1_END_ADDR)) -ifdef COMPILED_BOOT_BIN - $(OBJCOPY) --change-section-address .boot.head=0x10000ba8 -j .boot.head -j .bootloader -Obinary $(ELFFILE) $(RAM1P_IMAGE) -else -# $(OBJCOPY) -j .rom_ram -Obinary $(ELFFILE) $(RAM_IMAGE) - $(OBJCOPY) -j .ram.start.table -j .ram_image1.text -Obinary $(ELFFILE) $(RAM1R_IMAGE) - $(PICK) 0x$(RAM1_START_ADDR) 0x$(RAM1_END_ADDR) $(RAM1R_IMAGE) $(RAM1P_IMAGE) head+reset_offset 0x0B000 -endif -else - $(error "BOOT-image size = 0") -# $(error Flasher: COMPILE_BOOT = No) -endif -else - @if [ -s $(RAM1R_IMAGE) ]; then echo "Use external $(RAM1R_IMAGE)!"; fi -endif - -$(RAM2P_IMAGE): $(ELFFILE) $(NMAPFILE) - @echo "===========================================================" - @echo "Create image2p ($(RAM2P_IMAGE))" -# @echo "===========================================================" - @mkdir -p $(BIN_DIR) - @rm -f $(RAM2_IMAGE) $(RAM2P_IMAGE) - @$(eval RAM2_START_ADDR = $(shell grep __ram_image2_text $(NMAPFILE) | grep _start__ | awk '{print $$1}')) - @$(eval RAM2_END_ADDR = $(shell grep __ram_image2_text $(NMAPFILE) | grep _end__ | awk '{print $$1}')) - $(if $(RAM2_START_ADDR),,$(error "Not found __ram_image2_text_start__!")) - $(if $(RAM2_END_ADDR),,$(error "Not found __ram_image2_text_end__!")) - @$(OBJCOPY) -j .image2.start.table -j .ram_image2.text -j .ram_image2.rodata -j .ram.data -Obinary $(ELFFILE) $(RAM2_IMAGE) - @$(PICK) 0x$(RAM2_START_ADDR) 0x$(RAM2_END_ADDR) $(RAM2_IMAGE) $(RAM2P_IMAGE) body+reset_offset+sig - -$(RAM2NS_IMAGE):$(ELFFILE) $(NMAPFILE) - @echo "===========================================================" - @echo "Create image2ns ($(RAM2NS_IMAGE))" -# @echo "===========================================================" - mkdir -p $(BIN_DIR) - rm -f $(RAM2_IMAGE) $(RAM2NS_IMAGE) - $(eval RAM2_START_ADDR = $(shell grep __ram_image2_text $(NMAPFILE) | grep _start__ | awk '{print $$1}')) - $(eval RAM2_END_ADDR = $(shell grep __ram_image2_text $(NMAPFILE) | grep _end__ | awk '{print $$1}')) - $(if $(RAM2_START_ADDR),,$(error "Not found __ram_image2_text_start__!")) - $(if $(RAM2_END_ADDR),,$(error "Not found __ram_image2_text_end__!")) - $(OBJCOPY) -j .image2.start.table -j .ram_image2.text -j .ram_image2.rodata -j .ram.data -Obinary $(ELFFILE) $(RAM2_IMAGE) - $(PICK) 0x$(RAM2_START_ADDR) 0x$(RAM2_END_ADDR) $(RAM2_IMAGE) $(RAM2NS_IMAGE) body+reset_offset - -$(RAM3_IMAGE): $(ELFFILE) $(NMAPFILE) - @echo "===========================================================" - @echo "Create image3 (SDRAM, $(RAM3P_IMAGE))" -# @echo "===========================================================" - @mkdir -p $(BIN_DIR) - @rm -f $(RAM3_IMAGE) $(RAM3P_IMAGE) - @$(eval RAM3_START_ADDR = $(shell grep __sdram_data_ $(NMAPFILE) | grep _start__ | awk '{print $$1}')) - @$(eval RAM3_END_ADDR = $(shell grep __sdram_data_ $(NMAPFILE) | grep _end__ | awk '{print $$1}')) - $(if $(RAM3_START_ADDR),,$(error "Not found __sdram_data_start__!")) - $(if $(RAM3_END_ADDR),,$(error "Not found __sdram_data_end__!")) -#ifneq ($(RAM3_START_ADDR),$(RAM3_END_ADDR)) - @echo $(RAM3_START_ADDR) $(RAM3_END_ADDR) - @$(OBJCOPY) -j .image3 -j .sdr_text -j .sdr_rodata -j .sdr_data -Obinary $(ELFFILE) $(RAM3_IMAGE) - $(PICK) 0x$(RAM3_START_ADDR) 0x$(RAM3_END_ADDR) $(RAM3_IMAGE) $(RAM3P_IMAGE) body+reset_offset -#else -# @rm -f $(RAM3_IMAGE) $(RAM3P_IMAGE) -# @echo "SDRAM not used (size = 0)" -#endif - -$(ELFFILE): - $(error Falsher: file $@ not found) - clean: @rm -f $(BIN_DIR)/*.bin \ No newline at end of file diff --git a/project.mk b/project.mk index 967daf4..24d7e00 100644 --- a/project.mk +++ b/project.mk @@ -1,7 +1,7 @@ #============================================= # SDK CONFIG #============================================= -#USE_SDRAM = 1 +USE_SDRAM = 1 WEB_INA219_DRV = 1 #WEB_MLX90614_DRV = 1 #WEB_ADC_DRV = 1 @@ -29,6 +29,7 @@ endif RTOSDIR=freertos_v9.0.0 LWIPDIR=lwip_v1.4.1 + ifdef USE_UVC USE_SDRAM = 1 USE_GCC_LIB = 1 @@ -46,6 +47,7 @@ ADD_SRC_C += project/src/user/user_start.c # components DRAM_C += project/src/console/atcmd_user.c DRAM_C += project/src/console/wifi_console.c +#ADD_SRC_C += project/src/console/acl_tst.c #DRAM_C += project/src/console/wlan_tst.c #ADD_SRC_C += project/src/console/pwm_tst.c #ADD_SRC_C += project/src/WS2812/ws2812_tst.c diff --git a/project/inc/lwipopts.h b/project/inc/lwipopts.h index e5dd66a..b498296 100644 --- a/project/inc/lwipopts.h +++ b/project/inc/lwipopts.h @@ -27,6 +27,27 @@ #include #include "platform_opts.h" +//////////////////////////// + +#ifdef CONFIG_SDR_EN +#define WIFI_LOGO_CERTIFICATION_CONFIG 1 // for ping 10k test buffer setting +#else +#define WIFI_LOGO_CERTIFICATION_CONFIG 0 +#endif +/*LWIP_UART_ADAPTER==1: Enable LWIP_UART_ADAPTER when CONFIG_GAGENT is enabled, + because some GAGENT functions denpond on the following macro definitions.*/ +#define LWIP_UART_ADAPTER 0 + + +/** + * IP_FORWARD==1: Enables the ability to forward IP packets across network + * interfaces. If you are going to run lwIP on a device with only one network + * interface, define this to 0. + */ +#define IP_FORWARD 1 +#define IP_NAPT 1 + +//////////////////////////// /** * LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS==1: randomize the local port for the first * local TCP/UDP pcb (default==0). This can prevent creating predictable port @@ -34,20 +55,43 @@ */ #define LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS 1 -#define WIFI_LOGO_CERTIFICATION_CONFIG 1 //for ping 10k test buffer setting /** * MEM_LIBC_MALLOC==1: Use malloc/free/realloc provided by your C-library * instead of the lwip internal allocator. Can save code size if you * already use it. */ #define MEM_LIBC_MALLOC 1 + /** * MEMP_MEM_MALLOC==1: Use mem_malloc/mem_free instead of the lwip pool allocator. * Especially useful with MEM_LIBC_MALLOC but handle with care regarding execution * speed and usage from interrupts! */ #define MEMP_MEM_MALLOC 1 - + +/** + * LWIP_NETIF_HOSTNAME==1: use DHCP_OPTION_HOSTNAME with netif's hostname + * field. + */ +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETIF_HOSTNAME_SIZE 16 +/** + * netif0: DEF_HOSTNAME "0", netif1: DEF_HOSTNAME "1", .. + */ +#define DEF_HOSTNAME "rtl871x" + +/** + * LWIP_AUTOIP==1: Enable AUTOIP module. + */ +#define LWIP_AUTOIP 0 // Realtek modified (0->1) + +/** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided + * by your system, set this to 0 and include in cc.h */ +#if defined(_SYS__TIMEVAL_H_) +#define LWIP_TIMEVAL_PRIVATE 0 +#endif + +//////////////// /** * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain * critical regions during buffer allocation, deallocation and memory @@ -64,23 +108,16 @@ #define IP_FRAG 1 #define ARP_QUEUEING 0 -/** - * LWIP_NETIF_HOSTNAME==1: use DHCP_OPTION_HOSTNAME with netif's hostname - * field. - */ -#define LWIP_NETIF_HOSTNAME 1 -#define LWIP_NETIF_HOSTNAME_SIZE 16 -/** - * netif0: DEF_HOSTNAME "0", netif1: DEF_HOSTNAME "1", .. - */ -#define DEF_HOSTNAME "rtl871x" - /** * NO_SYS==1: Provides VERY minimal functionality. Otherwise, * use lwIP facilities. */ #define NO_SYS 0 +#ifndef CONFIG_DYNAMIC_TICKLESS +#define CONFIG_DYNAMIC_TICKLESS 0 +#endif + /* ---------- Memory options ---------- */ /* MEM_ALIGNMENT: should be set to the alignment of the CPU for which lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2 @@ -90,9 +127,9 @@ /* MEM_SIZE: the size of the heap memory. If the application will send a lot of data that needs to be copied, this should be set high. */ #if WIFI_LOGO_CERTIFICATION_CONFIG - #define MEM_SIZE (10*1024) //for ping 10k test + #define MEM_SIZE (16*1024) //for ping 10k test #else - #define MEM_SIZE (5*1024) + #define MEM_SIZE (6*1024) #endif /* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application @@ -119,7 +156,7 @@ a lot of data that needs to be copied, this should be set high. */ /* ---------- Pbuf options ---------- */ /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */ #if WIFI_LOGO_CERTIFICATION_CONFIG - #define PBUF_POOL_SIZE 30 //for ping 10k test + #define PBUF_POOL_SIZE 30 // for ping 10k test #else #define PBUF_POOL_SIZE 20 #endif @@ -155,58 +192,58 @@ a lot of data that needs to be copied, this should be set high. */ #define TCP_SND_QUEUELEN (4*TCP_SND_BUF/TCP_MSS) /* TCP receive window. */ -#define TCP_WND (4*TCP_MSS) // (2*TCP_MSS) - +#if WIFI_LOGO_CERTIFICATION_CONFIG +#define TCP_WND (4*TCP_MSS) +#else +#define TCP_WND (2*TCP_MSS) +#endif /* ---------- ICMP options ---------- */ -#define LWIP_ICMP 1 +#define LWIP_ICMP 1 /* ---------- ARP options ----------- */ -#define LWIP_ARP 1 -/** - * LWIP_AUTOIP==1: Enable AUTOIP module. - */ -#define LWIP_AUTOIP 0 //Realtek modified (0->1) +#define LWIP_ARP 1 /* ---------- DHCP options ---------- */ /* Define LWIP_DHCP to 1 if you want DHCP configuration of interfaces. DHCP is not implemented in lwIP 0.5.1, however, so turning this on does currently not work. */ -#define LWIP_DHCP 1 +#define LWIP_DHCP 1 /* ---------- UDP options ---------- */ -#define LWIP_UDP 1 -#define UDP_TTL 255 +#define LWIP_UDP 1 +#define UDP_TTL 255 /* ---------- DNS options ---------- */ -#define LWIP_DNS 1 +#define LWIP_DNS 1 /* ---------- UPNP options --------- */ #define LWIP_UPNP 0 /* Support Multicast */ -#define LWIP_IGMP 1 - -extern __attribute__ ((long_call)) unsigned int Rand(void); -#define LWIP_RAND() Rand() +#define LWIP_IGMP 1 +extern _LONG_CALL_ u32 Rand(void); +#define LWIP_RAND() Rand() // srand(sys_now()) /* Support TCP Keepalive */ #define LWIP_TCP_KEEPALIVE 1 -/*LWIP_UART_ADAPTER==1: Enable LWIP_UART_ADAPTER when CONFIG_GAGENT is enabled, - because some GAGENT functions denpond on the following macro definitions.*/ -#if CONFIG_EXAMPLE_UART_ADAPTER -#define LWIP_UART_ADAPTER 1 -#else -#define LWIP_UART_ADAPTER 0 -#endif -#if LWIP_UART_ADAPTER +#if LWIP_UART_ADAPTER || CONFIG_ETHERNET || CONFIG_EXAMPLE_UART_ATCMD || CONFIG_EXAMPLE_SPI_ATCMD + +/** + * LWIP_SO_SNDTIMEO==1: Enable send timeout for sockets/netconns and + * SO_SNDTIMEO processing. + */ #undef LWIP_SO_SNDTIMEO #define LWIP_SO_SNDTIMEO 1 #undef SO_REUSE #define SO_REUSE 1 +/** +* MEMP_NUM_NETCONN: the number of struct netconns. +* (only needed if you use the sequential API, like api_lib.c) +*/ #undef MEMP_NUM_NETCONN #define MEMP_NUM_NETCONN 10 @@ -218,15 +255,7 @@ extern __attribute__ ((long_call)) unsigned int Rand(void); #define TCP_KEEPCNT_DEFAULT 10U #endif -#if CONFIG_EXAMPLE_UART_ATCMD -#undef LWIP_SO_SNDTIMEO -#define LWIP_SO_SNDTIMEO 1 - -#undef SO_REUSE -#define SO_REUSE 1 - -#undef MEMP_NUM_NETCONN -#define MEMP_NUM_NETCONN 10 +#if CONFIG_EXAMPLE_UART_ATCMD || CONFIG_EXAMPLE_SPI_ATCMD #undef MEMP_NUM_TCP_PCB #define MEMP_NUM_TCP_PCB (MEMP_NUM_NETCONN) @@ -234,19 +263,22 @@ extern __attribute__ ((long_call)) unsigned int Rand(void); #undef MEMP_NUM_UDP_PCB #define MEMP_NUM_UDP_PCB (MEMP_NUM_NETCONN) -#undef TCP_WND -#define TCP_WND (4*TCP_MSS) - -#define TCP_KEEPIDLE_DEFAULT 10000UL -#define TCP_KEEPINTVL_DEFAULT 1000UL -#define TCP_KEEPCNT_DEFAULT 10U - #define ERRNO 1 + #endif -/* ---------- Statistics options ---------- */ -#define LWIP_STATS 0 -#define LWIP_PROVIDE_ERRNO 1 +/* + -------------------------------------- + --------- Statistics options --------- + -------------------------------------- + */ + +/* + * LWIP_STATS==1: Enable statistics collection in lwip_stats. + */ +#define LWIP_STATS 0 + +#define LWIP_PROVIDE_ERRNO 1 /* @@ -255,15 +287,6 @@ extern __attribute__ ((long_call)) unsigned int Rand(void); -------------------------------------- */ -/* -The STM32F2x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums by hardware: - - To use this feature let the following define uncommented. - - To disable it and process by CPU comment the the checksum. -*/ -//Do checksum by lwip - WLAN nic does not support Checksum offload -//#define CHECKSUM_BY_HARDWARE - - #ifdef CHECKSUM_BY_HARDWARE /* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing IP packets.*/ #define CHECKSUM_GEN_IP 0 @@ -311,7 +334,7 @@ The STM32F2x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums /** * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c) */ -#define LWIP_SOCKET 1 +#define LWIP_SOCKET 1 /* ----------------------------------- @@ -321,6 +344,7 @@ The STM32F2x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums #define LWIP_DEBUG 0 + /* --------------------------------- ---------- OS options ---------- @@ -376,11 +400,10 @@ The STM32F2x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums */ #define TCPIP_THREAD_PRIO (configMAX_PRIORITIES - 2) -/** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided - * by your system, set this to 0 and include in cc.h */ -#if defined(_SYS__TIMEVAL_H_) -#define LWIP_TIMEVAL_PRIVATE 0 -#endif +/* Added by Realtek */ +#ifndef DNS_IGNORE_REPLY_ERR +#define DNS_IGNORE_REPLY_ERR 1 +#endif /* DNS_IGNORE_REPLY_ERR */ #endif /* __LWIPOPTS_H__ */ diff --git a/project/src/console/atcmd_user.c b/project/src/console/atcmd_user.c index d743a9d..d00352d 100644 --- a/project/src/console/atcmd_user.c +++ b/project/src/console/atcmd_user.c @@ -377,6 +377,7 @@ LOCAL void fATSP(int argc, char *argv[]) }; printf("WakeLock Status %d\n", pmu_get_wakelock_status()); } + //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ MON_RAM_TAB_SECTION COMMAND_TABLE console_commands_at[] = { diff --git a/project/src/user/main.c b/project/src/user/main.c index ee2d4a3..2121def 100644 --- a/project/src/user/main.c +++ b/project/src/user/main.c @@ -74,7 +74,7 @@ int main(void) WDGStart(); #endif -#if (defined(CONFIG_CRYPTO_STARTUP) && (CONFIG_CRYPTO_STARTUP)) +#if 0 // (defined(CONFIG_CRYPTO_STARTUP) && (CONFIG_CRYPTO_STARTUP)) if(rtl_cryptoEngine_init() != 0 ) { DBG_8195A("Crypto engine init failed!\n"); } diff --git a/project/src/user/user_start.c b/project/src/user/user_start.c index 931f119..d08c0ed 100644 --- a/project/src/user/user_start.c +++ b/project/src/user/user_start.c @@ -11,6 +11,7 @@ #include "freertos_pmu.h" #include "task.h" #include "diag.h" +#include "hal_crypto.h" #include "netbios/netbios.h" #include "sntp/sntp.h" #include "user/sys_cfg.h" @@ -59,12 +60,11 @@ void sys_write_cfg(void) } extern void console_init(void); +extern void HalReInitPlatformTimer(void); void user_init_thrd(void) { - /* Read system config*/ - if(syscfg.cfg.b.pin_clear_cfg_enable && 0) { // user_test_clear_pin() wifi_cfg.load_flg = 0; @@ -82,6 +82,14 @@ void user_init_thrd(void) { /* Load cfg, init WiFi + LwIP init, WiFi start if wifi_cfg.mode != RTW_MODE_NONE */ wifi_init(); +#if (defined(CONFIG_CRYPTO_STARTUP) && (CONFIG_CRYPTO_STARTUP)) + if(rtl_cryptoEngine_init() != 0 ) { + error_printf("Crypto engine init failed!\n"); + } +#else +// rtl_cryptoEngine_deinit(); +#endif + #if defined(USE_NETBIOS) if(syscfg.cfg.b.netbios_ena) netbios_init(); #endif diff --git a/tools/rtlaimage/make_windows_exe.bat b/tools/rtlaimage/make_windows_exe.bat new file mode 100644 index 0000000..bd7540b --- /dev/null +++ b/tools/rtlaimage/make_windows_exe.bat @@ -0,0 +1,6 @@ +rem pip install wheel +rem pip install pyinstaller +pyinstaller -c --onedir --onefile -n rtlaimage rtlaimage.py +copy /b dist\rtlaimage.exe ..\..\USDK\component\soc\realtek\8195a\misc\iar_utility\common\tools\rtlaimage.exe +del /Q rtlaimage.spec +rm -rf dist build diff --git a/tools/rtlaimage/rtlaimage.py b/tools/rtlaimage/rtlaimage.py new file mode 100644 index 0000000..295d166 --- /dev/null +++ b/tools/rtlaimage/rtlaimage.py @@ -0,0 +1,345 @@ +#!/usr/bin/env python +# (RTL8195AM/RTL8711AM/RTL8711AF/RTL8710AF) RtlImages Utility +# pvvx + +from __future__ import print_function, division +from operator import attrgetter + +import argparse +import os +import struct +import sys + +__version__ = "17.01.18" + +PYTHON2 = sys.version_info[0] < 3 # True if on pre-Python 3 + +HM_IS_HDRR = 1 +HM_IS_HDRD = 2 +HM_IS_BOOT = 4 +HM_IS_OTA = 8 +HM_IS_SRAM = 16 +HM_IS_SDRAM = 32 + +# Function to return nth byte of a bitstring +# Different behaviour on Python 2 vs 3 +if PYTHON2: + def byte(bitstr, index): + return ord(bitstr[index]) +else: + def byte(bitstr, index): + return bitstr[index] + + +class FatalError(RuntimeError): + """ + Wrapper class for runtime errors that aren't caused by internal bugs, but by + xchip responses or input content. + """ + def __init__(self, message): + RuntimeError.__init__(self, message) + + @staticmethod + def WithResult(message, result): + """ + Return a fatal error object that appends the hex values of + 'result' as a string formatted argument. + """ + message += " (result was %s)" % hexify(result) + return FatalError(message) + +class ImageSegment(object): + """ Wrapper class for a segment in an RTL image + (very similar to a section in an ELFImage also) """ + def __init__(self, addr, data, file_offs = None): + self.addr = addr + # pad all ImageSegments to at least 4 bytes length + pad_mod = len(data) % 4 + if pad_mod != 0: + data += b"\x00" * (4 - pad_mod) + self.data = data + self.file_offs = file_offs + + def copy_with_new_addr(self, new_addr): + """ Return a new ImageSegment with same data, but mapped at + a new address. """ + return ImageSegment(new_addr, self.data, 0) + + def __repr__(self): + r = "len 0x%05x load 0x%08x" % (len(self.data), self.addr) + if self.file_offs is not None: + r += " file_offs 0x%08x" % (self.file_offs) + return r + +class ELFSection(ImageSegment): + """ Wrapper class for a section in an ELF image, has a section + name as well as the common properties of an ImageSegment. """ + def __init__(self, name, addr, data, size): + super(ELFSection, self).__init__(addr, data) + self.name = name.decode("utf-8") + self.size = size + + def __repr__(self): + return "%s %s" % (self.name, super(ELFSection, self).__repr__()) + +class ELFFile(object): + SEC_TYPE_PROGBITS = 0x01 + SEC_TYPE_STRTAB = 0x03 + + def __init__(self, name): + # Load sections from the ELF file + self.name = name + try: + with open(self.name, 'rb') as f: + self._read_elf_file(f) + except: + print('Error: Not open input ELF file <%s>!' % self.name) + sys.exit(-1) + + def get_section(self, section_name): + for s in self.sections: + if s.name == section_name: + return s + raise ValueError("No section %s in ELF file" % section_name) + + def _read_elf_file(self, f): + # read the ELF file header + LEN_FILE_HEADER = 0x34 + try: + (ident,_type,machine,_version, + self.entrypoint,_phoff,shoff,_flags, + _ehsize, _phentsize,_phnum,_shentsize, + _shnum,shstrndx) = struct.unpack("<16sHHLLLLLHHHHHH", f.read(LEN_FILE_HEADER)) + except struct.error as e: + raise FatalError("Failed to read a valid ELF header from %s: %s" % (self.name, e)) + + if byte(ident, 0) != 0x7f or ident[1:4] != b'ELF': + raise FatalError("%s has invalid ELF magic header" % self.name) + if machine != 0x28: + raise FatalError("%s does not appear to be an RTL ELF file. e_machine=%04x" % (self.name, machine)) + self._read_sections(f, shoff, shstrndx, _shnum) + + def _read_sections(self, f, section_header_offs, shstrndx, shnum): + LEN_SEC_HEADER = 0x28 + f.seek(section_header_offs) +# print("%d sections found in ELF file" % shnum) + if shnum == 0 or shnum > 100: + raise FatalError("%d sections found in ELF file!" % shnum) + section_header = f.read(shnum *LEN_SEC_HEADER) + if len(section_header) == 0: + raise FatalError("No section header found at offset %04x in ELF file." % section_header_offs) + if len(section_header) != shnum *LEN_SEC_HEADER: + print('WARNING: Unexpected ELF section header length %04x is not mod-%02x' % (len(section_header), LEN_SEC_HEADER)) + + # walk through the section header and extract all sections + section_header_offsets = range(0, len(section_header), LEN_SEC_HEADER) + + def read_section_header(offs): + name_offs,sec_type,_flags,lma,sec_offs,size = struct.unpack_from(" 1: + sorted(imgsegs, key = attrgetter('addr')) + elif len(imgsegs[0].data) == 0: # file size == 0 + self.addr = imgsegs[0].addr +# print('Segment at 0x%08x,' % self.addr, 'size 0x00000000 copy to image', self.fname) + sections.remove(imgsegs[0]) + return + self.addr = imgsegs[0].addr + s = imgsegs[len(imgsegs) - 1] + self.size = s.addr + len(s.data) - self.addr +# print('Segment at 0x%08x,' % self.addr, 'size 0x%08x' % self.size, 'copy to image', self.fname) + if self.addr < self.addrl and self.addr > self.addrh: + print('Segment Address Error!') + return + if self.size: + if self.hm & HM_IS_HDRR: + self.data = struct.pack(b'!' % self.fname) + sys.exit(-1) + + def save_ota(self, f, fn, chks): + if self.size: +# print('Segment at 0x%08x,' % self.addr, 'size 0x%08x' % self.size, 'save to %s' % fn) + try: + f.write(self.data) + i = 0 + while i < len(self.data): + chks += byte(self.data, i) + i += 1 + except: + print('Error: Not write file %s!' % fn) + sys.exit(-1) + return chks + + def save_sram(self, f, fn): + if self.size: +# print('Segment at 0x%08x,' % self.addr, 'size 0x%08x' % self.size, 'save to %s' % fn) + try: + if self.hm & HM_IS_BOOT: + gap = 0x0b000 + i = len(self.data) + if i > gap: + print('Error: Segment %s is big!' % self.name) + f.write(self.data) + while i < gap: + f.write('\0') + i += 1 + else: + f.write(self.data) + except: + print('Error: Not write file %s!' % fn) + sys.exit(-1) + +def elf2image(args): + + e = ELFFile(args.elffile) # ELFSection is a subclass of ImageSegment +# print(e.sections) + image = [xFirmwareImage('ram_1', ['.ram.start.table', '.ram_image1.text', '.ram_image1.data'], HM_IS_BOOT + HM_IS_SRAM, 0x10000bc8, 0x10070000), + xFirmwareImage('ram_2', ['.image2.start.table', '.ram_image2.text', '.ram_image2.rodata', '.ram.data'], HM_IS_OTA + HM_IS_HDRR + HM_IS_SRAM, 0x10006000, 0x10070000), + xFirmwareImage('sdram', ['.sdr_text', '.sdr_rodata', '.sdr_data'], HM_IS_OTA + HM_IS_HDRD + HM_IS_SDRAM, 0x30000000, 0x30200000)] + + img_sram_use = 0 + img_sdram_use = 0 + + for s in image: + s.load(e.sections) + if s.hm & HM_IS_SRAM: + img_sram_use += s.size + elif s.hm & HM_IS_SDRAM: + img_sdram_use += s.size + + for s in image: + if s.size: + print('Segment at 0x%08x,' % s.addr, 'size 0x%08x' % s.size, '(%s)' % s.fname) + s.save(args.outdir, None); + s.save(args.outdir, 1); + + if args.ota: + fn = args.outdir + 'ota.bin' + try: + chks = 0 + with open(fn, "wb") as f: + for s in image: + if s.hm & HM_IS_OTA: + chks = s.save_ota(f, fn, chks) + f.write(struct.pack('!' % fn) + sys.exit(-1) + + if args.ram_all: + if image[0].size == 0 and image[1].size == 0: + print('Error: Ram_all = 0!') + sys.exit(-1) + fn = args.outdir + 'ram_all.bin' + try: + with open(fn, "wb") as f: + for s in image: + if s.hm & HM_IS_SRAM: + s.save_sram(f, fn) + f.close() + + except: + print('Error: Not write file <%s>!' % fn) + sys.exit(-1) + + print('Images size: SRAM %u bytes, SDRAM %u bytes [%u]' % (img_sram_use, img_sdram_use, img_sram_use + img_sdram_use)) + + +def main(): + parser = argparse.ArgumentParser(description='RtlAImages Utility version %s' % __version__, prog='rtlimage') + parser.add_argument('--ram_all', '-r', action='store_true', help='Generate ram_all files') + parser.add_argument('--ota', '-a', action='store_true', help='Generate OTA files') + parser.add_argument('elffile', type=str, help='Input ELF file',) + parser.add_argument('--outdir', '-o', type=str, default='', help='Outpyt directory') + + args = parser.parse_args() + + print('RtlAImages Utility version %s' % __version__) + if args.elffile is None: + parser.print_help() + sys.exit(1) + elf2image(args) + sys.exit(0) + +if __name__ == '__main__': + try: + main() + except FatalError as e: + print('\nA fatal error occurred: %s' % e) + sys.exit(2) diff --git a/userset.mk b/userset.mk index 7a408f8..9fdc648 100644 --- a/userset.mk +++ b/userset.mk @@ -6,8 +6,8 @@ SDK_PATH = USDK/ #GCC_PATH = d:/MCU/GNU_Tools_ARM_Embedded/6.2017-q1-update/bin/# + or set in PATH #OPENOCD_PATH = D:/MCU/OpenOCD/bin/# + or set in PATH TOOLS_PATH ?= $(SDK_PATH)component/soc/realtek/8195a/misc/iar_utility/common/tools/ -FLASHER_TYPE = Jlink -#FLASHER_TYPE = cmsis-dap +#FLASHER_TYPE = Jlink +FLASHER_TYPE = cmsis-dap FLASHER_SPEED = 3500 FLASHER_PATH = flasher/ JLINK_PATH ?= D:/MCU/SEGGER/JLink_V612i/