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-2024 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 #endif
32 
33 #include "syshead.h"
34 
35 #ifdef ENABLE_DEBUG
36 
37 #include "error.h"
38 #include "common.h"
39 #include "crypto.h"
40 #include "misc.h"
41 #include "otime.h"
42 #include "gremlin.h"
43 
44 #include "memdbg.h"
45 
46 /*
47  * Parameters for packet corruption and droppage.
48  * Each parameter has 4 possible levels, 0 = disabled,
49  * while 1, 2, and 3 are enumerated in the below arrays.
50  * The parameter is a 2-bit field within the --gremlin
51  * parameter.
52  */
53 
54 /*
55  * Probability that we will drop a packet is 1 / n
56  */
57 static const int drop_freq[] = { 500, 100, 50 };
58 
59 /*
60  * Probability that we will corrupt a packet is 1 / n
61  */
62 static const int corrupt_freq[] = { 500, 100, 50 };
63 
64 /*
65  * When network goes up, it will be up for between
66  * UP_LOW and UP_HIGH seconds.
67  */
68 static const int up_low[] = { 60, 10, 5 };
69 static const int up_high[] = { 600, 60, 10 };
70 
71 /*
72  * When network goes down, it will be down for between
73  * DOWN_LOW and DOWN_HIGH seconds.
74  */
75 static const int down_low[] = { 5, 10, 10 };
76 static const int down_high[] = { 10, 60, 120 };
77 
78 /*
79  * Packet flood levels:
80  * { number of packets, packet size }
81  */
82 static const struct packet_flood_parms packet_flood_data[] =
83 {{10, 100}, {10, 1500}, {100, 1500}};
84 
85 struct packet_flood_parms
86 get_packet_flood_parms(int level)
87 {
88  ASSERT(level > 0 && level < 4);
89  return packet_flood_data [level - 1];
90 }
91 
92 /*
93  * Return true with probability 1/n
94  */
95 static bool
96 flip(int n)
97 {
98  return (get_random() % n) == 0;
99 }
100 
101 /*
102  * Return uniformly distributed random number between
103  * low and high.
104  */
105 static int
106 roll(int low, int high)
107 {
108  int ret;
109  ASSERT(low <= high);
110  ret = low + (get_random() % (high - low + 1));
111  ASSERT(ret >= low && ret <= high);
112  return ret;
113 }
114 
115 static bool initialized; /* GLOBAL */
116 static bool up; /* GLOBAL */
117 static time_t next; /* GLOBAL */
118 
119 /*
120  * Return false if we should drop a packet.
121  */
122 bool
123 ask_gremlin(int flags)
124 {
125  const int up_down_level = GREMLIN_UP_DOWN_LEVEL(flags);
126  const int drop_level = GREMLIN_DROP_LEVEL(flags);
127 
128  if (!initialized)
129  {
130  initialized = true;
131 
132  if (up_down_level)
133  {
134  up = false;
135  }
136  else
137  {
138  up = true;
139  }
140 
141  next = now;
142  }
143 
144  if (up_down_level) /* change up/down state? */
145  {
146  if (now >= next)
147  {
148  int delta;
149  if (up)
150  {
151  delta = roll(down_low[up_down_level-1], down_high[up_down_level-1]);
152  up = false;
153  }
154  else
155  {
156  delta = roll(up_low[up_down_level-1], up_high[up_down_level-1]);
157  up = true;
158  }
159 
160  msg(D_GREMLIN,
161  "GREMLIN: CONNECTION GOING %s FOR %d SECONDS",
162  (up ? "UP" : "DOWN"),
163  delta);
164  next = now + delta;
165  }
166  }
167 
168  if (drop_level)
169  {
170  if (up && flip(drop_freq[drop_level-1]))
171  {
172  dmsg(D_GREMLIN_VERBOSE, "GREMLIN: Random packet drop");
173  return false;
174  }
175  }
176 
177  return up;
178 }
179 
180 /*
181  * Possibly corrupt a packet.
182  */
183 void
184 corrupt_gremlin(struct buffer *buf, int flags)
185 {
186  const int corrupt_level = GREMLIN_CORRUPT_LEVEL(flags);
187  if (corrupt_level)
188  {
189  if (flip(corrupt_freq[corrupt_level-1]))
190  {
191  do
192  {
193  if (buf->len > 0)
194  {
195  uint8_t r = roll(0, 255);
196  int method = roll(0, 5);
197 
198  switch (method)
199  {
200  case 0: /* corrupt the first byte */
201  *BPTR(buf) = r;
202  break;
203 
204  case 1: /* corrupt the last byte */
205  *(BPTR(buf) + buf->len - 1) = r;
206  break;
207 
208  case 2: /* corrupt a random byte */
209  *(BPTR(buf) + roll(0, buf->len - 1)) = r;
210  break;
211 
212  case 3: /* append a random byte */
213  buf_write(buf, &r, 1);
214  break;
215 
216  case 4: /* reduce length by 1 */
217  --buf->len;
218  break;
219 
220  case 5: /* reduce length by a random amount */
221  buf->len -= roll(0, buf->len - 1);
222  break;
223  }
224  dmsg(D_GREMLIN_VERBOSE, "GREMLIN: Packet Corruption, method=%d", method);
225  }
226  else
227  {
228  break;
229  }
230  } while (flip(2)); /* a 50% chance we will corrupt again */
231  }
232  }
233 }
234 #endif /* ifdef ENABLE_DEBUG */
gremlin.h
buffer::len
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
get_random
long int get_random(void)
Definition: crypto.c:1611
dmsg
#define dmsg(flags,...)
Definition: error.h:148
ASSERT
#define ASSERT(x)
Definition: error.h:195
misc.h
D_GREMLIN
#define D_GREMLIN
Definition: errlevel.h:78
crypto.h
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
buf_write
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition: buffer.h:673
syshead.h
BPTR
#define BPTR(buf)
Definition: buffer.h:124
common.h
otime.h
now
time_t now
Definition: otime.c:34
config.h
D_GREMLIN_VERBOSE
#define D_GREMLIN_VERBOSE
Definition: errlevel.h:160
memdbg.h
msg
#define msg(flags,...)
Definition: error.h:144