cosmetics
[tomato.git] / release / src / router / openvpn / gremlin.c
blob37f32f765d9709f7c48f449c63bec7e8dabcee8f
1 /*
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
6 * packet compression.
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.
30 #include "syshead.h"
32 #ifdef ENABLE_DEBUG
34 #include "error.h"
35 #include "common.h"
36 #include "misc.h"
37 #include "otime.h"
38 #include "gremlin.h"
40 #include "memdbg.h"
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
47 * parameter.
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
97 * low and high.
99 static int roll(int low, int high) {
100 int ret;
101 ASSERT (low <= high);
102 ret = low + (get_random() % (high - low + 1));
103 ASSERT (ret >= low && ret <= high);
104 return ret;
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.
114 bool
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);
120 if (!initialized)
122 initialized = true;
124 if (up_down_level)
125 up = false;
126 else
127 up = true;
129 next = now;
132 if (up_down_level) /* change up/down state? */
134 if (now >= next)
136 int delta;
137 if (up)
139 delta = roll (down_low[up_down_level-1], down_high[up_down_level-1]);
140 up = false;
142 else
144 delta = roll (up_low[up_down_level-1], up_high[up_down_level-1]);
145 up = true;
148 msg (D_GREMLIN,
149 "GREMLIN: CONNECTION GOING %s FOR %d SECONDS",
150 (up ? "UP" : "DOWN"),
151 delta);
152 next = now + delta;
156 if (drop_level)
158 if (up && flip (drop_freq[drop_level-1]))
160 dmsg (D_GREMLIN_VERBOSE, "GREMLIN: Random packet drop");
161 return false;
165 return up;
169 * Possibly corrupt a packet.
171 void corrupt_gremlin (struct buffer *buf, int flags) {
172 const int corrupt_level = GREMLIN_CORRUPT_LEVEL (flags);
173 if (corrupt_level)
175 if (flip (corrupt_freq[corrupt_level-1]))
179 if (buf->len > 0)
181 uint8_t r = roll (0, 255);
182 int method = roll (0, 5);
184 switch (method) {
185 case 0: /* corrupt the first byte */
186 *BPTR (buf) = r;
187 break;
188 case 1: /* corrupt the last byte */
189 *(BPTR (buf) + buf->len - 1) = r;
190 break;
191 case 2: /* corrupt a random byte */
192 *(BPTR(buf) + roll (0, buf->len - 1)) = r;
193 break;
194 case 3: /* append a random byte */
195 buf_write (buf, &r, 1);
196 break;
197 case 4: /* reduce length by 1 */
198 --buf->len;
199 break;
200 case 5: /* reduce length by a random amount */
201 buf->len -= roll (0, buf->len - 1);
202 break;
204 dmsg (D_GREMLIN_VERBOSE, "GREMLIN: Packet Corruption, method=%d", method);
206 else
207 break;
208 } while (flip (2)); /* a 50% chance we will corrupt again */
213 #else
214 static void dummy(void) {}
215 #endif