2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Test protocol robustness by simulating dropped packets and
27 * network outages when the --gremlin option is used.
31 #include "config-win32.h"
49 * Parameters for packet corruption and droppage.
50 * Each parameter has 4 possible levels, 0 = disabled,
51 * while 1, 2, and 3 are enumerated in the below arrays.
52 * The parameter is a 2-bit field within the --gremlin
57 * Probability that we will drop a packet is 1 / n
59 static const int drop_freq
[] = { 500, 100, 50 };
62 * Probability that we will corrupt a packet is 1 / n
64 static const int corrupt_freq
[] = { 500, 100, 50 };
67 * When network goes up, it will be up for between
68 * UP_LOW and UP_HIGH seconds.
70 static const int up_low
[] = { 60, 10, 5 };
71 static const int up_high
[] = { 600, 60, 10 };
74 * When network goes down, it will be down for between
75 * DOWN_LOW and DOWN_HIGH seconds.
77 static const int down_low
[] = { 5, 10, 10 };
78 static const int down_high
[] = { 10, 60, 120 };
81 * Packet flood levels:
82 * { number of packets, packet size }
84 static const struct packet_flood_parms packet_flood_data
[] =
85 {{10, 100}, {10, 1500}, {100, 1500}};
87 struct packet_flood_parms
88 get_packet_flood_parms (int level
)
90 ASSERT (level
> 0 && level
< 4);
91 return packet_flood_data
[level
- 1];
95 * Return true with probability 1/n
97 static bool flip(int n
) {
98 return (get_random() % n
) == 0;
102 * Return uniformly distributed random number between
105 static int roll(int low
, int high
) {
107 ASSERT (low
<= high
);
108 ret
= low
+ (get_random() % (high
- low
+ 1));
109 ASSERT (ret
>= low
&& ret
<= high
);
113 static bool initialized
; /* GLOBAL */
114 static bool up
; /* GLOBAL */
115 static time_t next
; /* GLOBAL */
118 * Return false if we should drop a packet.
121 ask_gremlin (int flags
)
123 const int up_down_level
= GREMLIN_UP_DOWN_LEVEL (flags
);
124 const int drop_level
= GREMLIN_DROP_LEVEL (flags
);
138 if (up_down_level
) /* change up/down state? */
145 delta
= roll (down_low
[up_down_level
-1], down_high
[up_down_level
-1]);
150 delta
= roll (up_low
[up_down_level
-1], up_high
[up_down_level
-1]);
155 "GREMLIN: CONNECTION GOING %s FOR %d SECONDS",
156 (up
? "UP" : "DOWN"),
164 if (up
&& flip (drop_freq
[drop_level
-1]))
166 dmsg (D_GREMLIN_VERBOSE
, "GREMLIN: Random packet drop");
175 * Possibly corrupt a packet.
177 void corrupt_gremlin (struct buffer
*buf
, int flags
) {
178 const int corrupt_level
= GREMLIN_CORRUPT_LEVEL (flags
);
181 if (flip (corrupt_freq
[corrupt_level
-1]))
187 uint8_t r
= roll (0, 255);
188 int method
= roll (0, 5);
191 case 0: /* corrupt the first byte */
194 case 1: /* corrupt the last byte */
195 *(BPTR (buf
) + buf
->len
- 1) = r
;
197 case 2: /* corrupt a random byte */
198 *(BPTR(buf
) + roll (0, buf
->len
- 1)) = r
;
200 case 3: /* append a random byte */
201 buf_write (buf
, &r
, 1);
203 case 4: /* reduce length by 1 */
206 case 5: /* reduce length by a random amount */
207 buf
->len
-= roll (0, buf
->len
- 1);
210 dmsg (D_GREMLIN_VERBOSE
, "GREMLIN: Packet Corruption, method=%d", method
);
214 } while (flip (2)); /* a 50% chance we will corrupt again */
220 static void dummy(void) {}