Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
lwip_tcp_isn.c
Go to the documentation of this file.
00001 /** 00002 * @file 00003 * 00004 * Reference implementation of the TCP ISN algorithm standardized in RFC 6528. 00005 * Produce TCP Initial Sequence Numbers by combining an MD5-generated hash 00006 * based on the new TCP connection's identity and a stable secret, with the 00007 * current time at 4-microsecond granularity. 00008 * 00009 * Specifically, the implementation uses MD5 to compute a hash of the input 00010 * buffer, which contains both the four-tuple of the new TCP connection (local 00011 * and remote IP address and port), as well as a 16-byte secret to make the 00012 * results unpredictable to external parties. The secret must be given at 00013 * initialization time and should ideally remain the same across system 00014 * reboots. To be sure: the spoofing-resistance of the resulting ISN depends 00015 * mainly on the strength of the supplied secret! 00016 * 00017 * The implementation takes 32 bits from the computed hash, and adds to it the 00018 * current time, in 4-microsecond units. The current time is computed from a 00019 * boot time given at initialization, and the current uptime as provided by 00020 * sys_now(). Thus, it assumes that sys_now() returns a time value that is 00021 * relative to the boot time, i.e., that it starts at 0 at system boot, and 00022 * only ever increases monotonically. 00023 * 00024 * For efficiency reasons, a single MD5 input buffer is used, and partially 00025 * filled in at initialization time. Specifically, of this 64-byte buffer, the 00026 * first 36 bytes are used for the four-way TCP tuple data, followed by the 00027 * 16-byte secret, followed by 12-byte zero padding. The 64-byte size of the 00028 * buffer should achieve the best performance for the actual MD5 computation. 00029 * 00030 * Basic usage: 00031 * 00032 * 1. in your lwipopts.h, add the following lines: 00033 * 00034 * #include <lwip/arch.h> 00035 * struct ip_addr; 00036 * u32_t lwip_hook_tcp_isn(const struct ip_addr *local_ip, u16_t local_port, 00037 * const struct ip_addr *remote_ip, u16_t remote_port); 00038 * "#define LWIP_HOOK_TCP_ISN lwip_hook_tcp_isn"; 00039 * 00040 * 2. from your own code, call lwip_init_tcp_isn() at initialization time, with 00041 * appropriate parameters. 00042 */ 00043 00044 /* 00045 * Copyright (c) 2016 The MINIX 3 Project. 00046 * All rights reserved. 00047 * 00048 * Redistribution and use in source and binary forms, with or without modification, 00049 * are permitted provided that the following conditions are met: 00050 * 00051 * 1. Redistributions of source code must retain the above copyright notice, 00052 * this list of conditions and the following disclaimer. 00053 * 2. Redistributions in binary form must reproduce the above copyright notice, 00054 * this list of conditions and the following disclaimer in the documentation 00055 * and/or other materials provided with the distribution. 00056 * 3. The name of the author may not be used to endorse or promote products 00057 * derived from this software without specific prior written permission. 00058 * 00059 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00060 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00061 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00062 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00063 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00064 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00065 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00066 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00067 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00068 * OF SUCH DAMAGE. 00069 * 00070 * Author: David van Moolenbroek <david@minix3.org> 00071 */ 00072 00073 #include "lwip/opt.h" 00074 #include "lwip/ip_addr.h" 00075 #include "lwip/sys.h" 00076 #include <string.h> 00077 00078 /* pull in md5 of ppp? */ 00079 #if !PPP_SUPPORT 00080 #undef PPP_SUPPORT 00081 #define PPP_SUPPORT 1 00082 #define PPP_FAKED_ON 1 00083 #endif 00084 00085 #include "netif/ppp/ppp_opts.h" 00086 #include "netif/ppp/ppp.h" 00087 #include "netif/ppp/pppcrypt.h" 00088 00089 #if PPP_FAKED_ON && !LWIP_USE_EXTERNAL_POLARSSL && !LWIP_USE_EXTERNAL_MBEDTLS 00090 #undef LWIP_INCLUDED_POLARSSL_MD5 00091 #define LWIP_INCLUDED_POLARSSL_MD5 1 00092 #include "netif/ppp/polarssl/lwip_md5.c" 00093 #endif 00094 00095 static u8_t input[64]; 00096 static u32_t base_time; 00097 00098 /** 00099 * Initialize the TCP ISN module, with the boot time and a secret. 00100 * 00101 * @param boot_time Wall clock boot time of the system, in seconds. 00102 * @param secret_16_bytes A 16-byte secret used to randomize the TCP ISNs. 00103 */ 00104 void 00105 lwip_init_tcp_isn(u32_t boot_time, const u8_t *secret_16_bytes) 00106 { 00107 /* Initialize the input buffer with the secret and trailing zeroes. */ 00108 memset(input, 0, sizeof(input)); 00109 00110 MEMCPY(&input[36], secret_16_bytes, 16); 00111 00112 /* Save the boot time in 4-us units. Overflow is no problem here. */ 00113 base_time = boot_time * 250000; 00114 } 00115 00116 /** 00117 * Hook to generate an Initial Sequence Number (ISN) for a new TCP connection. 00118 * 00119 * @param local_ip The local IP address. 00120 * @param local_port The local port number, in host-byte order. 00121 * @param remote_ip The remote IP address. 00122 * @param remote_port The remote port number, in host-byte order. 00123 * @return The ISN to use for the new TCP connection. 00124 */ 00125 00126 u32_t 00127 lwip_hook_tcp_isn(const void *local_ip_ptr, u16_t local_port, 00128 const void *remote_ip_ptr, u16_t remote_port) 00129 { 00130 lwip_md5_context ctx; 00131 u8_t output[16]; 00132 u32_t isn; 00133 const ip_addr_t *local_ip = local_ip_ptr; 00134 const ip_addr_t *remote_ip = remote_ip_ptr; 00135 00136 #if LWIP_IPV4 && LWIP_IPV6 00137 if (IP_IS_V6(local_ip)) 00138 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00139 #if LWIP_IPV6 00140 { 00141 const ip6_addr_t *local_ip6, *remote_ip6; 00142 00143 local_ip6 = ip_2_ip6(local_ip); 00144 remote_ip6 = ip_2_ip6(remote_ip); 00145 00146 SMEMCPY(&input[0], &local_ip6->addr, 16); 00147 SMEMCPY(&input[16], &remote_ip6->addr, 16); 00148 } 00149 #endif /* LWIP_IPV6 */ 00150 #if LWIP_IPV4 && LWIP_IPV6 00151 else 00152 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00153 #if LWIP_IPV4 00154 { 00155 const ip4_addr_t *local_ip4, *remote_ip4; 00156 00157 local_ip4 = ip_2_ip4(local_ip); 00158 remote_ip4 = ip_2_ip4(remote_ip); 00159 00160 /* Represent IPv4 addresses as IPv4-mapped IPv6 addresses, to ensure that 00161 * the IPv4 and IPv6 address spaces are completely disjoint. */ 00162 memset(&input[0], 0, 10); 00163 input[10] = 0xff; 00164 input[11] = 0xff; 00165 SMEMCPY(&input[12], &local_ip4->addr, 4); 00166 memset(&input[16], 0, 10); 00167 input[26] = 0xff; 00168 input[27] = 0xff; 00169 SMEMCPY(&input[28], &remote_ip4->addr, 4); 00170 } 00171 #endif /* LWIP_IPV4 */ 00172 00173 input[32] = local_port >> 8; 00174 input[33] = local_port & 0xff; 00175 input[34] = remote_port >> 8; 00176 input[35] = remote_port & 0xff; 00177 00178 /* The secret and padding are already filled in. */ 00179 00180 /* Generate the hash, using MD5. */ 00181 lwip_md5_starts(&ctx); 00182 lwip_md5_update(&ctx, input, sizeof(input)); 00183 lwip_md5_finish(&ctx, output); 00184 00185 /* Arbitrarily take the first 32 bits from the generated hash. */ 00186 MEMCPY(&isn, output, sizeof(isn)); 00187 00188 /* Add the current time in 4-microsecond units. */ 00189 return isn + base_time + sys_now() * 250; 00190 }
Generated on Sun Jul 17 2022 08:25:25 by 1.7.2