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-2009 OpenVPN Technologies, Inc. <sales@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.
43 * Parameters for packet corruption and droppage.
44 * Each parameter has 4 possible levels, 0 = disabled,
45 * while 1, 2, and 3 are enumerated in the below arrays.
46 * The parameter is a 2-bit field within the --gremlin
51 * Probability that we will drop a packet is 1 / n
53 static const int drop_freq
[] = { 500, 100, 50 };
56 * Probability that we will corrupt a packet is 1 / n
58 static const int corrupt_freq
[] = { 500, 100, 50 };
61 * When network goes up, it will be up for between
62 * UP_LOW and UP_HIGH seconds.
64 static const int up_low
[] = { 60, 10, 5 };
65 static const int up_high
[] = { 600, 60, 10 };
68 * When network goes down, it will be down for between
69 * DOWN_LOW and DOWN_HIGH seconds.
71 static const int down_low
[] = { 5, 10, 10 };
72 static const int down_high
[] = { 10, 60, 120 };
75 * Packet flood levels:
76 * { number of packets, packet size }
78 static const struct packet_flood_parms packet_flood_data
[] =
79 {{10, 100}, {10, 1500}, {100, 1500}};
81 struct packet_flood_parms
82 get_packet_flood_parms (int level
)
84 ASSERT (level
> 0 && level
< 4);
85 return packet_flood_data
[level
- 1];
89 * Return true with probability 1/n
91 static bool flip(int n
) {
92 return (get_random() % n
) == 0;
96 * Return uniformly distributed random number between
99 static int roll(int low
, int high
) {
101 ASSERT (low
<= high
);
102 ret
= low
+ (get_random() % (high
- low
+ 1));
103 ASSERT (ret
>= low
&& ret
<= high
);
107 static bool initialized
; /* GLOBAL */
108 static bool up
; /* GLOBAL */
109 static time_t next
; /* GLOBAL */
112 * Return false if we should drop a packet.
115 ask_gremlin (int flags
)
117 const int up_down_level
= GREMLIN_UP_DOWN_LEVEL (flags
);
118 const int drop_level
= GREMLIN_DROP_LEVEL (flags
);
132 if (up_down_level
) /* change up/down state? */
139 delta
= roll (down_low
[up_down_level
-1], down_high
[up_down_level
-1]);
144 delta
= roll (up_low
[up_down_level
-1], up_high
[up_down_level
-1]);
149 "GREMLIN: CONNECTION GOING %s FOR %d SECONDS",
150 (up
? "UP" : "DOWN"),
158 if (up
&& flip (drop_freq
[drop_level
-1]))
160 dmsg (D_GREMLIN_VERBOSE
, "GREMLIN: Random packet drop");
169 * Possibly corrupt a packet.
171 void corrupt_gremlin (struct buffer
*buf
, int flags
) {
172 const int corrupt_level
= GREMLIN_CORRUPT_LEVEL (flags
);
175 if (flip (corrupt_freq
[corrupt_level
-1]))
181 uint8_t r
= roll (0, 255);
182 int method
= roll (0, 5);
185 case 0: /* corrupt the first byte */
188 case 1: /* corrupt the last byte */
189 *(BPTR (buf
) + buf
->len
- 1) = r
;
191 case 2: /* corrupt a random byte */
192 *(BPTR(buf
) + roll (0, buf
->len
- 1)) = r
;
194 case 3: /* append a random byte */
195 buf_write (buf
, &r
, 1);
197 case 4: /* reduce length by 1 */
200 case 5: /* reduce length by a random amount */
201 buf
->len
-= roll (0, buf
->len
- 1);
204 dmsg (D_GREMLIN_VERBOSE
, "GREMLIN: Packet Corruption, method=%d", method
);
208 } while (flip (2)); /* a 50% chance we will corrupt again */
214 static void dummy(void) {}