Skip to content

Commit 27a6965

Browse files
authored
Merge pull request #4284 from mikaleppanen/lwip_randlib
LWIP random library and TCP sequence number improvements
2 parents 1705d10 + 55753be commit 27a6965

File tree

7 files changed

+383
-4
lines changed

7 files changed

+383
-4
lines changed

features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ EthernetInterface::EthernetInterface()
2727
nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
2828
{
2929
_dhcp = false;
30+
3031
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
32+
_ip_address[sizeof(_ip_address) - 1] = '\0';
3133
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
34+
_netmask[sizeof(_netmask) - 1] = '\0';
3235
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
36+
_gateway[sizeof(_gateway) - 1] = '\0';
37+
3338
return NSAPI_ERROR_OK;
3439
}
3540

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "lwip_random.h"
18+
19+
#if FEATURE_COMMON_PAL
20+
21+
#include "randLIB.h"
22+
23+
void lwip_seed_random(void)
24+
{
25+
randLIB_seed_random();
26+
}
27+
28+
void lwip_add_random_seed(uint64_t seed)
29+
{
30+
randLIB_add_seed(seed);
31+
}
32+
33+
inline uint32_t lwip_get_random(void)
34+
{
35+
return randLIB_get_32bit();
36+
}
37+
38+
#else
39+
40+
void lwip_seed_random(void)
41+
{
42+
}
43+
44+
void lwip_add_random_seed(uint64_t seed)
45+
{
46+
}
47+
48+
uint32_t lwip_get_random(void)
49+
{
50+
return rand();
51+
}
52+
53+
#endif
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef LWIP_HDR_RANDOM_H
18+
#define LWIP_HDR_RANDOM_H
19+
20+
#include "lwip/opt.h"
21+
#include "lwip/debug.h"
22+
#include "lwip/def.h"
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
/** Init seed for Pseudo Random
29+
*
30+
* Function shall seed the pseudo-random generator. Function
31+
* is called during lwip initialisation.
32+
*/
33+
void lwip_seed_random(void);
34+
35+
/** Update seed for pseudo-random generator
36+
*
37+
* Adds seed information to existing generator, to perturb the
38+
* sequence.
39+
*
40+
* @param seed 64 bits of data to add to the seed.
41+
*/
42+
void lwip_add_random_seed(uint64_t seed);
43+
44+
/** Generates random number.
45+
*
46+
* @return 32-bit random number
47+
*/
48+
uint32_t lwip_get_random(void);
49+
50+
#ifdef __cplusplus
51+
}
52+
#endif
53+
54+
#endif /* LWIP_HDR_RANDOM_H */
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/**
2+
* @file
3+
*
4+
* Reference implementation of the TCP ISN algorithm standardized in RFC 6528.
5+
* Produce TCP Initial Sequence Numbers by combining an MD5-generated hash
6+
* based on the new TCP connection's identity and a stable secret, with the
7+
* current time at 4-microsecond granularity.
8+
*
9+
* Specifically, the implementation uses MD5 to compute a hash of the input
10+
* buffer, which contains both the four-tuple of the new TCP connection (local
11+
* and remote IP address and port), as well as a 16-byte secret to make the
12+
* results unpredictable to external parties. The secret must be given at
13+
* initialization time and should ideally remain the same across system
14+
* reboots. To be sure: the spoofing-resistance of the resulting ISN depends
15+
* mainly on the strength of the supplied secret!
16+
*
17+
* The implementation takes 32 bits from the computed hash, and adds to it the
18+
* current time, in 4-microsecond units. The current time is computed from a
19+
* boot time given at initialization, and the current uptime as provided by
20+
* sys_now(). Thus, it assumes that sys_now() returns a time value that is
21+
* relative to the boot time, i.e., that it starts at 0 at system boot, and
22+
* only ever increases monotonically.
23+
*
24+
* For efficiency reasons, a single MD5 input buffer is used, and partially
25+
* filled in at initialization time. Specifically, of this 64-byte buffer, the
26+
* first 36 bytes are used for the four-way TCP tuple data, followed by the
27+
* 16-byte secret, followed by 12-byte zero padding. The 64-byte size of the
28+
* buffer should achieve the best performance for the actual MD5 computation.
29+
*
30+
* Basic usage:
31+
*
32+
* 1. in your lwipopts.h, add the following lines:
33+
*
34+
* #include <lwip/arch.h>
35+
* struct ip_addr;
36+
* u32_t lwip_hook_tcp_isn(const struct ip_addr *local_ip, u16_t local_port,
37+
* const struct ip_addr *remote_ip, u16_t remote_port);
38+
* "#define LWIP_HOOK_TCP_ISN lwip_hook_tcp_isn";
39+
*
40+
* 2. from your own code, call lwip_init_tcp_isn() at initialization time, with
41+
* appropriate parameters.
42+
*/
43+
44+
/*
45+
* Copyright (c) 2016 The MINIX 3 Project.
46+
* All rights reserved.
47+
*
48+
* Redistribution and use in source and binary forms, with or without modification,
49+
* are permitted provided that the following conditions are met:
50+
*
51+
* 1. Redistributions of source code must retain the above copyright notice,
52+
* this list of conditions and the following disclaimer.
53+
* 2. Redistributions in binary form must reproduce the above copyright notice,
54+
* this list of conditions and the following disclaimer in the documentation
55+
* and/or other materials provided with the distribution.
56+
* 3. The name of the author may not be used to endorse or promote products
57+
* derived from this software without specific prior written permission.
58+
*
59+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
60+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
62+
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
63+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
64+
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
67+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
68+
* OF SUCH DAMAGE.
69+
*
70+
* Author: David van Moolenbroek <[email protected]>
71+
*/
72+
73+
#include "lwip_tcp_isn.h"
74+
#include "lwip/ip_addr.h"
75+
#include "lwip/sys.h"
76+
#include <string.h>
77+
78+
/* pull in md5 of ppp? */
79+
#define PPP_SUPPORT 1
80+
#include "netif/ppp/ppp_opts.h"
81+
#include "netif/ppp/ppp.h"
82+
#include "netif/ppp/pppcrypt.h"
83+
#if !LWIP_USE_EXTERNAL_POLARSSL && !LWIP_USE_EXTERNAL_MBEDTLS
84+
#undef LWIP_INCLUDED_POLARSSL_MD5
85+
#define LWIP_INCLUDED_POLARSSL_MD5 1
86+
#include "netif/ppp/polarssl/lwip_md5.c"
87+
#endif
88+
#if LWIP_USE_EXTERNAL_MBEDTLS
89+
#include "mbedtls/inc/mbedtls/md5.h"
90+
#define md5_context mbedtls_md5_context
91+
#endif
92+
93+
static u8_t input[64];
94+
static u32_t base_time;
95+
96+
/**
97+
* Initialize the TCP ISN module, with the boot time and a secret.
98+
*
99+
* @param boot_time Wall clock boot time of the system, in seconds.
100+
* @param secret_16_bytes A 16-byte secret used to randomize the TCP ISNs.
101+
*/
102+
void
103+
lwip_init_tcp_isn(u32_t boot_time, const u8_t *secret_16_bytes)
104+
{
105+
/* Initialize the input buffer with the secret and trailing zeroes. */
106+
memset(input, 0, sizeof(input));
107+
108+
MEMCPY(&input[36], secret_16_bytes, 16);
109+
110+
/* Save the boot time in 4-us units. Overflow is no problem here. */
111+
base_time = boot_time * 250000;
112+
}
113+
114+
/**
115+
* Hook to generate an Initial Sequence Number (ISN) for a new TCP connection.
116+
*
117+
* @param local_ip The local IP address.
118+
* @param local_port The local port number, in host-byte order.
119+
* @param remote_ip The remote IP address.
120+
* @param remote_port The remote port number, in host-byte order.
121+
* @return The ISN to use for the new TCP connection.
122+
*/
123+
u32_t
124+
lwip_hook_tcp_isn(const ip_addr_t *local_ip, u16_t local_port,
125+
const ip_addr_t *remote_ip, u16_t remote_port)
126+
{
127+
md5_context ctx;
128+
u8_t output[16];
129+
u32_t isn;
130+
131+
#if LWIP_IPV4 && LWIP_IPV6
132+
if (IP_IS_V6(local_ip))
133+
#endif /* LWIP_IPV4 && LWIP_IPV6 */
134+
#if LWIP_IPV6
135+
{
136+
const ip6_addr_t *local_ip6, *remote_ip6;
137+
138+
local_ip6 = ip_2_ip6(local_ip);
139+
remote_ip6 = ip_2_ip6(remote_ip);
140+
141+
SMEMCPY(&input[0], &local_ip6->addr, 16);
142+
SMEMCPY(&input[16], &remote_ip6->addr, 16);
143+
}
144+
#endif /* LWIP_IPV6 */
145+
#if LWIP_IPV4 && LWIP_IPV6
146+
else
147+
#endif /* LWIP_IPV4 && LWIP_IPV6 */
148+
#if LWIP_IPV4
149+
{
150+
const ip4_addr_t *local_ip4, *remote_ip4;
151+
152+
local_ip4 = ip_2_ip4(local_ip);
153+
remote_ip4 = ip_2_ip4(remote_ip);
154+
155+
/* Represent IPv4 addresses as IPv4-mapped IPv6 addresses, to ensure that
156+
* the IPv4 and IPv6 address spaces are completely disjoint. */
157+
memset(&input[0], 0, 10);
158+
input[10] = 0xff;
159+
input[11] = 0xff;
160+
SMEMCPY(&input[12], &local_ip4->addr, 4);
161+
memset(&input[16], 0, 10);
162+
input[26] = 0xff;
163+
input[27] = 0xff;
164+
SMEMCPY(&input[28], &remote_ip4->addr, 4);
165+
}
166+
#endif /* LWIP_IPV4 */
167+
168+
input[32] = local_port >> 8;
169+
input[33] = local_port & 0xff;
170+
input[34] = remote_port >> 8;
171+
input[35] = remote_port & 0xff;
172+
173+
/* The secret and padding are already filled in. */
174+
175+
/* Generate the hash, using MD5. */
176+
lwip_md5_starts(&ctx);
177+
lwip_md5_update(&ctx, input, sizeof(input));
178+
lwip_md5_finish(&ctx, output);
179+
180+
/* Arbitrarily take the first 32 bits from the generated hash. */
181+
MEMCPY(&isn, output, sizeof(isn));
182+
183+
/* Add the current time in 4-microsecond units. */
184+
return isn + base_time + sys_now() * 250;
185+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2016 The MINIX 3 Project.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* 3. The name of the author may not be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19+
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21+
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25+
* OF SUCH DAMAGE.
26+
*
27+
* Author: David van Moolenbroek <[email protected]>
28+
*/
29+
30+
#ifndef LWIP_HDR_CONTRIB_ADDONS_TCP_ISN_H
31+
#define LWIP_HDR_CONTRIB_ADDONS_TCP_ISN_H
32+
33+
#include "lwip/opt.h"
34+
#include "lwip/ip_addr.h"
35+
36+
#ifdef __cplusplus
37+
extern "C" {
38+
#endif
39+
40+
void lwip_init_tcp_isn(u32_t boot_time, const u8_t *secret_16_bytes);
41+
u32_t lwip_hook_tcp_isn(const ip_addr_t *local_ip, u16_t local_port,
42+
const ip_addr_t *remote_ip, u16_t remote_port);
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif /* LWIP_HDR_CONTRIB_ADDONS_TCP_ISN_H */

0 commit comments

Comments
 (0)