blob: f91d42f1d419dee8751bd834249f10de522d15d0 [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
Manuel Pégourié-Gonnard53ebe132015-05-15 11:56:13 +02004 * Copyright (C) 2014-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02005 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020027#endif
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020036#include <stdio.h>
37int main( void )
38{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020040 return( 0 );
41}
42#else
43
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/net.h"
45#include "mbedtls/error.h"
46#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020047
48#include <stdio.h>
49#include <stdlib.h>
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000050#include <string.h>
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020051#include <time.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020052
53/* For select() */
54#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
55 !defined(EFI32)
56#include <winsock2.h>
57#include <windows.h>
58#if defined(_MSC_VER)
59#if defined(_WIN32_WCE)
60#pragma comment( lib, "ws2.lib" )
61#else
62#pragma comment( lib, "ws2_32.lib" )
63#endif
64#endif /* _MSC_VER */
65#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
66#include <sys/time.h>
67#include <sys/types.h>
68#include <unistd.h>
69#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
70
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +020071/* For gettimeofday() */
72#if !defined(_WIN32)
73#include <sys/time.h>
74#endif
75
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020076#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020077
78#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020079#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020080#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020081#define DFL_LISTEN_PORT "5556"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020082
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020083#define USAGE \
84 "\n usage: udp_proxy param=<>...\n" \
85 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020086 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020087 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020088 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020090 "\n" \
91 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020092 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020093 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020094 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020095 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +020096 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020097 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +020098 " mtu=%%d default: 0 (unlimited)\n" \
99 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200100 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
101 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200102 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200103 "\n" \
104 " seed=%%d default: (use current time)\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200105 "\n"
106
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200107/*
108 * global options
109 */
110static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200111{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200112 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200113 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200114 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200115 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200116
117 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200118 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200119 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200120 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200121 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200122 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200123 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200124 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200125
126 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200127} opt;
128
129static void exit_usage( const char *name, const char *value )
130{
131 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200133 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200137 exit( 1 );
138}
139
140static void get_options( int argc, char *argv[] )
141{
142 int i;
143 char *p, *q;
144
145 opt.server_addr = DFL_SERVER_ADDR;
146 opt.server_port = DFL_SERVER_PORT;
147 opt.listen_addr = DFL_LISTEN_ADDR;
148 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200149 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200150
151 for( i = 1; i < argc; i++ )
152 {
153 p = argv[i];
154 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200155 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200156 *q++ = '\0';
157
158 if( strcmp( p, "server_addr" ) == 0 )
159 opt.server_addr = q;
160 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200161 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200162 else if( strcmp( p, "listen_addr" ) == 0 )
163 opt.listen_addr = q;
164 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200165 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200166 else if( strcmp( p, "duplicate" ) == 0 )
167 {
168 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200169 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200170 exit_usage( p, q );
171 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200172 else if( strcmp( p, "delay" ) == 0 )
173 {
174 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200175 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200176 exit_usage( p, q );
177 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200178 else if( strcmp( p, "delay_ccs" ) == 0 )
179 {
180 opt.delay_ccs = atoi( q );
181 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
182 exit_usage( p, q );
183 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200184 else if( strcmp( p, "drop" ) == 0 )
185 {
186 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200187 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200188 exit_usage( p, q );
189 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200190 else if( strcmp( p, "mtu" ) == 0 )
191 {
192 opt.mtu = atoi( q );
193 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
194 exit_usage( p, q );
195 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200196 else if( strcmp( p, "bad_ad" ) == 0 )
197 {
198 opt.bad_ad = atoi( q );
199 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
200 exit_usage( p, q );
201 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200202 else if( strcmp( p, "protect_hvr" ) == 0 )
203 {
204 opt.protect_hvr = atoi( q );
205 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
206 exit_usage( p, q );
207 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200208 else if( strcmp( p, "protect_len" ) == 0 )
209 {
210 opt.protect_len = atoi( q );
211 if( opt.protect_len < 0 )
212 exit_usage( p, q );
213 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200214 else if( strcmp( p, "seed" ) == 0 )
215 {
216 opt.seed = atoi( q );
217 if( opt.seed == 0 )
218 exit_usage( p, q );
219 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200220 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200221 exit_usage( p, NULL );
222 }
223}
224
225static const char *msg_type( unsigned char *msg, size_t len )
226{
227 if( len < 1 ) return( "Invalid" );
228 switch( msg[0] )
229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
231 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
232 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
233 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200234 default: return( "Unknown" );
235 }
236
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200237 if( len < 13 + 12 ) return( "Invalid handshake" );
238
239 /*
240 * Our handshake message are less than 2^16 bytes long, so they should
241 * have 0 as the first byte of length, frag_offset and frag_length.
242 * Otherwise, assume they are encrypted.
243 */
244 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
245
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200246 switch( msg[13] )
247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
249 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
250 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
251 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
252 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
253 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
254 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
255 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
256 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
257 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
258 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
259 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200260 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200261 }
262}
263
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200264/* Return elapsed time in milliseconds since the first call */
265static unsigned long ellapsed_time( void )
266{
267#if defined(_WIN32)
268 return( 0 );
269#else
270 static struct timeval ref = { 0, 0 };
271 struct timeval now;
272
273 if( ref.tv_sec == 0 && ref.tv_usec == 0 )
274 {
275 gettimeofday( &ref, NULL );
276 return( 0 );
277 }
278
279 gettimeofday( &now, NULL );
280 return( 1000 * ( now.tv_sec - ref.tv_sec )
281 + ( now.tv_usec - ref.tv_usec ) / 1000 );
282#endif
283}
284
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200285typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200286{
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200287 int dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200288 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200289 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200290 unsigned len;
291 unsigned char buf[MAX_MSG_SIZE];
292} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200293
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200294/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
295void print_packet( const packet *p, const char *why )
296{
297 if( why == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( " %05lu %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200299 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_printf( " %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200302 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200303 fflush( stdout );
304}
305
306int send_packet( const packet *p, const char *why )
307{
308 int ret;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200309 int dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200310
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200311 /* insert corrupted ApplicationData record? */
312 if( opt.bad_ad &&
313 strcmp( p->type, "ApplicationData" ) == 0 )
314 {
315 unsigned char buf[MAX_MSG_SIZE];
316 memcpy( buf, p->buf, p->len );
317 ++buf[p->len - 1];
318
319 print_packet( p, "corrupted" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_net_send( &dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200323 return( ret );
324 }
325 }
326
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200327 print_packet( p, why );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ( ret = mbedtls_net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200331 return( ret );
332 }
333
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200334 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200335 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200336 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200337 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200338 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200339 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( ret = mbedtls_net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200344 return( ret );
345 }
346 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200347
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200348 return( 0 );
349}
350
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200351static packet prev;
352
353void clear_pending( void )
354{
355 memset( &prev, 0, sizeof( packet ) );
356}
357
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200358/*
359 * Avoid dropping or delaying a packet that was already dropped twice: this
360 * only results in uninteresting timeouts. We can't rely on type to identify
361 * packets, since during renegotiation they're all encrypted. So, rely on
362 * size mod 2048 (which is usually just size).
363 */
364static unsigned char dropped[2048] = { 0 };
365#define DROP_MAX 2
366
367/*
368 * OpenSSL groups packets in a datagram the first time it sends them, but not
369 * when it resends them. Count every record as seen the first time.
370 */
371void update_dropped( const packet *p )
372{
373 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200374 const unsigned char *end = p->buf + p->len;
375 const unsigned char *cur = p->buf;
376 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
377
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200378 ++dropped[id];
379
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200380 /* Avoid counting single record twice */
381 if( len == p->len )
382 return;
383
384 while( cur < end )
385 {
386 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
387
388 id = len % sizeof( dropped );
389 ++dropped[id];
390
391 cur += len;
392 }
393}
394
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200395int handle_message( const char *way, int dst, int src )
396{
397 int ret;
398 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200399 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200400
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200401 /* receive packet */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ( ret = mbedtls_net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200405 return( ret );
406 }
407
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200408 cur.len = ret;
409 cur.type = msg_type( cur.buf, cur.len );
410 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200411 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200412 print_packet( &cur, NULL );
413
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200414 id = cur.len % sizeof( dropped );
415
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200416 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200417 if( ( opt.mtu != 0 &&
418 cur.len > (unsigned) opt.mtu ) ||
419 ( opt.drop != 0 &&
420 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200421 ! ( opt.protect_hvr &&
422 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200423 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200424 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200425 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200426 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200427 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200428 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200429 else if( ( opt.delay_ccs == 1 &&
430 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
431 ( opt.delay != 0 &&
432 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200433 ! ( opt.protect_hvr &&
434 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200435 prev.dst == 0 &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200436 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200437 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200438 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200439 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200440 memcpy( &prev, &cur, sizeof( packet ) );
441 }
442 else
443 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200444 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200445 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
446 return( ret );
447
448 /* send previously delayed message if any */
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200449 if( prev.dst != 0 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200450 {
451 ret = send_packet( &prev, "delayed" );
452 memset( &prev, 0, sizeof( packet ) );
453 if( ret != 0 )
454 return( ret );
455 }
456 }
457
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200458 return( 0 );
459}
460
461int main( int argc, char *argv[] )
462{
463 int ret;
464
465 int listen_fd = -1;
466 int client_fd = -1;
467 int server_fd = -1;
468
469 int nb_fds;
470 fd_set read_fds;
471
472 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200473
474 /*
475 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
476 * exactly 1 in N packets would lead to problems when a flight has exactly
477 * N packets: the same packet would be dropped on every resend.
478 *
479 * In order to be able to reproduce problems reliably, the seed may be
480 * specified explicitly.
481 */
482 if( opt.seed == 0 )
483 {
484 opt.seed = time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200486 }
487
488 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200489
490 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200491 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200492 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200493 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200494 opt.server_addr, opt.server_port );
495 fflush( stdout );
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
498 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200501 goto exit;
502 }
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200505
506 /*
507 * 1. Setup the "listening" UDP socket
508 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200509 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200510 opt.listen_addr, opt.listen_port );
511 fflush( stdout );
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
514 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200517 goto exit;
518 }
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200521
522 /*
523 * 2. Wait until a client connects
524 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200525accept:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200527 fflush( stdout );
528
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200529 if( ( ret = mbedtls_net_accept( listen_fd, &client_fd,
530 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200533 goto exit;
534 }
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200537 fflush( stdout );
538
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200539 mbedtls_printf( " . Re-bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200540 opt.listen_addr, opt.listen_port );
541 fflush( stdout );
542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
544 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200547 goto exit;
548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200551
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200552 /*
553 * 3. Forward packets forever (kill the process to terminate it)
554 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200555 clear_pending();
556 memset( dropped, 0, sizeof( dropped ) );
557
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200558 nb_fds = client_fd;
559 if( nb_fds < server_fd )
560 nb_fds = server_fd;
561 if( nb_fds < listen_fd )
562 nb_fds = listen_fd;
563 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200564
565 while( 1 )
566 {
567 FD_ZERO( &read_fds );
568 FD_SET( server_fd, &read_fds );
569 FD_SET( client_fd, &read_fds );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200570 FD_SET( listen_fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200571
572 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
573 {
574 perror( "select" );
575 goto exit;
576 }
577
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200578 if( FD_ISSET( listen_fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200579 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200580
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200581 if( FD_ISSET( client_fd, &read_fds ) )
582 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200583 if( ( ret = handle_message( "S <- C",
584 server_fd, client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200585 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200586 }
587
588 if( FD_ISSET( server_fd, &read_fds ) )
589 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200590 if( ( ret = handle_message( "S -> C",
591 client_fd, server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200592 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200593 }
594 }
595
596exit:
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200599 if( ret != 0 )
600 {
601 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_strerror( ret, error_buf, 100 );
603 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200604 fflush( stdout );
605 }
606#endif
607
608 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200610
611 if( listen_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_net_close( listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200613
614#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200616 fflush( stdout ); getchar();
617#endif
618
619 return( ret != 0 );
620}
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#endif /* MBEDTLS_NET_C */