OpenVPN
gremlin.c
Go to the documentation of this file.
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.
7  *
8  * Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
9  *
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.
13  *
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.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 /*
25  * Test protocol robustness by simulating dropped packets and
26  * network outages when the --gremlin option is used.
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #elif defined(_MSC_VER)
32 #include "config-msvc.h"
33 #endif
34 
35 #include "syshead.h"
36 
37 #ifdef ENABLE_DEBUG
38 
39 #include "error.h"
40 #include "common.h"
41 #include "crypto.h"
42 #include "misc.h"
43 #include "otime.h"
44 #include "gremlin.h"
45 
46 #include "memdbg.h"
47 
48 /*
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
53  * parameter.
54  */
55 
56 /*
57  * Probability that we will drop a packet is 1 / n
58  */
59 static const int drop_freq[] = { 500, 100, 50 };
60 
61 /*
62  * Probability that we will corrupt a packet is 1 / n
63  */
64 static const int corrupt_freq[] = { 500, 100, 50 };
65 
66 /*
67  * When network goes up, it will be up for between
68  * UP_LOW and UP_HIGH seconds.
69  */
70 static const int up_low[] = { 60, 10, 5 };
71 static const int up_high[] = { 600, 60, 10 };
72 
73 /*
74  * When network goes down, it will be down for between
75  * DOWN_LOW and DOWN_HIGH seconds.
76  */
77 static const int down_low[] = { 5, 10, 10 };
78 static const int down_high[] = { 10, 60, 120 };
79 
80 /*
81  * Packet flood levels:
82  * { number of packets, packet size }
83  */
84 static const struct packet_flood_parms packet_flood_data[] =
85 {{10, 100}, {10, 1500}, {100, 1500}};
86 
87 struct packet_flood_parms
88 get_packet_flood_parms(int level)
89 {
90  ASSERT(level > 0 && level < 4);
91  return packet_flood_data [level - 1];
92 }
93 
94 /*
95  * Return true with probability 1/n
96  */
97 static bool
98 flip(int n)
99 {
100  return (get_random() % n) == 0;
101 }
102 
103 /*
104  * Return uniformly distributed random number between
105  * low and high.
106  */
107 static int
108 roll(int low, int high)
109 {
110  int ret;
111  ASSERT(low <= high);
112  ret = low + (get_random() % (high - low + 1));
113  ASSERT(ret >= low && ret <= high);
114  return ret;
115 }
116 
117 static bool initialized; /* GLOBAL */
118 static bool up; /* GLOBAL */
119 static time_t next; /* GLOBAL */
120 
121 /*
122  * Return false if we should drop a packet.
123  */
124 bool
125 ask_gremlin(int flags)
126 {
127  const int up_down_level = GREMLIN_UP_DOWN_LEVEL(flags);
128  const int drop_level = GREMLIN_DROP_LEVEL(flags);
129 
130  if (!initialized)
131  {
132  initialized = true;
133 
134  if (up_down_level)
135  {
136  up = false;
137  }
138  else
139  {
140  up = true;
141  }
142 
143  next = now;
144  }
145 
146  if (up_down_level) /* change up/down state? */
147  {
148  if (now >= next)
149  {
150  int delta;
151  if (up)
152  {
153  delta = roll(down_low[up_down_level-1], down_high[up_down_level-1]);
154  up = false;
155  }
156  else
157  {
158  delta = roll(up_low[up_down_level-1], up_high[up_down_level-1]);
159  up = true;
160  }
161 
162  msg(D_GREMLIN,
163  "GREMLIN: CONNECTION GOING %s FOR %d SECONDS",
164  (up ? "UP" : "DOWN"),
165  delta);
166  next = now + delta;
167  }
168  }
169 
170  if (drop_level)
171  {
172  if (up && flip(drop_freq[drop_level-1]))
173  {
174  dmsg(D_GREMLIN_VERBOSE, "GREMLIN: Random packet drop");
175  return false;
176  }
177  }
178 
179  return up;
180 }
181 
182 /*
183  * Possibly corrupt a packet.
184  */
185 void
186 corrupt_gremlin(struct buffer *buf, int flags)
187 {
188  const int corrupt_level = GREMLIN_CORRUPT_LEVEL(flags);
189  if (corrupt_level)
190  {
191  if (flip(corrupt_freq[corrupt_level-1]))
192  {
193  do
194  {
195  if (buf->len > 0)
196  {
197  uint8_t r = roll(0, 255);
198  int method = roll(0, 5);
199 
200  switch (method)
201  {
202  case 0: /* corrupt the first byte */
203  *BPTR(buf) = r;
204  break;
205 
206  case 1: /* corrupt the last byte */
207  *(BPTR(buf) + buf->len - 1) = r;
208  break;
209 
210  case 2: /* corrupt a random byte */
211  *(BPTR(buf) + roll(0, buf->len - 1)) = r;
212  break;
213 
214  case 3: /* append a random byte */
215  buf_write(buf, &r, 1);
216  break;
217 
218  case 4: /* reduce length by 1 */
219  --buf->len;
220  break;
221 
222  case 5: /* reduce length by a random amount */
223  buf->len -= roll(0, buf->len - 1);
224  break;
225  }
226  dmsg(D_GREMLIN_VERBOSE, "GREMLIN: Packet Corruption, method=%d", method);
227  }
228  else
229  {
230  break;
231  }
232  } while (flip(2)); /* a 50% chance we will corrupt again */
233  }
234  }
235 }
236 
237 #else /* ifdef ENABLE_DEBUG */
238 static void
239 dummy(void)
240 {
241 }
242 #endif /* ifdef ENABLE_DEBUG */
#define dmsg
Definition: error.h:174
#define ASSERT(x)
Definition: error.h:221
list flags
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
static bool buf_write(struct buffer *dest, const void *src, int size)
Definition: buffer.h:689
#define BPTR(buf)
Definition: buffer.h:124
#define D_GREMLIN
Definition: errlevel.h:78
#define D_GREMLIN_VERBOSE
Definition: errlevel.h:154
time_t now
Definition: otime.c:36
#define msg
Definition: error.h:173
static void dummy(void)
Definition: gremlin.c:239
unsigned __int8 uint8_t
Definition: config-msvc.h:123
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
long int get_random(void)
Definition: crypto.c:1767